Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class AppPreferences(
val publicKey = stringPreference("publicKey", "")
val privateKey = stringPreference("privateKey", "")

val appAuthToken = stringPreference("appAuthToken", "")

suspend fun cred(): Cred? {
return when (credType.get()) {
CredType.None -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import io.github.wiiznokes.gitnote.R
import io.github.wiiznokes.gitnote.ui.component.AppPage
import io.github.wiiznokes.gitnote.ui.component.SetupButton
import io.github.wiiznokes.gitnote.ui.component.SetupLine
import io.github.wiiznokes.gitnote.ui.component.SetupPage
import io.github.wiiznokes.gitnote.ui.viewmodel.InitState

private const val TAG = "AuthorizeGitNoteScreen"
Expand All @@ -21,39 +25,71 @@ private const val TAG = "AuthorizeGitNoteScreen"
fun AuthorizeGitNoteScreen(
onBackClick: () -> Unit,
authState: InitState,
appAuthToken: String,
onSuccess: () -> Unit,
getLaunchOAuthScreenIntent: () -> Intent,
fetchInfos: (String) -> Unit,
vmHashCode: Int,
) {

AppPage(
title = stringResource(R.string.authorize_gitnote),
title = stringResource(R.string.authorize_gitnote_title),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
onBackClick = onBackClick,
onBackClickEnabled = !authState.isLoading() && authState != InitState.AuthentificationSuccess
onBackClickEnabled = !authState.isLoading() && authState != InitState.FetchingInfosSuccess
) {

LaunchedEffect(authState) {
Log.d(TAG, "LaunchedEffect: $authState, hash=${vmHashCode}")
if (authState is InitState.AuthentificationSuccess) {
if (authState is InitState.FetchingInfosSuccess) {
onSuccess()
}
}

val ctx = LocalContext.current
var authButtonClicked = remember {
false
}

SetupPage {

SetupLine(
text = ""
) {
val ctx = LocalContext.current

SetupButton(
onClick = {
authButtonClicked = true
val intent = getLaunchOAuthScreenIntent()
ctx.startActivity(intent)
},
enabled = !authState.isLoading() && authState != InitState.FetchingInfosSuccess,
text = if (authButtonClicked && authState.isLoading()) {
authState.message()
} else {
stringResource(R.string.authorize_gitnote)
}
)
}

Button(
onClick = {
val intent = getLaunchOAuthScreenIntent()
ctx.startActivity(intent)
},
enabled = !authState.isLoading() && authState != InitState.AuthentificationSuccess
) {
if (!authState.isLoading()) {
Text(text = stringResource(R.string.authorize_gitnote))
} else {
Text(text = authState.message())
if (appAuthToken.isNotEmpty()) {
SetupLine(
text = ""
) {
SetupButton(
onClick = {
authButtonClicked = false
fetchInfos(appAuthToken)
},
enabled = !authState.isLoading() && authState != InitState.FetchingInfosSuccess,
text = if (!authButtonClicked && authState.isLoading()) {
authState.message()
} else {
"Fetch repositories metadata with the previous logged account"
}
)
}
}
}
}
Expand All @@ -70,6 +106,8 @@ private fun AuthorizeGitNoteScreenPreview() {
getLaunchOAuthScreenIntent = {
Intent()
},
vmHashCode = 0
vmHashCode = 0,
appAuthToken = "hello",
fetchInfos = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ fun RemoteScreen(
vm.setStateToIdle()
},
getLaunchOAuthScreenIntent = { vm.getLaunchOAuthScreenIntent() },
vmHashCode = vm.hashCode()
vmHashCode = vm.hashCode(),
appAuthToken = vm.prefs.appAuthToken.getAsState().value,
fetchInfos = vm::fetchInfos
)

is EnterUrl -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
var repos = listOf<RepoInfo>()
private set

private var token = String()

lateinit var userInfo: UserInfo
private set

Expand Down Expand Up @@ -261,13 +259,21 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
CoroutineScope(Dispatchers.IO).launch {

_initState.emit(InitState.GettingAccessToken)
token = try {
val token = try {
provider!!.exchangeCodeForAccessToken(code)
} catch (e: Exception) {
Log.e(TAG, "exchangeCodeForAccessToken: ${e.message}, $e")
_initState.emit(InitState.Error(e.message))
return@launch
}
prefs.appAuthToken.update(token)

fetchInfos(token = token)
}
}

fun fetchInfos(token: String) {
CoroutineScope(Dispatchers.IO).launch {

_initState.emit(InitState.FetchingRepos)

Expand All @@ -289,7 +295,7 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
}

Log.d(TAG, "emit: Success")
_initState.emit(InitState.AuthentificationSuccess)
_initState.emit(InitState.FetchingInfosSuccess)
}
}

Expand All @@ -305,7 +311,7 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
_initState.emit(InitState.AddingDeployKey)
try {
provider!!.addDeployKeyToRepo(
token = token,
token = prefs.appAuthToken.get(),
publicKey = publicKey,
fullRepoName = repoName
)
Expand Down Expand Up @@ -340,7 +346,7 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
_initState.emit(InitState.CreatingRemoteRepo)
try {
provider!!.createNewRepo(
token = token,
token = prefs.appAuthToken.get(),
repoName = repoName
)
} catch (e: Exception) {
Expand All @@ -354,7 +360,7 @@ class SetupViewModel(val authFlow: SharedFlow<String>) : ViewModel(), SetupViewM
_initState.emit(InitState.AddingDeployKey)
try {
provider!!.addDeployKeyToRepo(
token = token,
token = prefs.appAuthToken.get(),
publicKey = publicKey,
fullRepoName = repoName
)
Expand Down Expand Up @@ -389,7 +395,7 @@ sealed class InitState {
data object FetchingRepos : InitState()
data object GettingUserInfo : InitState()

data object AuthentificationSuccess : InitState()
data object FetchingInfosSuccess : InitState()

data object CreatingRemoteRepo : InitState()
data object AddingDeployKey : InitState()
Expand All @@ -412,9 +418,9 @@ sealed class InitState {
GettingAccessToken -> "Getting the access token"
GettingUserInfo -> "Getting user information"
Idle -> ""
AuthentificationSuccess -> ""
FetchingInfosSuccess -> ""
}
}

fun isLoading(): Boolean = this !is Idle && this !is Error && this !is AuthentificationSuccess
fun isLoading(): Boolean = this !is Idle && this !is Error && this !is FetchingInfosSuccess
}
1 change: 0 additions & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@

<string name="read_only_mode_activate">Aktivovat režim jen pro čtení</string>
<string name="read_only_mode_deactive">Deaktivovat režim jen pro čtení</string>
<string name="authorize_gitnote">Autorizovat Gitnote</string>
<string name="clone_url">Klonovat URL</string>
<string name="url_explain_create_remote_repo">Přejděte na webovou stránku, vytvořte repozitář a zkopírujte adresu URL klonu git</string>
<string name="url_explain_enter_url">Zadejte adresu URL klonu Git</string>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@

<string name="read_only_mode_activate">Activer la lecture seule</string>
<string name="read_only_mode_deactive">Desactiver la lecture seule</string>
<string name="authorize_gitnote">Autoriser Gitnote</string>
<string name="clone_url">Clone URL</string>
<string name="url_explain_create_remote_repo">"Allez sur la page Web, créez un référentiel et copiez son URL"</string>
<string name="url_explain_enter_url">Entrez l\'URL de clonage</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@

<string name="read_only_mode_activate">Activate read only mode</string>
<string name="read_only_mode_deactive">Deactivate read only mode</string>
<string name="authorize_gitnote">Authorize Gitnote</string>
<string name="authorize_gitnote_title">Fetch repositories metadata</string>
<string name="authorize_gitnote">Authorize Gitnote and fetch repositories metadata</string>
<string name="clone_url">Clone URL</string>
<string name="url_explain_create_remote_repo">Go to the website, create a repository and copy its git clone URL</string>
<string name="url_explain_enter_url">Enter the Git clone URL</string>
Expand Down
Loading