Jasmin Fajkic
09/01/2022, 5:48 PMinterface SessionManager {
val currentProfileComplete: Boolean
val hasSavedProfile: Boolean
val session: Session
fun killSession()
fun saveProfile(profile: ProfileResponse?)
suspend fun getCurrentUserId(): Resource<Int>?
}
data class Session(
var profile: ProfileResponse?,
var isAuthenticated: Boolean,
)
class SessionManagerImpl @Inject constructor(
private val credentialsManager: CredentialsManager,
) : SessionManager {
private var _session = MutableStateFlow(Session(null, false))
override val session = _session.asStateFlow().value
override fun killSession() {
credentialsManager.removeCredentials()
_session.update { it.copy(profile = null, isAuthenticated = false) }
}
override fun saveProfile(profile: ProfileResponse?) {
Log.d("Prof", profile.toString())
_session.value = Session(profile, isAuthenticated = true)
}
override suspend fun getCurrentUserId(): Resource<Int>? {
return when (val userId = credentialsManager.getUserIdFromToken()) {
is Resource.Success -> userId
is Resource.Error -> null
else -> null
}
}
override val hasSavedProfile = session.profile != null
override val currentProfileComplete =
hasSavedProfile && (session.profile?.status != null || session.profile?.status === "completed")
}
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
@Inject
lateinit var sessionManager: SessionManager
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
val intent = Intent()
FirebaseApp.initializeApp(this)
Firebase.dynamicLinks
.getDynamicLink(intent)
.addOnSuccessListener(this) { pendingDynamicLinkData: PendingDynamicLinkData? ->
if (pendingDynamicLinkData != null) {
intent.data = pendingDynamicLinkData.link
this.startActivity(intent)
}
}
.addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }
setContent {
PhoenixTheme {
BottomTabView(sessionManager)
}
}
}
}
Stylianos Gakis
09/01/2022, 5:53 PMoverride val session = _session.asStateFlow().value
I think it's there. You're assigning the initial value of _session
into session
and that's it, you never change thatJasmin Fajkic
09/01/2022, 5:54 PMStylianos Gakis
09/01/2022, 5:55 PMJasmin Fajkic
09/01/2022, 5:56 PMinterface SessionManager {
val currentProfileComplete: Boolean
val hasSavedProfile: Boolean
val session: StateFlow<Session>
fun killSession()
fun saveProfile(profile: ProfileResponse?)
suspend fun getCurrentUserId(): Resource<Int>?
}
data class Session(
var profile: ProfileResponse?,
var isAuthenticated: Boolean,
)
Stylianos Gakis
09/01/2022, 6:01 PMJasmin Fajkic
09/01/2022, 6:01 PMval session = sessionManager.session.collectAsState()
Stylianos Gakis
09/01/2022, 6:04 PMsession =
flow { while(true) { emit(smth); delay(500); emit(smthElse); delay(500); } }.stateIn(...)
And see if this updates your UI.Jasmin Fajkic
09/01/2022, 6:27 PM@Composable
fun Login(navigateToMain: () ->Unit, sessionManager: SessionManager) {
val loginViewModel: LoginViewModel = hiltViewModel()
val loginState = loginViewModel.loginState.collectAsState().value
val session = sessionManager.session.collectAsState()
val session1 = loginViewModel.sessionManager.session.collectAsState()
val coroutineScope = rememberCoroutineScope()
LaunchedEffect(session.value.isAuthenticated) {
if(session.value.isAuthenticated) {
navigateToMain()
}
}
Log.d("ISAUTH1", session.value.toString())
Log.d("ISAUTH2", session1.value.toString())
@Module
@InstallIn(SingletonComponent::class)
object SessionManagerModule {
@Provides
fun providesSessionManager(
credentialsManager: CredentialsManager,
): SessionManager =
SessionManagerImpl(credentialsManager)
}
@Provides
@Singleton
fun providesSessionManager(
credentialsManager: CredentialsManager,
): SessionManager =
SessionManagerImpl(credentialsManager)
Stylianos Gakis
09/01/2022, 8:41 PM