From f0a7339c4b645dd7ba683443a1c2ca9fc0911f45 Mon Sep 17 00:00:00 2001 From: maureenorea-clores <93700127+maureenorea-clores@users.noreply.github.com> Date: Thu, 22 Feb 2024 15:04:16 +0900 Subject: [PATCH] improve: save user details in sample app cache --- .../com/rakuten/test/AppUserInfoProvider.kt | 44 ++++++++++--- .../com/rakuten/test/MainActivityFragment.kt | 66 ++++--------------- .../java/com/rakuten/test/MainApplication.kt | 2 +- test/src/main/res/layout/fragment_main.xml | 2 +- test/src/main/res/values/strings.xml | 2 +- 5 files changed, 51 insertions(+), 65 deletions(-) diff --git a/test/src/main/java/com/rakuten/test/AppUserInfoProvider.kt b/test/src/main/java/com/rakuten/test/AppUserInfoProvider.kt index e753f712..79bd92f8 100644 --- a/test/src/main/java/com/rakuten/test/AppUserInfoProvider.kt +++ b/test/src/main/java/com/rakuten/test/AppUserInfoProvider.kt @@ -1,16 +1,44 @@ package com.rakuten.test +import android.content.Context import com.rakuten.tech.mobile.inappmessaging.runtime.UserInfoProvider +import com.rakuten.tech.mobile.sdkutils.PreferencesUtil -class AppUserInfoProvider : UserInfoProvider { - - var userId = "" - var accessToken = "" - var idTracking = "" +class AppUserInfoProvider(private val context: Context) : UserInfoProvider { - override fun provideAccessToken() = accessToken + override fun provideAccessToken(): String { + if (!isIdTracking()) { + return PreferencesUtil.getString(context, SHARED_FILE, TOKEN_OR_ID_TRACKING, "").orEmpty() + } + return "" + } + + override fun provideUserId(): String { + return PreferencesUtil.getString(context, SHARED_FILE, USER_ID, "").orEmpty() + } + + override fun provideIdTrackingIdentifier(): String { + if (isIdTracking()) { + return PreferencesUtil.getString(context, SHARED_FILE, TOKEN_OR_ID_TRACKING, "").orEmpty() + } + return "" + } + + fun isIdTracking(): Boolean { + return PreferencesUtil.getBoolean(context, SHARED_FILE, IS_ID_TRACKING, true) + } - override fun provideUserId() = userId + fun saveUser(userId: String, tokenOrIdTracking: String, isIdTracking: Boolean) { + PreferencesUtil.clear(context, SHARED_FILE) + PreferencesUtil.putString(context, SHARED_FILE, USER_ID, userId) + PreferencesUtil.putString(context, SHARED_FILE, TOKEN_OR_ID_TRACKING, tokenOrIdTracking) + PreferencesUtil.putBoolean(context, SHARED_FILE, IS_ID_TRACKING, isIdTracking) + } - override fun provideIdTrackingIdentifier() = idTracking + companion object { + private const val USER_ID: String = "user-id" + private const val SHARED_FILE: String = "user-shared-file" + private const val TOKEN_OR_ID_TRACKING: String = "token-or-id-tracking" + private const val IS_ID_TRACKING: String = "is-id-tracking" + } } \ No newline at end of file diff --git a/test/src/main/java/com/rakuten/test/MainActivityFragment.kt b/test/src/main/java/com/rakuten/test/MainActivityFragment.kt index 86b484e0..15fd71fe 100644 --- a/test/src/main/java/com/rakuten/test/MainActivityFragment.kt +++ b/test/src/main/java/com/rakuten/test/MainActivityFragment.kt @@ -16,21 +16,10 @@ import com.rakuten.tech.mobile.inappmessaging.runtime.data.models.appevents.AppS import com.rakuten.tech.mobile.inappmessaging.runtime.data.models.appevents.CustomEvent import com.rakuten.tech.mobile.inappmessaging.runtime.data.models.appevents.LoginSuccessfulEvent import com.rakuten.tech.mobile.inappmessaging.runtime.data.models.appevents.PurchaseSuccessfulEvent -import com.rakuten.tech.mobile.sdkutils.PreferencesUtil class MainActivityFragment : Fragment(), View.OnClickListener { - - private var tokenOrIdTrackingType = 0 // Use Tracking ID over access token by default private var ignoredContexts = "" - override fun onCreate(savedInstanceState: Bundle?) { - context?.let { - updateUser(PreferencesUtil.getString(it, SHARED_FILE, USER_ID, "user1") ?: "user1", - PreferencesUtil.getString(it, SHARED_FILE, TOKEN_OR_ID_TRACKING, "trackingId1") ?: "trackingId1") - } - super.onCreate(savedInstanceState) - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_main, container, false) @@ -129,27 +118,26 @@ class MainActivityFragment : Fragment(), View.OnClickListener { private fun showUserInfo() { val contentView = LayoutInflater.from(activity).inflate(R.layout.dialog_users, null) - val application = activity?.application as MainApplication + val appProvider = (activity?.application as MainApplication).provider val userId = contentView.findViewById(R.id.edit_userid) - userId.setText(application.provider.userId) - val tokenOrIdTracking = contentView.findViewById(R.id.edit_tokenOrTrackingId) - tokenOrIdTracking.setText(application.provider.idTracking) + userId.setText(appProvider.provideUserId()) val tokenOrIdTrackingRadio = contentView.findViewById(R.id.dialog_radio_group) - tokenOrIdTrackingRadio.check(tokenOrIdTrackingRadio.getChildAt(tokenOrIdTrackingType).id) + val isIdTracking = appProvider.isIdTracking() + tokenOrIdTrackingRadio.check(tokenOrIdTrackingRadio.getChildAt(if (isIdTracking) 0 else 1).id) + val tokenOrIdTracking = contentView.findViewById(R.id.edit_tokenOrTrackingId) + tokenOrIdTracking.setText( + if (isIdTracking) appProvider.provideIdTrackingIdentifier() else appProvider.provideAccessToken()) val dialog = AlertDialog.Builder(activity) .setView(contentView) .setTitle(R.string.dialog_title_user) .setPositiveButton(android.R.string.ok) { dialog, _ -> - if (application.provider.userId != userId.text.toString()) { - InAppMessaging.instance().closeMessage() - } - - updateUser(userId.text.toString(), tokenOrIdTracking.text.toString()) - tokenOrIdTrackingType = tokenOrIdTrackingRadio.indexOfChild( - tokenOrIdTrackingRadio.findViewById(tokenOrIdTrackingRadio.checkedRadioButtonId)) - + val tokenOrIdTrackingType = tokenOrIdTrackingRadio + .indexOfChild(tokenOrIdTrackingRadio.findViewById(tokenOrIdTrackingRadio.checkedRadioButtonId)) + appProvider.saveUser(userId.text.toString(), tokenOrIdTracking.text.toString(), + tokenOrIdTrackingType == 0) + InAppMessaging.instance().logEvent(LoginSuccessfulEvent()) dialog.dismiss() } .setNegativeButton(android.R.string.cancel) {dialog, _ -> @@ -191,34 +179,4 @@ class MainActivityFragment : Fragment(), View.OnClickListener { dialog.show() } - - override fun onDestroy() { - activity?.let { - val application = it.application as MainApplication - PreferencesUtil.putString(it, SHARED_FILE, USER_ID, application.provider.userId) - PreferencesUtil.putString(it, SHARED_FILE, TOKEN_OR_ID_TRACKING, - if (tokenOrIdTrackingType == 0) application.provider.idTracking else application.provider.accessToken) - } - super.onDestroy() - } - - private fun updateUser(userId: String, tokenOrIdTracking: String) { - val application = activity?.application as MainApplication - application.provider.userId = userId - - // Only one of access token or ID tracking is expected to be set at the same time - if (tokenOrIdTrackingType == 0) { - application.provider.accessToken = "" - application.provider.idTracking = tokenOrIdTracking - } else { - application.provider.idTracking = "" - application.provider.accessToken = tokenOrIdTracking - } - } - - companion object { - private const val USER_ID: String = "user-id" - private const val SHARED_FILE: String = "user-shared-file" - private const val TOKEN_OR_ID_TRACKING: String = "token-or-id-tracking" - } } \ No newline at end of file diff --git a/test/src/main/java/com/rakuten/test/MainApplication.kt b/test/src/main/java/com/rakuten/test/MainApplication.kt index 42fd1571..75791061 100644 --- a/test/src/main/java/com/rakuten/test/MainApplication.kt +++ b/test/src/main/java/com/rakuten/test/MainApplication.kt @@ -5,7 +5,7 @@ import com.rakuten.tech.mobile.inappmessaging.runtime.InAppMessaging class MainApplication : Application() { - val provider = AppUserInfoProvider() + val provider = AppUserInfoProvider(this) lateinit var settings: IAMSettings override fun onCreate() { diff --git a/test/src/main/res/layout/fragment_main.xml b/test/src/main/res/layout/fragment_main.xml index 0a604b07..27a91681 100644 --- a/test/src/main/res/layout/fragment_main.xml +++ b/test/src/main/res/layout/fragment_main.xml @@ -79,7 +79,7 @@ android:id="@+id/custom_event" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="@string/custom_event_newuser"/> + android:text="@string/custom_event"/>