Skip to content

Commit

Permalink
improve: save user details in sample app cache
Browse files Browse the repository at this point in the history
  • Loading branch information
maureenorea-clores authored Feb 22, 2024
1 parent ba3946f commit f0a7339
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 65 deletions.
44 changes: 36 additions & 8 deletions test/src/main/java/com/rakuten/test/AppUserInfoProvider.kt
Original file line number Diff line number Diff line change
@@ -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"
}
}
66 changes: 12 additions & 54 deletions test/src/main/java/com/rakuten/test/MainActivityFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<EditText>(R.id.edit_userid)
userId.setText(application.provider.userId)
val tokenOrIdTracking = contentView.findViewById<EditText>(R.id.edit_tokenOrTrackingId)
tokenOrIdTracking.setText(application.provider.idTracking)
userId.setText(appProvider.provideUserId())
val tokenOrIdTrackingRadio = contentView.findViewById<RadioGroup>(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<EditText>(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, _ ->
Expand Down Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/src/main/java/com/rakuten/test/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion test/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

<Button
android:id="@+id/reconfigure"
Expand Down
2 changes: 1 addition & 1 deletion test/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<string name="purchase_successful">Purchase Successful</string>
<string name="purchase_successful_twice">Purchase Successful 2x</string>
<string name="login_purchase_successful">Login and Purchase Successful</string>
<string name="custom_event_newuser">Custom Event: newUser</string>
<string name="custom_event">Custom Event</string>
<string name="change_user">Change User</string>
<string name="label_userid">User ID</string>
<string name="label_tokenOrTrackingID">Tracking ID/\nAccess Token</string>
Expand Down

0 comments on commit f0a7339

Please sign in to comment.