Skip to content

Commit

Permalink
Added state storage
Browse files Browse the repository at this point in the history
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
  • Loading branch information
ArnyminerZ committed Sep 17, 2024
1 parent 7923ec9 commit 235a5da
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize

@OptIn(SavedStateHandleSaveableApi::class)
@HiltViewModel(assistedFactory = LoginScreenModel.Factory::class)
class LoginScreenModel @AssistedInject constructor(
savedStateHandle: SavedStateHandle,
@Assisted val initialLoginType: LoginType,
@Assisted val skipLoginTypePage: Boolean,
@Assisted val initialLoginInfo: LoginInfo,
Expand Down Expand Up @@ -71,10 +73,14 @@ class LoginScreenModel @AssistedInject constructor(
else
Page.LoginType

var page by mutableStateOf(startPage)
var page by savedStateHandle.saveable {
mutableStateOf(startPage)
}
private set

var finish by mutableStateOf(false)
var finish by savedStateHandle.saveable {
mutableStateOf(false)
}
private set


Expand Down Expand Up @@ -146,7 +152,9 @@ class LoginScreenModel @AssistedInject constructor(
val loginType: LoginType
): Parcelable

var loginTypeUiState by mutableStateOf(LoginTypeUiState(loginType = initialLoginType))
var loginTypeUiState by savedStateHandle.saveable {
mutableStateOf(LoginTypeUiState(loginType = initialLoginType))
}
private set

fun selectLoginType(loginType: LoginType) {
Expand All @@ -166,10 +174,14 @@ class LoginScreenModel @AssistedInject constructor(
val loginInfo: LoginInfo
): Parcelable

var loginDetailsUiState by mutableStateOf(LoginDetailsUiState(
loginType = initialLoginType,
loginInfo = loginInfo
))
var loginDetailsUiState by savedStateHandle.saveable {
mutableStateOf(
LoginDetailsUiState(
loginType = initialLoginType,
loginInfo = loginInfo
)
)
}
private set

fun updateLoginInfo(loginInfo: LoginInfo) {
Expand All @@ -187,7 +199,9 @@ class LoginScreenModel @AssistedInject constructor(
val logs: String? = null
): Parcelable

var detectResourcesUiState by mutableStateOf(DetectResourcesUiState())
var detectResourcesUiState by savedStateHandle.saveable {
mutableStateOf(DetectResourcesUiState())
}
private set

private var foundConfig: DavResourceFinder.Configuration? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import at.bitfire.davdroid.network.NextcloudLoginFlow
Expand All @@ -16,29 +17,29 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.logging.Level
import java.util.logging.Logger
import kotlinx.coroutines.launch
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import java.util.logging.Level
import java.util.logging.Logger

@HiltViewModel(assistedFactory = NextcloudLoginModel.Factory::class)
class NextcloudLoginModel @AssistedInject constructor(
@Assisted val initialLoginInfo: LoginInfo,
@ApplicationContext val context: Context,
private val logger: Logger
//val state: SavedStateHandle
private val logger: Logger,
val state: SavedStateHandle
): ViewModel() {

@AssistedFactory
interface Factory {
fun create(loginInfo: LoginInfo): NextcloudLoginModel
}

/*companion object {
companion object {
const val STATE_POLL_URL = "poll_url"
const val STATE_TOKEN = "token"
}*/
}

data class UiState(
val baseUrl: String = "",
Expand Down Expand Up @@ -93,7 +94,8 @@ class NextcloudLoginModel @AssistedInject constructor(
val loginFlow = NextcloudLoginFlow(context)

// Login flow state
/*private var pollUrl: HttpUrl?
// Login flow state
private var pollUrl: HttpUrl?
get() = state.get<String>(STATE_POLL_URL)?.toHttpUrlOrNull()
set(value) {
state[STATE_POLL_URL] = value.toString()
Expand All @@ -102,7 +104,7 @@ class NextcloudLoginModel @AssistedInject constructor(
get() = state.get<String>(STATE_TOKEN)
set(value) {
state[STATE_TOKEN] = value
}*/
}

override fun onCleared() {
loginFlow.close()
Expand All @@ -126,6 +128,10 @@ class NextcloudLoginModel @AssistedInject constructor(
try {
val loginUrl = loginFlow.initiate(baseUrl)

// Those values get initialized by loginFlow.initiate
pollUrl = loginFlow.pollUrl
token = loginFlow.token

uiState = uiState.copy(
loginUrl = loginUrl,
inProgress = false
Expand Down Expand Up @@ -155,6 +161,9 @@ class NextcloudLoginModel @AssistedInject constructor(
inProgress = true
)

pollUrl?.let { loginFlow.pollUrl = it }
token?.let { loginFlow.token = it }

val loginInfo = try {
loginFlow.fetchLoginInfo()
} catch (e: Exception) {
Expand Down

0 comments on commit 235a5da

Please sign in to comment.