MyDataViewModel.kt 2.29 KB
Newer Older
1
package org.dpppt.android.app.mydata
2

3
import androidx.lifecycle.MutableLiveData
4
import androidx.lifecycle.ViewModel
5
6
import io.reactivex.rxjava3.disposables.Disposable
import mobi.lab.mvvm.Event
7
import org.dpppt.android.app.common.util.asLiveData
8
import org.dpppt.android.app.common.util.backgroundToMain
9
10
import org.dpppt.android.app.domain.entity.DomainException
import org.dpppt.android.app.domain.usecase.data.DeleteLocalDataUseCase
11
import org.dpppt.android.app.domain.usecase.infopage.CreatePrivacyPolicyUrlUseCase
12
import org.dpppt.android.app.util.dispose
13
import javax.inject.Inject
14

15
16
class MyDataViewModel @Inject constructor(
    private val createPrivacyPolicyUrlUseCase: CreatePrivacyPolicyUrlUseCase,
17
    private val deleteUseCase: DeleteLocalDataUseCase
18
) : ViewModel() {
19

20
    private val _action = MutableLiveData<Event<Action>>()
21
    val action = _action.asLiveData()
22
23
24
25
26
27

    private var disposable: Disposable? = null

    override fun onCleared() {
        super.onCleared()
        dispose(disposable)
28
29
30
    }

    fun onReturnHomeClicked() {
31
        returnToHome()
32
33
34
    }

    fun onPrivacyPolicyClicked() {
35
        _action.value = Event(Action.OpenPrivacyPolicyUrl(createPrivacyPolicyUrlUseCase.execute()))
36
37
    }

38
    fun onDeleteClicked() {
39
        onDeleteData()
40
41
    }

42
    fun onDeleteData() {
43
44
45
46
47
48
49
50
51
52
        dispose(disposable)
        disposable = deleteUseCase.execute()
            .backgroundToMain()
            .subscribe(::onDeleteSuccess, ::onDeleteError)
    }

    fun onDeleteErrorReturnHomeClicked() {
        restartToHome()
    }

53
    private fun restartToHome() {
54
        _action.value = Event(Action.NavigateToHomeRestart)
55
    }
56

57
58
59
60
    private fun returnToHome() {
        _action.value = Event(Action.NavigateToHomeWithoutRestart)
    }

61
62
63
64
65
66
67
68
69
70
71
72
    private fun onDeleteSuccess() {
        _action.value = Event(Action.ShowDeleteSuccess)
        restartToHome()
    }

    private fun onDeleteError(error: Throwable) {
        _action.value = Event(Action.ShowDeleteError(DomainException.from(error)))
    }

    sealed class Action {
        object ShowDeleteSuccess : Action()
        data class ShowDeleteError(val error: DomainException) : Action()
73
74
        data class OpenPrivacyPolicyUrl(val url: String) : Action()
        object NavigateToHomeRestart : Action()
75
        object NavigateToHomeWithoutRestart : Action()
76
    }
77
}