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 @@ -25,7 +25,7 @@ sealed interface RemoteDestination : Parcelable {
data object PickRepo : RemoteDestination

@Parcelize
data class SelectGenerateNewKeys(
data class SelectGenerateNewSshKeys(
val url: String
) : RemoteDestination

Expand All @@ -35,6 +35,11 @@ sealed interface RemoteDestination : Parcelable {
val url: String,
) : RemoteDestination

@Parcelize
data class Credentials(
val url: String,
) : RemoteDestination

@Parcelize
data object Cloning : RemoteDestination
// @Parcelize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package io.github.wiiznokes.gitnote.ui.screen.setup.remote

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.tooling.preview.Preview
import io.github.wiiznokes.gitnote.R
import io.github.wiiznokes.gitnote.provider.GithubProvider
import io.github.wiiznokes.gitnote.provider.Provider
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.model.Cred
import io.github.wiiznokes.gitnote.ui.model.StorageConfiguration
import io.github.wiiznokes.gitnote.ui.viewmodel.SetupViewModelI
import io.github.wiiznokes.gitnote.ui.viewmodel.SetupViewModelMock


@Composable
fun CredentialsScreen(
onBackClick: () -> Unit,
vm: SetupViewModelI,
storageConfig: StorageConfiguration,
provider: Provider?,
url: String,
onClone: () -> Unit,
onSuccess: () -> Unit,
) {

AppPage(
title = "Credentials",
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
onBackClick = onBackClick,
) {


SetupPage {

if (provider is GithubProvider) {

SetupLine(
text = "Create a new access token to allow this app to access your repository. Be sure to grant the “Contents” permission with read and write access."
) {
val uriHandler = LocalUriHandler.current

SetupButton(
text = "1. " + "Create a token",
onClick = { uriHandler.openUri("https://github.com/settings/personal-access-tokens/new") },
link = true
)
}
}

val username = rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue())
}
val password = rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue())
}


val usernameFocusRequester = remember { FocusRequester() }
val passwordFocusRequester = remember { FocusRequester() }

LaunchedEffect(null) {
usernameFocusRequester.requestFocus()
}

SetupLine(
text = "Enter username"
) {

OutlinedTextField(
modifier = Modifier
.fillMaxSize()
.focusRequester(usernameFocusRequester),
value = username.value,
onValueChange = {
username.value = it
},
label = {
Text(text = "Username")
},
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Unspecified
),
keyboardActions = KeyboardActions(
onDone = {
passwordFocusRequester.requestFocus()
}
)
)
}

SetupLine(
text = "Enter password"
) {

OutlinedTextField(
modifier = Modifier
.fillMaxSize()
.focusRequester(passwordFocusRequester),
value = password.value,
onValueChange = {
password.value = it
},
label = {
Text(text = "Password")
},
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password
)
)


}

SetupLine(
text = stringResource(R.string.try_cloning)
) {

SetupButton(
text = stringResource(R.string.clone_repo),
onClick = {
vm.cloneRepo(
storageConfig = storageConfig,
remoteUrl = url,
cred = Cred.UserPassPlainText(
username = username.value.text,
password = password.value.text,
),
onSuccess = onSuccess
)

onClone()
},
enabled = username.value.text.isNotEmpty() && password.value.text.isNotEmpty()
)
}
}

}
}


@Preview
@Composable
private fun CredentialsScreenPreview() {
CredentialsScreen(
onBackClick = {},
storageConfig = StorageConfiguration.App,
url = "url",
provider = GithubProvider(),
vm = SetupViewModelMock(),
onSuccess = {},
onClone = {}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import io.github.wiiznokes.gitnote.ui.component.SetupPage

private val sshGitRegex = Regex("""^(?:git@|ssh://git@)[\w.-]+:[\w./-]+(?:\.git)?$""")

private fun isUrlCorrect(url: String): Boolean {
fun isUrlSsh(url: String): Boolean {
return sshGitRegex.matches(url)
}

private fun isUrlCorrect(url: String): Boolean {
return url.isNotBlank()
}

@Composable
fun EnterUrlWithProviderScreen(
onBackClick: () -> Unit,
Expand Down Expand Up @@ -130,7 +134,6 @@ private fun UrlTextField(url: MutableState<TextFieldValue>) {
Text(text = "git@github.com:wiiznokes/gitnote.git")
},
singleLine = true,
isError = !isUrlCorrect(url.value.text),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Uri
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private fun extractUserRepo(url: String): String? {
}

@Composable
fun GenerateNewKeysScreen(
fun GenerateNewSshKeysScreen(
onBackClick: () -> Unit,
cloneState: InitState,
provider: Provider?,
Expand Down Expand Up @@ -184,8 +184,8 @@ fun GenerateNewKeysScreen(

@Preview
@Composable
private fun GenerateNewKeysScreenPreview() {
GenerateNewKeysScreen(
private fun GenerateNewSshKeysScreenPreview() {
GenerateNewSshKeysScreen(
onBackClick = {},
cloneState = InitState.Idle,
provider = GithubProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.AuthorizeGit
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.EnterUrl
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.GenerateNewKeys
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.PickRepo
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.SelectGenerateNewKeys
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.SelectGenerateNewSshKeys
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.SelectProvider
import io.github.wiiznokes.gitnote.ui.destination.RemoteDestination.SelectSetupAutomatically
import io.github.wiiznokes.gitnote.ui.model.StorageConfiguration
Expand Down Expand Up @@ -93,18 +93,16 @@ fun RemoteScreen(
onBackClick = { navController.pop() },
provider = vm.provider!!,
onUrl = { url ->
navController.navigate(
SelectGenerateNewKeys(url = url)
)
if (isUrlSsh(url)) navController.navigate(SelectGenerateNewSshKeys(url = url))
else navController.navigate(RemoteDestination.Credentials(url = url))
}
)
} else {
EnterUrlScreen(
onBackClick = { navController.pop() },
onUrl = { url ->
navController.navigate(
SelectGenerateNewKeys(url = url)
)
if (isUrlSsh(url)) navController.navigate(SelectGenerateNewSshKeys(url = url))
else navController.navigate(RemoteDestination.Credentials(url = url))
}
)
}
Expand All @@ -122,7 +120,7 @@ fun RemoteScreen(
onClone = { navController.navigate(RemoteDestination.Cloning) }
)

is SelectGenerateNewKeys -> SelectGenerateNewKeysScreen(
is SelectGenerateNewSshKeys -> SelectGenerateNewSshKeysScreen(
onBackClick = { navController.pop() },
onGenerate = {
navController.navigate(
Expand All @@ -133,7 +131,7 @@ fun RemoteScreen(
},
)

is GenerateNewKeys -> GenerateNewKeysScreen(
is GenerateNewKeys -> GenerateNewSshKeysScreen(
onBackClick = { navController.pop() },
cloneState = initState,
provider = vm.provider,
Expand All @@ -145,6 +143,16 @@ fun RemoteScreen(
onClone = { navController.navigate(RemoteDestination.Cloning) }
)

is RemoteDestination.Credentials -> CredentialsScreen(
onBackClick = { navController.pop() },
storageConfig = storageConfig,
url = remoteDestination.url,
provider = vm.provider,
vm = vm,
onSuccess = onInitSuccess,
onClone = { navController.navigate(RemoteDestination.Cloning) }
)

RemoteDestination.Cloning -> CloningScreen(
cloneState = initState,
onCancel = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import io.github.wiiznokes.gitnote.ui.component.SetupPage


@Composable
fun SelectGenerateNewKeysScreen(
fun SelectGenerateNewSshKeysScreen(
onBackClick: () -> Unit,
onGenerate: () -> Unit,
) {
Expand Down Expand Up @@ -43,8 +43,8 @@ fun SelectGenerateNewKeysScreen(

@Preview
@Composable
private fun SelectGenerateNewKeysScreenPreview() {
SelectGenerateNewKeysScreen(
private fun SelectGenerateNewSshKeysScreenPreview() {
SelectGenerateNewSshKeysScreen(
onBackClick = {},
onGenerate = {}
)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<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. Zvolte metodu SSH</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>
<string name="open_link_create_repo">Vytvořit repozitář</string>
<string name="next">Další</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<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. Choisissez celui avec SSH "</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>
<string name="open_link_create_repo">Créer le référentiel</string>
<string name="next">Suivant</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<string name="read_only_mode_deactive">Deactivate read only mode</string>
<string name="authorize_gitnote">Authorize Gitnote</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. Choose the SSH method</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>
<string name="open_link_create_repo">Create repository</string>
<string name="next">Next</string>
Expand Down
Loading