From 25bec085a6719a269b7acac226abba09432a1f23 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Wed, 20 Nov 2024 16:42:14 +0100 Subject: [PATCH] feat: Choose correct activity to start in LaunchActivity --- .../swisstransfer/ui/LaunchActivity.kt | 18 +++++- .../swisstransfer/ui/MainApplication.kt | 4 +- .../ui/utils/AccountPreferences.kt | 61 +++++++++++++++++++ .../swisstransfer/ui/utils/AccountUtils.kt | 25 ++++++-- 4 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountPreferences.kt diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/LaunchActivity.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/LaunchActivity.kt index 0a43d6344..0a40779ac 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/LaunchActivity.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/LaunchActivity.kt @@ -22,13 +22,27 @@ import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen +import com.infomaniak.swisstransfer.ui.utils.AccountUtils +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint @SuppressLint("CustomSplashScreen") -class LaunchActivity : ComponentActivity() { +class LaunchActivity(private val accountUtils: AccountUtils) : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() + super.onCreate(savedInstanceState) - startActivity(Intent(this, MainActivity::class.java)) + + startTargetActivity() finish() } + + private fun startTargetActivity() { + startActivity(Intent(this, chooseTargetActivity())) + } + + private fun chooseTargetActivity(): Class = when { + accountUtils.isUserConnected() -> MainActivity::class + else -> OnboardingActivity::class + }.java } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt index 127bc1f53..9ed8b2d68 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt @@ -34,7 +34,7 @@ import kotlinx.coroutines.launch import javax.inject.Inject @HiltAndroidApp -class MainApplication : Application(), Configuration.Provider { +class MainApplication(private val accountUtils: AccountUtils) : Application(), Configuration.Provider { @Inject lateinit var accountManager: AccountManager @@ -55,7 +55,7 @@ class MainApplication : Application(), Configuration.Provider { super.onCreate() globalCoroutineScope.launch { - AccountUtils.init(accountManager) + accountUtils.init() // TODO: Move to the end of the onboarding activity uploadRecaptcha.initializeClient() } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountPreferences.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountPreferences.kt new file mode 100644 index 000000000..9ba64899d --- /dev/null +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountPreferences.kt @@ -0,0 +1,61 @@ +/* + * Infomaniak SwissTransfer - Android + * Copyright (C) 2024 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.swisstransfer.ui.utils + +import android.content.Context +import android.content.SharedPreferences +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject +import javax.inject.Singleton +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +@Singleton +class AccountPreferences @Inject constructor(@ApplicationContext private val appContext: Context) { + + private val sharedPreferences = appContext.applicationContext.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE)!! + + private var _currentUserId by sharedValue("currentUserId", NO_USER) + var currentUserId + get() = _currentUserId.takeIf { it != NO_USER } + set(value) { + _currentUserId = value ?: NO_USER + } + + //region SharedValues methods to remove + // TODO: Extend SharedValues when the util class is migrated to Core2 instead of using these two methods + private fun sharedValue(key: String, defaultValue: Int): ReadWriteProperty = with(sharedPreferences) { + return object : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>): Int = getInt(key, defaultValue) + override fun setValue(thisRef: Any, property: KProperty<*>, value: Int) = transaction { putInt(key, value) } + } + } + + private fun SharedPreferences.transaction(block: SharedPreferences.Editor.() -> Unit) { + with(edit()) { + block(this) + apply() + } + } + //endregion + + companion object { + private const val SHARED_PREFS_NAME = "AccountPreferences" + private const val NO_USER = -1 + } +} diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountUtils.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountUtils.kt index 77a9fde5a..0a7f1526c 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountUtils.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/AccountUtils.kt @@ -18,12 +18,29 @@ package com.infomaniak.swisstransfer.ui.utils import com.infomaniak.multiplatform_swisstransfer.managers.AccountManager +import javax.inject.Inject +import javax.inject.Singleton -object AccountUtils { +@Singleton +class AccountUtils @Inject constructor( + private val accountManager: AccountManager, + private val accountPreferences: AccountPreferences, +) { - private const val DEFAULT_USER_ID = 0 + suspend fun init(userId: Int = DEFAULT_USER_ID) { + accountPreferences.currentUserId = userId + accountManager.loadUser(userId) + } + + suspend fun logout(userId: Int) { + accountManager.removeUser(userId) + // TODO: Handle logging as the next available connected user or the DEFAULT_USER_ID + accountPreferences.currentUserId = DEFAULT_USER_ID + } + + fun isUserConnected(): Boolean = accountPreferences.currentUserId != null - suspend fun init(accountManager: AccountManager) { - accountManager.loadUser(userId = DEFAULT_USER_ID) + companion object { + const val DEFAULT_USER_ID = 0 } }