Skip to content

Commit

Permalink
feat: delete folder (#42)
Browse files Browse the repository at this point in the history
* feat: delete folder

* editParams

* clean up code

* selectedNotes: take Note instead of path
  • Loading branch information
wiiznokes authored May 20, 2024
1 parent 99ca89e commit 3645fbb
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 154 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

_Supported Android versions: 11 to 14_

Android note which integrate Git. You can use this app with other desktop editors.
Android note app which integrate Git. You can use this app with other desktop editors.

## Why

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import dev.olshevski.navigation.reimagined.rememberNavController
import io.github.wiiznokes.gitnote.MyApp.Companion.appModule
import io.github.wiiznokes.gitnote.ui.destination.AppDestination
import io.github.wiiznokes.gitnote.ui.destination.Destination
import io.github.wiiznokes.gitnote.ui.destination.EditParams
import io.github.wiiznokes.gitnote.ui.destination.InitDestination
import io.github.wiiznokes.gitnote.ui.screen.app.AppScreen
import io.github.wiiznokes.gitnote.ui.screen.init.InitScreen
Expand Down Expand Up @@ -59,7 +60,7 @@ class MainActivity : ComponentActivity() {
if (isEditUnsaved()) {
Log.d(TAG, "launch as EDIT_IS_UNSAVED")
Destination.App(
AppDestination.EditSaved
AppDestination.Edit(EditParams.Saved)
)
} else {
Destination.App(
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/io/github/wiiznokes/gitnote/data/room/Dao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,32 @@ interface RepoDatabaseDao {
@Upsert
suspend fun insertNoteFolder(noteFolder: NoteFolder)

/**
* Delete all notes inside the note folder, and the note folder
*/
suspend fun deleteNoteFolder(noteFolder: NoteFolder) {
internalDeleteNotesIn(noteFolder.relativePath)
internalDeleteNoteFolder(noteFolder)
}

/**
* Private
*/
@Query("DELETE FROM Notes WHERE relativePath LIKE :relativePath || '%'")
suspend fun internalDeleteNotesIn(relativePath: String)

// todo
// suspend fun removeNoteFolder(noteFolder: NoteFolder)
/**
* Private
*/
@Delete
suspend fun internalDeleteNoteFolder(noteFolder: NoteFolder)

@Upsert
suspend fun insertNote(note: Note)

@Delete
suspend fun removeNote(note: Note)

@Query("DELETE FROM Notes WHERE relativePath = :relativePath")
suspend fun removeNote(relativePath: String): Int


@Query("DELETE FROM NoteFolders")
fun removeAllNoteFolder()

Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/io/github/wiiznokes/gitnote/data/room/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.github.wiiznokes.gitnote.data.room
import android.os.Parcelable
import androidx.room.Entity
import io.github.wiiznokes.gitnote.BuildConfig
import io.github.wiiznokes.gitnote.data.platform.NodeFs
import io.github.wiiznokes.gitnote.data.removeFirstAndLastSlash
import io.github.wiiznokes.gitnote.data.requireNotEndOrStartWithSlash
import io.github.wiiznokes.gitnote.ui.model.FileExtension
Expand Down Expand Up @@ -50,6 +51,17 @@ data class NoteFolder(
if (relativePath == "") return null
return relativePath.substringBeforeLast("/", missingDelimiterValue = "")
}

fun toFolderFs(rootPath: String): NodeFs.Folder {
return NodeFs.Folder.fromPath(rootPath, relativePath)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
return id == (other as Note).id
}
override fun hashCode(): Int = id
}

@Entity(
Expand Down Expand Up @@ -113,4 +125,15 @@ data class Note(
requireNotEndOrStartWithSlash(nameWithoutExtension())
}
}

fun toFileFs(rootPath: String): NodeFs.File {
return NodeFs.File.fromPath(rootPath, relativePath)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
return id == (other as Note).id
}
override fun hashCode(): Int = id
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.util.Log
import io.github.wiiznokes.gitnote.MyApp
import io.github.wiiznokes.gitnote.R
import io.github.wiiznokes.gitnote.data.AppPreferences
import io.github.wiiznokes.gitnote.data.platform.NodeFs
import io.github.wiiznokes.gitnote.data.room.Note
import io.github.wiiznokes.gitnote.data.room.NoteFolder
import io.github.wiiznokes.gitnote.data.room.RepoDatabase
Expand Down Expand Up @@ -113,7 +112,7 @@ class StorageManager {
dao.insertNote(new)

val rootPath = prefs.repoPath()
val previousFile = NodeFs.File.fromPath(rootPath, previous.relativePath)
val previousFile = previous.toFileFs(rootPath)

previousFile.delete().onFailure {
val message =
Expand All @@ -122,8 +121,7 @@ class StorageManager {
uiHelper.makeToast(message)
}

val newFile = NodeFs.File.fromPath(rootPath, new.relativePath)

val newFile = new.toFileFs(rootPath)
newFile.create().onFailure {
val message = uiHelper.getString(R.string.error_create_file, it.message)
Log.e(TAG, message)
Expand All @@ -150,8 +148,7 @@ class StorageManager {
update {
dao.insertNote(note)

val rootPath = prefs.repoPath()
val file = NodeFs.File.fromPath(rootPath, note.relativePath)
val file = note.toFileFs(prefs.repoPath())

file.create().onFailure {
val message = uiHelper.getString(R.string.error_create_file, it.message)
Expand All @@ -175,8 +172,7 @@ class StorageManager {
update {
dao.removeNote(note)

val rootPath = prefs.repoPath()
val file = NodeFs.File.fromPath(rootPath, note.relativePath)
val file = note.toFileFs(prefs.repoPath())
file.delete().onFailure {
val message = uiHelper.getString(R.string.error_delete_file, file.path, it.message)
Log.e(TAG, message)
Expand All @@ -186,24 +182,20 @@ class StorageManager {
}
}

suspend fun deleteNotes(noteRelativePaths: List<String>): Result<Unit> = locker.withLock {
Log.d(TAG, "deleteNotes: ${noteRelativePaths.size}")
suspend fun deleteNotes(notes: List<Note>): Result<Unit> = locker.withLock {
Log.d(TAG, "deleteNotes: ${notes.size}")

update {

// optimization because we only see the db state on screen
noteRelativePaths.forEach { noteRelativePath ->
val removedNoteCount = dao.removeNote(noteRelativePath)
if (removedNoteCount != 1) {
Log.e(TAG, "removed note count was != 1 from the doa: $removedNoteCount")
}
notes.forEach { note ->
dao.removeNote(note)
}

val rootPath = prefs.repoPath()
noteRelativePaths.forEach { noteRelativePath ->
val repoPath = prefs.repoPath()
notes.forEach { note ->

Log.d(TAG, "deleting $noteRelativePath")
val file = NodeFs.File.fromPath(rootPath, noteRelativePath)
Log.d(TAG, "deleting $note")
val file = note.toFileFs(repoPath)

file.delete().onFailure {
val message =
Expand All @@ -222,9 +214,7 @@ class StorageManager {
update {
dao.insertNoteFolder(noteFolder)

val rootPath = prefs.repoPath()
val folder = NodeFs.Folder.fromPath(rootPath, noteFolder.relativePath)

val folder = noteFolder.toFolderFs(prefs.repoPath())
folder.create().onFailure {
val message = uiHelper.getString(R.string.error_create_folder, it.message)
Log.e(TAG, message)
Expand All @@ -235,6 +225,23 @@ class StorageManager {
}
}

suspend fun deleteNoteFolder(noteFolder: NoteFolder): Result<Unit> = locker.withLock {
Log.d(TAG, "deleteNoteFolder: $noteFolder")

update {
dao.deleteNoteFolder(noteFolder)

val folder = noteFolder.toFolderFs(prefs.repoPath())
folder.delete().onFailure {
val msg = uiHelper.getString(R.string.error_delete_folder, it.message)
Log.e(TAG, msg)
uiHelper.makeToast(msg)
}

success(Unit)
}
}

suspend fun closeRepo() = locker.withLock {
gitManager.closeRepo()
dao.clearDatabase()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.github.wiiznokes.gitnote.ui.component

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
Expand All @@ -14,23 +10,20 @@ import androidx.compose.material3.Text
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.core.util.TypedValueCompat.pxToDp


private val TAG = "CustomDropDown"

data class CustomDropDownModel(
val text: String,
val onClick: () -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ sealed interface AppDestination : Parcelable {
data object Grid : AppDestination

@Parcelize
data class Edit(val note: Note, val editType: EditType) : AppDestination

@Parcelize
data object EditSaved : AppDestination
data class Edit(val params: EditParams) : AppDestination

@Parcelize
data class Settings(val settingsDestination: SettingsDestination) : AppDestination

}

@Parcelize
sealed class EditParams : Parcelable {
data object Saved : EditParams()

data class Idle(
val note: Note,
val editType: EditType
) : EditParams()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum class NewRepoSource {

fun getExplorerTitle(): String {
val context = MyApp.appModule.context

return when (this) {
Create -> context.getString(R.string.create_repo_explorer)
Open -> context.getString(R.string.open_repo_explorer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dev.olshevski.navigation.reimagined.navigate
import dev.olshevski.navigation.reimagined.pop
import dev.olshevski.navigation.reimagined.rememberNavController
import io.github.wiiznokes.gitnote.ui.destination.AppDestination
import io.github.wiiznokes.gitnote.ui.destination.EditParams
import io.github.wiiznokes.gitnote.ui.destination.SettingsDestination
import io.github.wiiznokes.gitnote.ui.screen.app.grid.GridScreen
import io.github.wiiznokes.gitnote.ui.screen.settings.SettingsScreen
Expand Down Expand Up @@ -47,17 +48,20 @@ fun AppScreen(
)
},
onEditClick = { note, editType ->
navController.navigate(AppDestination.Edit(note, editType))
navController.navigate(AppDestination.Edit(EditParams.Idle(note, editType)))
},
onStorageFailure = onStorageFailure
)
}

is AppDestination.Edit -> EditScreen(
initialNote = it.note,
initialEditType = it.editType,
editParams = it.params,
onFinished = {
navController.pop()

if (it.params == EditParams.Saved) {
navController.navigate(AppDestination.Grid)
}
}
)

Expand All @@ -66,15 +70,6 @@ fun AppScreen(
destination = it.settingsDestination,
onStorageFailure = onStorageFailure
)

AppDestination.EditSaved -> EditScreen(
initialNote = null,
initialEditType = null,
onFinished = {
navController.pop()
navController.navigate(AppDestination.Grid)
}
)
}
}
}
Expand All @@ -98,7 +93,6 @@ private object AppNavTransitionSpec : NavTransitionSpec<AppDestination> {
}

is AppDestination.Settings -> slide(backWard = true)
AppDestination.EditSaved -> crossFade()
}
}
}
Expand Down
Loading

0 comments on commit 3645fbb

Please sign in to comment.