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 @@ -11,6 +11,7 @@ import androidx.room.Upsert
import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery
import io.github.wiiznokes.gitnote.data.platform.NodeFs
import io.github.wiiznokes.gitnote.manager.Progress
import io.github.wiiznokes.gitnote.ui.model.GridNote
import io.github.wiiznokes.gitnote.ui.model.SortOrder
import io.github.wiiznokes.gitnote.ui.screen.app.DrawerFolderModel
Expand All @@ -29,7 +30,7 @@ interface RepoDatabaseDao {

// todo: use @Transaction
// todo: don't clear the all database each time
suspend fun clearAndInit(rootPath: String, timestamps: HashMap<String, Long>) {
suspend fun clearAndInit(rootPath: String, timestamps: HashMap<String, Long>, progressCb: ((Progress) -> Unit)? = null) {
Log.d(TAG, "clearAndInit")
clearDatabase()

Expand Down Expand Up @@ -83,6 +84,7 @@ interface RepoDatabaseDao {
)
//Log.d(TAG, "add noteFolder: $noteFolder")
insertNoteFolder(noteFolder)
progressCb?.invoke(Progress.GeneratingDatabase(noteFolder.relativePath))
initRec(nodeFs)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class GitManager {
private val uiHelper = MyApp.appModule.uiHelper

private val locker = Mutex()
private var isRepoInitialized = false
var isRepoInitialized = false
private set
private var isLibInitialized = false

private suspend fun <T> safelyAccessLibGit2(f: suspend () -> T): Result<T> = locker.withLock {
Expand Down Expand Up @@ -95,23 +96,21 @@ class GitManager {
isRepoInitialized = true
}

private var actualCb: ((Int) -> Unit)? = null
private var actualCb: ((Int) -> Boolean)? = null

/**
* This function is called from native code
*/
@Keep
fun progressCb(progress: Int) {
if (actualCb != null) {
actualCb?.invoke(progress)
}
fun progressCb(progress: Int): Boolean {
return actualCb?.invoke(progress) != false
}

suspend fun cloneRepo(
repoPath: String,
repoUrl: String,
cred: Cred?,
progressCallback: (Int) -> Unit
progressCallback: (Int) -> Boolean
): Result<Unit> = safelyAccessLibGit2 {
Log.d(TAG, "clone repo: $repoPath, $repoUrl, $cred")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ sealed interface SyncState {
}
}

sealed class Progress {
data object Timestamps: Progress()

data class GeneratingDatabase(val path: String): Progress()
}

class StorageManager {


Expand Down Expand Up @@ -95,7 +101,7 @@ class StorageManager {
* The caller must ensure that all files has been committed
* to keep the database in sync with the remote repo
*/
private suspend fun updateDatabaseWithoutLocker(force: Boolean = false): Result<Unit> {
private suspend fun updateDatabaseWithoutLocker(force: Boolean = false, progressCb: ((Progress) -> Unit)? = null): Result<Unit> {

val fsCommit = gitManager.lastCommit()
val databaseCommit = prefs.databaseCommit.get()
Expand All @@ -109,9 +115,10 @@ class StorageManager {
val repoPath = prefs.repoPath()
Log.d(TAG, "repoPath = $repoPath")

progressCb?.invoke(Progress.Timestamps)
val timestamps = gitManager.getTimestamps().getOrThrow()

dao.clearAndInit(repoPath, timestamps)
dao.clearAndInit(repoPath, timestamps, progressCb)
prefs.databaseCommit.update(fsCommit)

return success(Unit)
Expand All @@ -120,8 +127,8 @@ class StorageManager {
/**
* See the documentation of [updateDatabaseWithoutLocker]
*/
suspend fun updateDatabase(force: Boolean = false): Result<Unit> = locker.withLock {
updateDatabaseWithoutLocker(force)
suspend fun updateDatabase(force: Boolean = false, progressCb: ((Progress) -> Unit)? = null): Result<Unit> = locker.withLock {
updateDatabaseWithoutLocker(force, progressCb)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ private fun SetupTitle(

@Composable
fun NextButton(
modifier: Modifier = Modifier,
text: String,
onClick: () -> Unit,
enabled: Boolean = true,
) {
Button(
modifier = Modifier
modifier = modifier
.fillMaxWidth(),
onClick = onClick,
enabled = enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ sealed interface RemoteDestination : Parcelable {
val url: String,
) : RemoteDestination

@Parcelize
data object Cloning : RemoteDestination
// @Parcelize
// data class LoadKeysFromDevice(
// val provider: Provider?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,12 @@ fun DrawerScreen(
missingDelimiterValue = ""
)

if (drawerState.isOpen) {

val scope = rememberCoroutineScope()
BackHandler(enabled = drawerState.isOpen) {
if (currentNoteFolderRelativePath.isEmpty()) {
val scope = rememberCoroutineScope()
BackHandler {
scope.launch { drawerState.close() }
}
scope.launch { drawerState.close() }
} else {
BackHandler {
vm.openFolder(getParent(currentNoteFolderRelativePath))
}
vm.openFolder(getParent(currentNoteFolderRelativePath))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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.viewmodel.InitState
import io.github.wiiznokes.gitnote.ui.viewmodel.InitState.AuthState

private const val TAG = "AuthorizeGitNoteScreen"

Expand All @@ -32,12 +31,12 @@ fun AuthorizeGitNoteScreen(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
onBackClick = onBackClick,
onBackClickEnabled = authState.isClickable()
onBackClickEnabled = !authState.isLoading()
) {

LaunchedEffect(authState) {
Log.d(TAG, "LaunchedEffect: $authState, hash=${vmHashCode}")
if (authState is AuthState.Success) {
if (authState is InitState.AuthentificationSuccess) {
onSuccess()
}
}
Expand All @@ -49,7 +48,7 @@ fun AuthorizeGitNoteScreen(
val intent = getLaunchOAuthScreenIntent()
ctx.startActivity(intent)
},
enabled = authState.isClickable() && authState != AuthState.Success
enabled = !authState.isLoading() && authState != InitState.AuthentificationSuccess
) {
if (!authState.isLoading()) {
Text(text = stringResource(R.string.authorize_gitnote))
Expand All @@ -66,7 +65,7 @@ private fun AuthorizeGitNoteScreenPreview() {

AuthorizeGitNoteScreen(
onBackClick = {},
authState = AuthState.Idle,
authState = InitState.Idle,
onSuccess = {},
getLaunchOAuthScreenIntent = {
Intent()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.github.wiiznokes.gitnote.ui.screen.setup.remote

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.wiiznokes.gitnote.ui.component.AppPage
import io.github.wiiznokes.gitnote.ui.viewmodel.InitState

private const val TAG = "CloningScreen"


@Composable
fun CloningScreen(
cloneState: InitState,
onCancel: () -> Unit,
) {
AppPage(
title = "Clone",
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
onBackClickEnabled = !cloneState.isLoading()
) {
Text(text = cloneState.message())

Spacer(Modifier.height(20.dp))

Button(
onClick = onCancel,
enabled = cloneState !is InitState.CalculatingTimestamps && cloneState !is InitState.GeneratingDatabase
) {
Text("Cancel")
}
}
}

@Preview
@Composable
private fun PickRepoScreenPreview() {

CloningScreen(
cloneState = InitState.Cloning(50),
onCancel = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fun GenerateNewKeysScreen(
url: String,
vm: SetupViewModelI,
generateSshKeys: () -> Pair<String, String>,
onClone: () -> Unit,
onSuccess: () -> Unit,
) {

Expand All @@ -61,7 +62,7 @@ fun GenerateNewKeysScreen(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
onBackClick = onBackClick,
onBackClickEnabled = cloneState.isClickable()
onBackClickEnabled = !cloneState.isLoading()
) {

val publicKey = rememberSaveable { mutableStateOf("") }
Expand Down Expand Up @@ -161,9 +162,7 @@ fun GenerateNewKeysScreen(
) {

SetupButton(
text = if (cloneState.isLoading()) {
cloneState.message()
} else stringResource(R.string.clone_repo),
text = stringResource(R.string.clone_repo),
onClick = {
vm.cloneRepo(
storageConfig = storageConfig,
Expand All @@ -174,8 +173,9 @@ fun GenerateNewKeysScreen(
),
onSuccess = onSuccess
)

onClone()
},
enabled = cloneState.isClickable()
)
}
}
Expand All @@ -187,13 +187,14 @@ fun GenerateNewKeysScreen(
private fun GenerateNewKeysScreenPreview() {
GenerateNewKeysScreen(
onBackClick = {},
cloneState = InitState.CloneState.Idle,
cloneState = InitState.Idle,
provider = GithubProvider(),
storageConfig = StorageConfiguration.App,
url = "url",
vm = SetupViewModelMock(),
generateSshKeys = { "aaaaaaaaaaaabbbbbbbbbbbbb" to "aaaaaaaaaaaabbbbbbbbbbbbb" },
onSuccess = {}
onSuccess = {},
onClone = {}
)

}
Loading
Loading