Skip to content

Commit

Permalink
feat: Choose correct activity to start in LaunchActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Nov 26, 2024
1 parent 00c8569 commit 25bec08
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<out ComponentActivity> = when {
accountUtils.isUserConnected() -> MainActivity::class
else -> OnboardingActivity::class
}.java
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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<Any, Int> = with(sharedPreferences) {
return object : ReadWriteProperty<Any, Int> {
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 25bec08

Please sign in to comment.