Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#225 #227

Closed
wants to merge 5 commits into from
13 changes: 3 additions & 10 deletions app/src/main/kotlin/com/bff/wespot/splash/SplashViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.bff.wespot.splash
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bff.wespot.domain.repository.firebase.config.RemoteConfigRepository
import com.bff.wespot.domain.util.RemoteConfigKey
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -12,20 +11,14 @@ import javax.inject.Inject

@HiltViewModel
class SplashViewModel @Inject constructor(
private val dataSource: RemoteConfigRepository
private val remoteConfigRepository: RemoteConfigRepository,
) : ViewModel() {
private val _start = MutableStateFlow(false)
val start = _start.asStateFlow()

private val _minVersion = MutableStateFlow("")
val minVersion = _minVersion.asStateFlow()

init {
viewModelScope.launch {
_start.value = dataSource.startRemoteConfig()
if (_start.value) {
_minVersion.emit(dataSource.fetchFromRemoteConfig(RemoteConfigKey.MIN_VERSION))
}
_start.value = remoteConfigRepository.startRemoteConfig()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bff.wespot.data.remote.extensions

import com.google.android.gms.tasks.Task
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import timber.log.Timber
import kotlin.coroutines.resumeWithException

@OptIn(ExperimentalCoroutinesApi::class)
suspend fun <T> Task<T>.await(): T {
return suspendCancellableCoroutine { cont ->
addOnCompleteListener {
val exception = it.exception
if (exception != null) {
cont.resumeWithException(exception)
Timber.e(exception)
} else {
cont.resume(it.result, null)
Timber.d(it.result.toString())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package com.bff.wespot.data.remote.source.firebase.messaging

interface MessagingDataSource {
suspend fun getFcmToken(): String

suspend fun removeFcmToken()
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.bff.wespot.data.remote.source.firebase.messaging

import com.bff.wespot.data.remote.extensions.await
import com.google.firebase.messaging.FirebaseMessaging
import kotlinx.coroutines.suspendCancellableCoroutine
import timber.log.Timber
import javax.inject.Inject

class MessagingDataSourceImpl @Inject constructor(
private val messaging: FirebaseMessaging,
) : MessagingDataSource {
override suspend fun getFcmToken(): String {
return suspendCancellableCoroutine { continuation ->
messaging.token.addOnCompleteListener {
continuation.resumeWith(Result.success(it.result))
}.addOnFailureListener {
Timber.e("Update FcmToken Failed Exception : ", it)
}
}
messaging.isAutoInitEnabled = true
return messaging.token.await()
}

override suspend fun removeFcmToken() {
messaging.isAutoInitEnabled = false
messaging.deleteToken().await()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ class MessagingRepositoryImpl @Inject constructor(
private val messagingDataSource: MessagingDataSource,
): MessagingRepository {
override suspend fun getFcmToken(): String = messagingDataSource.getFcmToken()

override suspend fun removeFcmToken() = messagingDataSource.removeFcmToken()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package com.bff.wespot.domain.repository.firebase.messaging

interface MessagingRepository {
suspend fun getFcmToken(): String

suspend fun removeFcmToken()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.bff.wespot.domain.repository.BasePagingRepository
import com.bff.wespot.domain.repository.DataStoreRepository
import com.bff.wespot.domain.repository.auth.AuthRepository
import com.bff.wespot.domain.repository.firebase.config.RemoteConfigRepository
import com.bff.wespot.domain.repository.firebase.messaging.MessagingRepository
import com.bff.wespot.domain.repository.message.MessageStorageRepository
import com.bff.wespot.domain.repository.user.ProfileRepository
import com.bff.wespot.domain.util.RemoteConfigKey
Expand All @@ -22,6 +23,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
import org.orbitmvi.orbit.syntax.simple.postSideEffect
Expand All @@ -35,6 +37,7 @@ class EntireViewModel @Inject constructor(
private val profileRepository: ProfileRepository,
private val authRepository: AuthRepository,
private val remoteConfigRepository: RemoteConfigRepository,
private val messagingRepository: MessagingRepository,
private val messageStorageRepository: MessageStorageRepository,
private val dataStoreRepository: DataStoreRepository,
private val messageBlockedRepository: BasePagingRepository<BlockedMessage, Paging<BlockedMessage>>,
Expand Down Expand Up @@ -124,9 +127,12 @@ class EntireViewModel @Inject constructor(
}

private fun clearCachedData() {
viewModelScope.launch {
launch { dataStoreRepository.clear() }
launch { profileRepository.clearProfile() }
viewModelScope.launch(coroutineDispatcher) {
supervisorScope {
launch { dataStoreRepository.clear() }
launch { profileRepository.clearProfile() }
launch { messagingRepository.removeFcmToken() }
}
}
}

Expand Down
Loading