2019-03-16 23:36:16 +11:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.viewmodel
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.entity.MastoList
|
|
|
|
import com.keylesspalace.tusky.network.MastodonApi
|
2020-01-03 04:50:58 +11:00
|
|
|
import com.keylesspalace.tusky.util.RxAwareViewModel
|
2019-03-16 23:36:16 +11:00
|
|
|
import com.keylesspalace.tusky.util.replacedFirstWhich
|
2020-01-03 04:50:58 +11:00
|
|
|
import com.keylesspalace.tusky.util.withoutFirstWhich
|
2021-05-17 03:53:27 +10:00
|
|
|
import io.reactivex.rxjava3.core.Observable
|
|
|
|
import io.reactivex.rxjava3.subjects.BehaviorSubject
|
|
|
|
import io.reactivex.rxjava3.subjects.PublishSubject
|
2019-03-16 23:36:16 +11:00
|
|
|
import java.io.IOException
|
|
|
|
import java.net.ConnectException
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
2020-01-03 04:50:58 +11:00
|
|
|
internal class ListsViewModel @Inject constructor(private val api: MastodonApi) : RxAwareViewModel() {
|
2019-03-16 23:36:16 +11:00
|
|
|
enum class LoadingState {
|
|
|
|
INITIAL, LOADING, LOADED, ERROR_NETWORK, ERROR_OTHER
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Event {
|
|
|
|
CREATE_ERROR, DELETE_ERROR, RENAME_ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
data class State(val lists: List<MastoList>, val loadingState: LoadingState)
|
|
|
|
|
|
|
|
val state: Observable<State> get() = _state
|
|
|
|
val events: Observable<Event> get() = _events
|
|
|
|
private val _state = BehaviorSubject.createDefault(State(listOf(), LoadingState.INITIAL))
|
|
|
|
private val _events = PublishSubject.create<Event>()
|
|
|
|
|
|
|
|
fun retryLoading() {
|
|
|
|
loadIfNeeded()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadIfNeeded() {
|
|
|
|
val state = _state.value!!
|
2019-04-21 06:36:44 +10:00
|
|
|
if (state.loadingState == LoadingState.LOADING || state.lists.isNotEmpty()) return
|
2019-03-16 23:36:16 +11:00
|
|
|
updateState {
|
|
|
|
copy(loadingState = LoadingState.LOADING)
|
|
|
|
}
|
|
|
|
|
2021-06-29 05:13:24 +10:00
|
|
|
api.getLists().subscribe(
|
|
|
|
{ lists ->
|
|
|
|
updateState {
|
|
|
|
copy(
|
2019-03-16 23:36:16 +11:00
|
|
|
lists = lists,
|
|
|
|
loadingState = LoadingState.LOADED
|
2021-06-29 05:13:24 +10:00
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{ err ->
|
|
|
|
updateState {
|
|
|
|
copy(
|
|
|
|
loadingState = if (err is IOException || err is ConnectException)
|
|
|
|
LoadingState.ERROR_NETWORK else LoadingState.ERROR_OTHER
|
|
|
|
)
|
|
|
|
}
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
2021-06-29 05:13:24 +10:00
|
|
|
).autoDispose()
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fun createNewList(listName: String) {
|
2021-06-29 05:13:24 +10:00
|
|
|
api.createList(listName).subscribe(
|
|
|
|
{ list ->
|
|
|
|
updateState {
|
|
|
|
copy(lists = lists + list)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sendEvent(Event.CREATE_ERROR)
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
2021-06-29 05:13:24 +10:00
|
|
|
).autoDispose()
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fun renameList(listId: String, listName: String) {
|
2021-06-29 05:13:24 +10:00
|
|
|
api.updateList(listId, listName).subscribe(
|
|
|
|
{ list ->
|
|
|
|
updateState {
|
|
|
|
copy(lists = lists.replacedFirstWhich(list) { it.id == listId })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sendEvent(Event.RENAME_ERROR)
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
2021-06-29 05:13:24 +10:00
|
|
|
).autoDispose()
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteList(listId: String) {
|
2021-06-29 05:13:24 +10:00
|
|
|
api.deleteList(listId).subscribe(
|
|
|
|
{
|
|
|
|
updateState {
|
|
|
|
copy(lists = lists.withoutFirstWhich { it.id == listId })
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sendEvent(Event.DELETE_ERROR)
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
2021-06-29 05:13:24 +10:00
|
|
|
).autoDispose()
|
2019-03-16 23:36:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
private inline fun updateState(crossinline fn: State.() -> State) {
|
|
|
|
_state.onNext(fn(_state.value!!))
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sendEvent(event: Event) {
|
|
|
|
_events.onNext(event)
|
|
|
|
}
|
|
|
|
}
|