use RxAwareViewModel for more ViewModels (#1613)

* use RxAwareViewModel for more ViewModels

* fix ReportViewModel
This commit is contained in:
Konrad Pozniak 2020-01-02 18:50:58 +01:00 committed by GitHub
commit cdb9d87f41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 115 deletions

View file

@ -17,26 +17,23 @@
package com.keylesspalace.tusky.viewmodel
import android.util.Log
import androidx.lifecycle.ViewModel
import com.keylesspalace.tusky.entity.Account
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.Either
import com.keylesspalace.tusky.util.Either.Left
import com.keylesspalace.tusky.util.Either.Right
import com.keylesspalace.tusky.util.RxAwareViewModel
import com.keylesspalace.tusky.util.withoutFirstWhich
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.subjects.BehaviorSubject
import javax.inject.Inject
data class State(val accounts: Either<Throwable, List<Account>>, val searchResult: List<Account>?)
class AccountsInListViewModel @Inject constructor(private val api: MastodonApi) : ViewModel() {
class AccountsInListViewModel @Inject constructor(private val api: MastodonApi) : RxAwareViewModel() {
val state: Observable<State> get() = _state
private val _state = BehaviorSubject.createDefault(State(Right(listOf()), null))
private val disposable = CompositeDisposable()
fun load(listId: String) {
val state = _state.value!!
@ -45,7 +42,7 @@ class AccountsInListViewModel @Inject constructor(private val api: MastodonApi)
updateState { copy(accounts = Right(accounts)) }
}, { e ->
updateState { copy(accounts = Left(e)) }
}).addTo(disposable)
}).autoDispose()
}
}
@ -59,7 +56,7 @@ class AccountsInListViewModel @Inject constructor(private val api: MastodonApi)
Log.i(javaClass.simpleName,
"Failed to add account to the list: ${account.username}")
})
.addTo(disposable)
.autoDispose()
}
fun deleteAccountFromList(listId: String, accountId: String) {
@ -73,7 +70,7 @@ class AccountsInListViewModel @Inject constructor(private val api: MastodonApi)
}, {
Log.i(javaClass.simpleName, "Failed to remove account from thelist: $accountId")
})
.addTo(disposable)
.autoDispose()
}
fun search(query: String) {
@ -85,7 +82,7 @@ class AccountsInListViewModel @Inject constructor(private val api: MastodonApi)
updateState { copy(searchResult = result) }
}, {
updateState { copy(searchResult = listOf()) }
}).addTo(disposable)
}).autoDispose()
}
}

View file

@ -16,14 +16,12 @@
package com.keylesspalace.tusky.viewmodel
import androidx.lifecycle.ViewModel
import com.keylesspalace.tusky.entity.MastoList
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.util.withoutFirstWhich
import com.keylesspalace.tusky.util.RxAwareViewModel
import com.keylesspalace.tusky.util.replacedFirstWhich
import com.keylesspalace.tusky.util.withoutFirstWhich
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.addTo
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
import java.io.IOException
@ -31,7 +29,7 @@ import java.net.ConnectException
import javax.inject.Inject
internal class ListsViewModel @Inject constructor(private val api: MastodonApi) : ViewModel() {
internal class ListsViewModel @Inject constructor(private val api: MastodonApi) : RxAwareViewModel() {
enum class LoadingState {
INITIAL, LOADING, LOADED, ERROR_NETWORK, ERROR_OTHER
}
@ -46,7 +44,6 @@ internal class ListsViewModel @Inject constructor(private val api: MastodonApi)
val events: Observable<Event> get() = _events
private val _state = BehaviorSubject.createDefault(State(listOf(), LoadingState.INITIAL))
private val _events = PublishSubject.create<Event>()
private val disposable = CompositeDisposable()
fun retryLoading() {
loadIfNeeded()
@ -71,7 +68,7 @@ internal class ListsViewModel @Inject constructor(private val api: MastodonApi)
copy(loadingState = if (err is IOException || err is ConnectException)
LoadingState.ERROR_NETWORK else LoadingState.ERROR_OTHER)
}
}).addTo(disposable)
}).autoDispose()
}
fun createNewList(listName: String) {
@ -81,7 +78,7 @@ internal class ListsViewModel @Inject constructor(private val api: MastodonApi)
}
}, {
sendEvent(Event.CREATE_ERROR)
}).addTo(disposable)
}).autoDispose()
}
fun renameList(listId: String, listName: String) {
@ -91,7 +88,7 @@ internal class ListsViewModel @Inject constructor(private val api: MastodonApi)
}
}, {
sendEvent(Event.RENAME_ERROR)
}).addTo(disposable)
}).autoDispose()
}
fun deleteList(listId: String) {
@ -101,7 +98,7 @@ internal class ListsViewModel @Inject constructor(private val api: MastodonApi)
}
}, {
sendEvent(Event.DELETE_ERROR)
}).addTo(disposable)
}).autoDispose()
}
private inline fun updateState(crossinline fn: State.() -> State) {