|
|
|
|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
|
|
|
|
package com.naviapp.webredirection.presentation.viewModel
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
|
@@ -24,7 +25,12 @@ import com.navi.common.network.models.GenericErrorResponse
|
|
|
|
|
import com.navi.common.utils.Constants.URL
|
|
|
|
|
import com.navi.common.utils.isValidResponse
|
|
|
|
|
import com.navi.common.viewmodel.BaseVM
|
|
|
|
|
import com.naviapp.analytics.utils.NaviAnalytics
|
|
|
|
|
import com.naviapp.common.navigator.NaviDeepLinkNavigator.WEB_URL
|
|
|
|
|
import com.naviapp.manager.usecase.UserDataUploadWorkerUseCase
|
|
|
|
|
import com.naviapp.registration.helper.isReadContactsPermissionGranted
|
|
|
|
|
import com.naviapp.registration.helper.isReadSmsPermissionGranted
|
|
|
|
|
import com.naviapp.utils.Constants.PL_WEB
|
|
|
|
|
import com.naviapp.utils.buildUrlWithParameters
|
|
|
|
|
import com.naviapp.utils.generateRandomString
|
|
|
|
|
import com.naviapp.utils.generateSHA256Hash
|
|
|
|
|
@@ -32,6 +38,7 @@ import com.naviapp.utils.getVersionCode
|
|
|
|
|
import com.naviapp.utils.toMutableMap
|
|
|
|
|
import com.naviapp.webredirection.data.WebRedirectionRepository
|
|
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
|
@@ -39,15 +46,21 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
|
|
|
|
|
|
|
|
@HiltViewModel
|
|
|
|
|
class WebRedirectionVM @Inject constructor(private val repository: WebRedirectionRepository) :
|
|
|
|
|
BaseVM() {
|
|
|
|
|
class WebRedirectionVM
|
|
|
|
|
@Inject
|
|
|
|
|
constructor(
|
|
|
|
|
private val repository: WebRedirectionRepository,
|
|
|
|
|
private val userDataUploadWorkerUseCase: UserDataUploadWorkerUseCase,
|
|
|
|
|
@ApplicationContext val context: Context
|
|
|
|
|
) : BaseVM() {
|
|
|
|
|
|
|
|
|
|
private val userDataAnalyticsTracker by lazy { NaviAnalytics.naviAnalytics.UserData() }
|
|
|
|
|
private var parameterMap: MutableMap<String, String> = mutableMapOf()
|
|
|
|
|
private val _screenState: MutableStateFlow<UiState> = MutableStateFlow(UiState.Loading)
|
|
|
|
|
val screenState: StateFlow<UiState>
|
|
|
|
|
get() = _screenState
|
|
|
|
|
|
|
|
|
|
fun startRedirectionProcess() {
|
|
|
|
|
fun startRedirectionProcess(screenName: String) {
|
|
|
|
|
viewModelScope.safeLaunch(Dispatchers.IO) {
|
|
|
|
|
val codeVerifier = generateRandomString(RANDOM_STRING_LENGTH)
|
|
|
|
|
val codeChallenger = generateSHA256Hash(codeVerifier)
|
|
|
|
|
@@ -57,6 +70,7 @@ class WebRedirectionVM @Inject constructor(private val repository: WebRedirectio
|
|
|
|
|
appSessionToken = BaseUtils.getSessionToken().toString()
|
|
|
|
|
)
|
|
|
|
|
if (response.isValidResponse()) {
|
|
|
|
|
handleUserData(screenName)
|
|
|
|
|
val webRedirectionDelayInMillis =
|
|
|
|
|
parameterMap[WEB_REDIRECTION_DELAY_IN_MILLIS]?.toLongWithSafe()
|
|
|
|
|
?: FirebaseRemoteConfigHelper.getLong(
|
|
|
|
|
@@ -80,6 +94,16 @@ class WebRedirectionVM @Inject constructor(private val repository: WebRedirectio
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun handleUserData(screenName: String) {
|
|
|
|
|
val applicationType = getValueFromMap(APPLICATION_TYPE).orEmpty()
|
|
|
|
|
when (applicationType) {
|
|
|
|
|
PL_WEB -> {
|
|
|
|
|
uploadUserData(screenName)
|
|
|
|
|
}
|
|
|
|
|
else -> {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun generateUrlForWebRedirect(codeVerifier: String, token: String): String? {
|
|
|
|
|
return getValueFromMap(WEB_BASE_URL)?.let { url ->
|
|
|
|
|
updateParameters(token, codeVerifier)
|
|
|
|
|
@@ -123,6 +147,26 @@ class WebRedirectionVM @Inject constructor(private val repository: WebRedirectio
|
|
|
|
|
return baseUrl.isNotNullAndNotEmpty()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun uploadUserData(screenName: String) {
|
|
|
|
|
val isReadSmsPermissionGranted = isReadSmsPermissionGranted(context)
|
|
|
|
|
if (isReadSmsPermissionGranted) {
|
|
|
|
|
userDataAnalyticsTracker.onDataPermissionAvailable(
|
|
|
|
|
screenName = screenName,
|
|
|
|
|
isReadSmsPermissionGranted = true,
|
|
|
|
|
isReadContactsPermissionGranted = isReadContactsPermissionGranted(context),
|
|
|
|
|
workerType = UserDataUploadWorkerUseCase.WEB_REDIRECTION_UPLOAD_WORKER
|
|
|
|
|
)
|
|
|
|
|
userDataUploadWorkerUseCase.initDataUploadWorker(screenName)
|
|
|
|
|
} else {
|
|
|
|
|
userDataAnalyticsTracker.onDataPermissionNotAvailable(
|
|
|
|
|
screenName = screenName,
|
|
|
|
|
isReadSmsPermissionGranted = false,
|
|
|
|
|
isReadContactsPermissionGranted = isReadContactsPermissionGranted(context),
|
|
|
|
|
workerType = UserDataUploadWorkerUseCase.WEB_REDIRECTION_UPLOAD_WORKER
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
private const val CODE_VERIFIER = "codeVerifier"
|
|
|
|
|
private const val TOKEN = "token"
|
|
|
|
|
|