-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update project notifications settings to RX2 (#2115)
Co-authored-by: Isabel Martin <arkariang@gmail.com>
- Loading branch information
Showing
3 changed files
with
72 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 30 additions & 18 deletions
48
app/src/main/java/com/kickstarter/viewmodels/ProjectNotificationSettingsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,62 @@ | ||
package com.kickstarter.viewmodels | ||
|
||
import com.kickstarter.libs.ActivityViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.kickstarter.libs.Environment | ||
import com.kickstarter.libs.rx.transformers.Transformers | ||
import com.kickstarter.libs.utils.extensions.addToDisposable | ||
import com.kickstarter.libs.utils.extensions.isNotNull | ||
import com.kickstarter.models.ProjectNotification | ||
import com.kickstarter.services.ApiClientType | ||
import com.kickstarter.ui.activities.ProjectNotificationSettingsActivity | ||
import rx.Observable | ||
import rx.subjects.BehaviorSubject | ||
import com.kickstarter.services.ApiClientTypeV2 | ||
import io.reactivex.Observable | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.subjects.BehaviorSubject | ||
|
||
interface ProjectNotificationSettingsViewModel { | ||
interface Outputs { | ||
fun projectNotifications(): Observable<List<ProjectNotification>> | ||
fun unableToFetchProjectNotificationsError(): Observable<Void> | ||
fun unableToFetchProjectNotificationsError(): Observable<Unit> | ||
} | ||
|
||
class ViewModel(environment: Environment) : | ||
ActivityViewModel<ProjectNotificationSettingsActivity>(environment), Outputs { | ||
private val client: ApiClientType | ||
class ProjectNotificationSettingsViewModel(environment: Environment) : androidx.lifecycle.ViewModel(), Outputs { | ||
private val client: ApiClientTypeV2 = requireNotNull(environment.apiClientV2()) | ||
private val projectNotifications = BehaviorSubject.create<List<ProjectNotification>>() | ||
private val unableToFetchProjectNotificationsError = BehaviorSubject.create<Void>() | ||
private val unableToFetchProjectNotificationsError = BehaviorSubject.create<Unit>() | ||
private val disposables = CompositeDisposable() | ||
|
||
val outputs: Outputs = this | ||
|
||
override fun projectNotifications(): Observable<List<ProjectNotification>> = projectNotifications | ||
|
||
override fun unableToFetchProjectNotificationsError(): Observable<Void> = unableToFetchProjectNotificationsError | ||
override fun unableToFetchProjectNotificationsError(): Observable<Unit> = unableToFetchProjectNotificationsError | ||
|
||
init { | ||
client = requireNotNull(environment.apiClient()) | ||
|
||
val projectNotificationsNotification = client | ||
.fetchProjectNotifications() | ||
.materialize() | ||
|
||
projectNotificationsNotification | ||
.compose(Transformers.values()) | ||
.compose(bindToLifecycle()) | ||
.compose(Transformers.valuesV2()) | ||
.filter { it.isNotNull() } | ||
.subscribe { | ||
projectNotifications.onNext(it) | ||
} | ||
.addToDisposable(disposables) | ||
|
||
projectNotificationsNotification | ||
.compose(Transformers.errors()) | ||
.compose(bindToLifecycle()) | ||
.subscribe { unableToFetchProjectNotificationsError.onNext(null) } | ||
.compose(Transformers.errorsV2()) | ||
.subscribe { unableToFetchProjectNotificationsError.onNext(Unit) } | ||
.addToDisposable(disposables) | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
disposables.clear() | ||
} | ||
} | ||
|
||
class Factory(private val environment: Environment) : ViewModelProvider.Factory { | ||
override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T { | ||
return ProjectNotificationSettingsViewModel(environment) as T | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters