-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplashViewModel.kt
More file actions
46 lines (42 loc) · 1.4 KB
/
SplashViewModel.kt
File metadata and controls
46 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.songlib.presentation.splash
import android.content.Context
import androidx.lifecycle.*
import com.songlib.core.helpers.NetworkUtils
import com.songlib.domain.repos.PrefsRepo
import com.songlib.domain.repos.SubsRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class SplashViewModel @Inject constructor(
private val prefsRepo: PrefsRepo,
private val subsRepo: SubsRepo,
) : ViewModel() {
private val _isLoading = MutableStateFlow(true)
val isLoading: StateFlow<Boolean> = _isLoading.asStateFlow()
fun initializeApp(context: Context) {
viewModelScope.launch {
try {
if (NetworkUtils.isNetworkAvailable(context)) {
checkSubscriptionAndTime(true)
} else {
checkSubscriptionAndTime(false)
}
_isLoading.value = false
} catch (e: Exception) {
_isLoading.value = false
} finally {
_isLoading.value = false
}
}
}
private suspend fun checkSubscriptionAndTime(isOnline: Boolean) {
if (!prefsRepo.isProUser) {
subsRepo.isProUser(isOnline) { isActive ->
prefsRepo.isProUser = isActive
}
}
prefsRepo.updateAppOpenTime()
}
}