Skip to content

Commit

Permalink
Feat app storage (#35)
Browse files Browse the repository at this point in the history
* add basic type

* bottom sheet ui

* some fixes

* working solution

* fix git push and pull without remove url

* ui fixes

* fix

* add localization

* rename to git_wrapper

* french translation
  • Loading branch information
wiiznokes authored May 19, 2024

Verified

This commit was signed with the committer’s verified signature.
zadjadr Zadjad Rezai
1 parent 94f7f51 commit f349aa4
Showing 20 changed files with 554 additions and 317 deletions.
4 changes: 2 additions & 2 deletions app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@


cmake_minimum_required(VERSION 3.22.1)
project("gitnote")
project("git_wrapper")


set(JNI_LIBS ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
@@ -18,7 +18,7 @@ add_library(${CMAKE_PROJECT_NAME} SHARED
merge.cpp
remote.cpp
CallbackHandler.cpp
gitnote.cpp)
git_wrapper.cpp)

target_link_libraries(${CMAKE_PROJECT_NAME}
${JNI_LIBS}/libgit2.so
Original file line number Diff line number Diff line change
@@ -80,6 +80,7 @@ Java_io_github_wiiznokes_gitnote_manager_GitManagerKt_lastCommitLib(JNIEnv *env,

err = git_reference_name_to_id(&sha_last_commit, repo, "HEAD");

// XXX: can fail with error 4 if there are no commit yet in the repo
CHECK_LG2_RETURN(err, "git_reference_name_to_id", NULL);

// Convert git_oid to string
37 changes: 14 additions & 23 deletions app/src/main/java/io/github/wiiznokes/gitnote/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@ import dev.olshevski.navigation.reimagined.popAll
import dev.olshevski.navigation.reimagined.popUpTo
import dev.olshevski.navigation.reimagined.rememberNavController
import io.github.wiiznokes.gitnote.MyApp.Companion.appModule
import io.github.wiiznokes.gitnote.helper.StoragePermissionHelper
import io.github.wiiznokes.gitnote.ui.destination.AppDestination
import io.github.wiiznokes.gitnote.ui.destination.Destination
import io.github.wiiznokes.gitnote.ui.destination.InitDestination
@@ -56,23 +55,19 @@ class MainActivity : ComponentActivity() {


val startDestination: Destination = remember {
if (!StoragePermissionHelper.isPermissionGranted()) {
Destination.Init(InitDestination.LocalStoragePermission)
} else {
if (runBlocking { vm.tryInit() }) {
if (isEditUnsaved()) {
Log.d(TAG, "launch as EDIT_IS_UNSAVED")
Destination.App(
AppDestination.EditSaved
)
} else {
Destination.App(
AppDestination.Grid
//AppDestination.Settings(SettingsDestination.Main)
)
}
} else Destination.Init(InitDestination.Main)
}
if (runBlocking { vm.tryInit() }) {
if (isEditUnsaved()) {
Log.d(TAG, "launch as EDIT_IS_UNSAVED")
Destination.App(
AppDestination.EditSaved
)
} else {
Destination.App(
AppDestination.Grid
//AppDestination.Settings(SettingsDestination.Main)
)
}
} else Destination.Init(InitDestination.Main)
}


@@ -104,11 +99,7 @@ class MainActivity : ComponentActivity() {
appDestination = destination.appDestination,
onStorageFailure = {
navController.popAll()
if (!StoragePermissionHelper.isPermissionGranted()) {
navController.navigate(Destination.Init(InitDestination.LocalStoragePermission))
} else {
navController.navigate(Destination.Init(InitDestination.Main))
}
navController.navigate(Destination.Init(InitDestination.Main))
}
)
}
100 changes: 80 additions & 20 deletions app/src/main/java/io/github/wiiznokes/gitnote/data/AppPreferences.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,77 @@
package io.github.wiiznokes.gitnote.data

import android.content.Context
import android.os.Parcelable
import io.github.wiiznokes.gitnote.MyApp
import io.github.wiiznokes.gitnote.manager.PreferencesManager
import io.github.wiiznokes.gitnote.ui.model.GitCreed
import io.github.wiiznokes.gitnote.ui.model.NoteMinWidth
import io.github.wiiznokes.gitnote.ui.model.Provider
import io.github.wiiznokes.gitnote.ui.model.SortOrder
import io.github.wiiznokes.gitnote.ui.model.SortType
import io.github.wiiznokes.gitnote.ui.theme.Theme
import kotlinx.coroutines.runBlocking
import kotlinx.parcelize.Parcelize
import kotlin.io.path.pathString

class AppPreferences(
context: Context
) : PreferencesManager(context, "settings") {

companion object {
val appStorageRepoPath =
MyApp.appModule.context.filesDir.toPath().resolve("repo").pathString
const val DEFAULT_USERNAME = "gitnote"
}

val dynamicColor = booleanPreference("dynamicColor", true)
val theme = enumPreference("theme", Theme.SYSTEM)


val isRepoInitialize = booleanPreference("isRepoInitialize", false)
val databaseCommit = stringPreference("")

val repoPath = stringPreference("repoPath")
private val repoPath = stringPreference("repoPath")

suspend fun repoPath(): String {
return when (repoState.get()) {
RepoState.NoRepo -> throw Exception("calling repoPath function with no repo initialized")
RepoState.AppStorage -> appStorageRepoPath
RepoState.DeviceStorage -> repoPath.get()
}
}

fun repoPathBlocking(): String = runBlocking { repoPath() }


fun repoPathSafely(): String {
return try {
repoPathBlocking()
} catch (e: Exception) {
""
}
}

val remoteUrl = stringPreference("remoteUrl", "")
val userName = stringPreference("userName", "")
val password = stringPreference(
"password",
""
)
val provider = enumPreference("provider", Provider.GitHub)

suspend fun gitCreed(): GitCreed? {
val userName = this.userName.get()
val password = this.password.get()

return if (userName.isEmpty() or password.isEmpty()) {
null
} else {
GitCreed(
userName = userName,
password = password
)
}
}

val provider = enumPreference("provider", Provider.GitHub)

val sortType = enumPreference("sortType", SortType.Modification)

@@ -50,30 +94,46 @@ class AppPreferences(
)
)

suspend fun initRepo(repoPath: String) {
isRepoInitialize.update(true)
this.repoPath.update(repoPath)

val repoState = enumPreference("repoState", RepoState.NoRepo)

suspend fun initRepo(repoState: NewRepoState) {
when (repoState) {
NewRepoState.AppStorage -> {
this.repoState.update(RepoState.AppStorage)
}

is NewRepoState.DeviceStorage -> {
this.repoState.update(RepoState.DeviceStorage)
this.repoPath.update(repoState.path)
}
}
lastOpenedFolder.update("")
}

suspend fun closeRepo() {
isRepoInitialize.update(false)
repoState.update(RepoState.NoRepo)
databaseCommit.update("")
}

suspend fun gitCreed(): GitCreed? {
val userName = this.userName.get()
val password = this.password.get()
}

return if (userName.isEmpty() or password.isEmpty()) {
null
} else {
GitCreed(
userName = userName,
password = password
)
}
}

enum class RepoState {
NoRepo,
AppStorage,
DeviceStorage
}

@Parcelize
sealed class NewRepoState : Parcelable {
data object AppStorage : NewRepoState()
class DeviceStorage(val path: String) : NewRepoState()

fun repoPath(): String {
return when (this) {
AppStorage -> AppPreferences.appStorageRepoPath
is DeviceStorage -> this.path
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.wiiznokes.gitnote.data.platform

import android.os.Environment
import io.github.wiiznokes.gitnote.MyApp
import io.github.wiiznokes.gitnote.R
import io.github.wiiznokes.gitnote.data.removeFirstAndLastSlash
import io.github.wiiznokes.gitnote.ui.model.FileExtension
import io.github.wiiznokes.gitnote.util.toResult
@@ -11,6 +13,7 @@ import java.nio.file.attribute.FileTime
import kotlin.Result.Companion.failure
import kotlin.Result.Companion.success
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createDirectories
import kotlin.io.path.createDirectory
import kotlin.io.path.createFile
import kotlin.io.path.deleteExisting
@@ -140,11 +143,11 @@ sealed class NodeFs(

try {
if (!pathFs.isDirectory()) {
return failure(Exception("The path is not a directory"))
return failure(Exception(MyApp.appModule.context.getString(R.string.error_path_not_directory)))
}

if (pathFs.listDirectoryEntries().isNotEmpty()) {
return failure(Exception("The path is not empty"))
return failure(Exception(MyApp.appModule.context.getString(R.string.error_path_not_empty)))
}

} catch (e: Exception) {
@@ -185,7 +188,7 @@ sealed class NodeFs(

override fun create(): Result<Unit> {
return toResult {
pathFs.createDirectory()
pathFs.createDirectories()
}
}

Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ class GitManager {

init {
Log.d(TAG, "init")
System.loadLibrary("gitnote")
System.loadLibrary("git_wrapper")
}
}

Loading

0 comments on commit f349aa4

Please sign in to comment.