Skip to content

Commit

Permalink
refactor(prefs): reorganize state management
Browse files Browse the repository at this point in the history
SUITEDEV-36367

Co-authored-by: LasOri <[email protected]>
Co-authored-by: megamegax <[email protected]>
  • Loading branch information
3 people committed Sep 13, 2024
1 parent fd791a7 commit b926e48
Show file tree
Hide file tree
Showing 18 changed files with 205 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SampleApplication : Application(), EventHandler, NotificationInformationLi

val config = EmarsysConfig(
application = this,
applicationCode = if (Prefs.applicationCode.isEmpty()) null else Prefs.applicationCode,
applicationCode = Prefs.applicationCode.ifEmpty { null },
merchantId = Prefs.merchantId,
verboseConsoleLoggingEnabled = true
)
Expand All @@ -37,7 +37,10 @@ class SampleApplication : Application(), EventHandler, NotificationInformationLi
Emarsys.setup(config)
createNotificationChannels()
setupEventHandlers()
setContactIfPresent(context)
}

private fun setContactIfPresent(context: Context) {
if (Prefs.contactFieldId != 0 && Prefs.contactFieldValue.isNotEmpty()) {
Emarsys.setContact(
contactFieldValue = Prefs.contactFieldValue,
Expand All @@ -51,7 +54,7 @@ class SampleApplication : Application(), EventHandler, NotificationInformationLi
}
}

fun setupEventHandlers() {
private fun setupEventHandlers() {
if (Prefs.applicationCode.isNotEmpty()) {
Emarsys.push.setNotificationEventHandler(this)
Emarsys.push.setSilentMessageEventHandler(this)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.emarsys.sample.dashboard

import android.app.Application
import android.content.Context
import android.os.Build
import android.util.Log
Expand All @@ -22,12 +21,10 @@ import androidx.compose.ui.res.stringResource
import coil.annotation.ExperimentalCoilApi
import com.emarsys.Emarsys
import com.emarsys.sample.R
import com.emarsys.sample.SampleApplication
import com.emarsys.sample.dashboard.button.CheckLocationPermissionsButton
import com.emarsys.sample.dashboard.button.CopyPushTokenButton
import com.emarsys.sample.dashboard.button.TrackPushTokenButton
import com.emarsys.sample.dashboard.button.trackPushToken
import com.emarsys.sample.main.sdkinfo.SetupInfo
import com.emarsys.sample.ui.component.ColumnWithTapGesture
import com.emarsys.sample.ui.component.button.GoogleSignInButton
import com.emarsys.sample.ui.component.button.StyledTextButton
Expand All @@ -46,9 +43,8 @@ import kotlin.system.exitProcess

class DashboardScreen(
override val context: Context,
override val application: Application
private val viewModel: DashboardViewModel
) : DetailScreen() {
private val viewModel = DashboardViewModel()

@OptIn(ExperimentalMaterialApi::class)
@ExperimentalCoilApi
Expand Down Expand Up @@ -104,8 +100,6 @@ class DashboardScreen(
Emarsys.config.changeMerchantId(
merchantId = viewModel.getTfMerchantIdValue(),
)
SetupInfo.merchantId = viewModel.getTfMerchantIdValue()
SetupInfo.notifyObservers()
customTextToast(
context,
context.getString(R.string.merchant_id_change_success)
Expand All @@ -130,7 +124,7 @@ class DashboardScreen(
Text(text = stringResource(id = R.string.enable))
Switch(
checked = viewModel.isGeofenceEnabled(),
onCheckedChange = { enabled ->
onCheckedChange = { isEnabled ->
if (!viewModel.isGeofenceEnabled()) {
Emarsys.geofence.enable {
if (it != null) {
Expand All @@ -140,7 +134,7 @@ class DashboardScreen(
context.getString(R.string.something_went_wrong)
)
} else {
viewModel.geofenceEnabled.value = enabled
viewModel.geofenceEnabled.value = isEnabled
customTextToast(
context,
context.getString(R.string.geofence_enabled)
Expand All @@ -149,7 +143,7 @@ class DashboardScreen(
}
} else {
Emarsys.geofence.disable()
viewModel.geofenceEnabled.value = enabled
viewModel.geofenceEnabled.value = isEnabled
customTextToast(context, context.getString(R.string.geofence_disabled))
}
}
Expand Down Expand Up @@ -208,59 +202,44 @@ class DashboardScreen(
account.email!!
) {
if (verifyLogin(it, context)) {
setupInfoSetContact(context)
setContact(context)
}
}
}
}

private fun onChangeAppCodeClicked(somethingWentWrong: String, appCodeChangeSuccess: String) {
Emarsys.config.changeApplicationCode(
applicationCode = viewModel.getTfAppCodeValue(),
completionListener = { changeError ->
if (viewModel.shouldChangeEnv()) {
if (changeError != null) {
customTextToast(context, somethingWentWrong)
}
exitProcess(0)
} else if (changeError != null) {
customTextToast(context, somethingWentWrong)
} else {
if (viewModel.hasLogin()) {
setupInfoClearContact()
customTextToast(context, appCodeChangeSuccess)
} else {
(application as SampleApplication).setupEventHandlers()
customTextToast(context, appCodeChangeSuccess)
}
viewModel.resetContactInfo()
}
applicationCode = viewModel.getTfAppCodeValue()
) { changeError ->
if (changeError != null) {
customTextToast(context, somethingWentWrong)
} else {
viewModel.clearContact()
customTextToast(context, appCodeChangeSuccess)
}
)
SetupInfo.applicationCode = viewModel.getTfAppCodeValue()
SetupInfo.notifyObservers()
if (viewModel.shouldChangeEnv()) {
exitProcess(0)
}
}
}

private fun onLoginClicked() {
if (!viewModel.hasLogin()) {
if (viewModel.isContactDataPresent()
if (!viewModel.hasLogin() && viewModel.isContactDataPresent()) {
Emarsys.setContact(
contactFieldId = viewModel.getTfContactFieldIdValue().toInt(),
contactFieldValue = viewModel.getTfContactFieldValue()
) {
Emarsys.setContact(
contactFieldId = viewModel.getTfContactFieldIdValue().toInt(),
contactFieldValue = viewModel.getTfContactFieldValue()
) {
if (verifyLogin(it, context)) {
setupInfoSetContact(context)
}
if (verifyLogin(it, context)) {
setContact(context)
}
}
} else {
Emarsys.clearContact {
if (it != null) {
customTextToast(context, context.getString(R.string.log_out_fail))
} else {
setupInfoClearContact()
viewModel.resetContactInfo()
viewModel.clearContact()
customTextToast(context, context.getString(R.string.log_out_success))
}
}
Expand All @@ -281,18 +260,11 @@ class DashboardScreen(
}
}

private fun setupInfoClearContact() {
SetupInfo.contactFieldId = 0.toString()
SetupInfo.contactFieldValue = ""
SetupInfo.loggedIn = false
SetupInfo.notifyObservers()
}

private fun setupInfoSetContact(context: Context) {
SetupInfo.contactFieldValue = viewModel.getTfContactFieldValue()
SetupInfo.contactFieldId = viewModel.getTfContactFieldIdValue()
SetupInfo.loggedIn = true
SetupInfo.notifyObservers()
private fun setContact(context: Context) {
viewModel.setContact(
fieldId = viewModel.getTfContactFieldIdValue(),
fieldValue = viewModel.getTfContactFieldValue()
)
viewModel.isLoggedIn.value = true
trackPushToken(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import com.emarsys.Emarsys
import com.emarsys.sample.pref.Prefs
import com.emarsys.sample.pref.update

class DashboardViewModel : ViewModel() {

Expand All @@ -30,18 +31,10 @@ class DashboardViewModel : ViewModel() {
return this.tfContactFieldId.value
}

fun setTfContactFieldId(value: String) {
this.tfContactFieldId.value = value
}

fun getTfContactFieldValue(): String {
return this.tfContactFieldValue.value
}

fun setTfContactField(value: String) {
this.tfContactFieldValue.value = value
}

fun hasLogin(): Boolean {
return this.isLoggedIn.value
}
Expand All @@ -66,12 +59,6 @@ class DashboardViewModel : ViewModel() {
return this.geofenceEnabled.value
}

fun resetContactInfo() {
this.setTfContactField("")
this.setTfContactFieldId("0")
this.isLoggedIn.value = false
}

fun isContactDataPresent(): Boolean {
return if (
this.getTfContactFieldValue().isNotEmpty() &&
Expand All @@ -97,4 +84,31 @@ class DashboardViewModel : ViewModel() {
fun isErrorVisible(): Boolean {
return this.errorVisible.value
}

fun getInfoMap(): Map<String, String> {
return mapOf(
"loggedIn" to isLoggedIn.value.toString(),
"applicationCode" to tfAppCode.value,
"merchantId" to tfMerchantId.value,
"hardwareId" to Prefs.hardwareId,
"languageCode" to Prefs.languageCode,
"sdkVersion" to Prefs.sdkVersion,
"contactFieldValue" to tfContactFieldValue.value,
"contactFieldId" to tfContactFieldId.value
)
}

fun clearContact() {
tfContactFieldId.value = "0"
tfContactFieldValue.value = ""
isLoggedIn.value = false
Prefs.update(getInfoMap())
}

fun setContact(fieldId: String, fieldValue: String) {
tfContactFieldId.value = fieldId
tfContactFieldValue.value = fieldValue
isLoggedIn.value = true
Prefs.update(getInfoMap())
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.emarsys.sample.inapp

import android.app.Application
import android.content.Context
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -46,8 +44,7 @@ import com.emarsys.sample.ui.style.rowWithPointEightWidth
import java.util.UUID

class InAppScreen(
override val context: Context,
override val application: Application
override val context: Context
) : DetailScreen() {
private val trackCustomEvent = {
if (viewModel.isEventPresent()) {
Expand All @@ -71,7 +68,6 @@ class InAppScreen(

private val viewModel = InAppViewModel()

@OptIn(ExperimentalAnimationApi::class)
@ExperimentalCoilApi
@ExperimentalComposeUiApi
@Composable
Expand Down
13 changes: 8 additions & 5 deletions sample/src/main/kotlin/com/emarsys/sample/inbox/InboxScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.emarsys.sample.inbox

import android.app.Application
import android.content.Context
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.PaddingValues
Expand All @@ -10,7 +9,12 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
Expand All @@ -26,12 +30,11 @@ import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import kotlinx.coroutines.delay

class InboxScreen(
override val context: Context,
override val application: Application
override val context: Context
) : DetailScreen() {

private val viewModel = InboxViewModel()
private val messagePresenter = MessagePresenter(context, viewModel)
private val messagePresenter = MessagePresenter(context)

@ExperimentalCoilApi
@ExperimentalComposeUiApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material.Card
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
Expand All @@ -18,6 +22,7 @@ import com.emarsys.mobileengage.api.inbox.Message
import com.emarsys.sample.R
import com.emarsys.sample.inbox.component.InboxButton
import com.emarsys.sample.inbox.event.InboxAppEventHandler
import com.emarsys.sample.ui.cardWithFullWidth
import com.emarsys.sample.ui.component.button.StyledTextButton
import com.emarsys.sample.ui.component.divider.DividerWithBackgroundColor
import com.emarsys.sample.ui.component.row.RowWithCenteredContent
Expand All @@ -26,11 +31,10 @@ import com.emarsys.sample.ui.component.text.TextWithFullWidthLine
import com.emarsys.sample.ui.component.text.TitleText
import com.emarsys.sample.ui.component.textfield.StyledTextField
import com.emarsys.sample.ui.component.toast.customTextToast
import com.emarsys.sample.ui.style.cardWithFullWidth
import com.emarsys.sample.ui.style.columnWithMaxWidth
import com.emarsys.sample.ui.style.rowWithMaxWidth

class MessagePresenter(private val context: Context, private val viewModel: InboxViewModel) {
class MessagePresenter(private val context: Context) {

private val inboxAppEventHandler = InboxAppEventHandler()

Expand Down
Loading

0 comments on commit b926e48

Please sign in to comment.