-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Separating user meta-data * Fix duplicate class * Fix duplicate class * Fix duplicate class * Light theme * Fix saving note * Update filepicker dependency * Package prefixes are fixed * Move constants * Update arklib-android * Update app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt Co-authored-by: Hieu Vu <[email protected]> * Update app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt Co-authored-by: Hieu Vu <[email protected]> * Minor fixes * Fix NotesViewModel --------- Co-authored-by: Kirill Taran <[email protected]> Co-authored-by: Hieu Vu <[email protected]>
- Loading branch information
1 parent
39c89bc
commit 1c94c60
Showing
25 changed files
with
354 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package space.taran.arkmemo.data.repositories | ||
|
||
import android.util.Log | ||
import dev.arkbuilders.arklib.ResourceId | ||
import dev.arkbuilders.arklib.computeId | ||
import dev.arkbuilders.arklib.data.index.RootIndex | ||
import dev.arkbuilders.arklib.user.properties.Properties | ||
import dev.arkbuilders.arklib.user.properties.PropertiesStorage | ||
import dev.arkbuilders.arklib.user.properties.PropertiesStorageRepo | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.job | ||
import kotlinx.coroutines.withContext | ||
import space.taran.arkmemo.data.ResourceMeta | ||
import space.taran.arkmemo.models.TextNote | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
import kotlin.io.path.deleteIfExists | ||
import kotlin.io.path.exists | ||
import kotlin.io.path.extension | ||
import kotlin.io.path.fileSize | ||
import kotlin.io.path.forEachLine | ||
import kotlin.io.path.getLastModifiedTime | ||
import kotlin.io.path.moveTo | ||
import kotlin.io.path.name | ||
import kotlin.io.path.writeLines | ||
|
||
@Singleton | ||
class TextNotesRepo @Inject constructor() { | ||
|
||
private val iODispatcher = Dispatchers.IO | ||
|
||
private lateinit var propertiesStorage: PropertiesStorage | ||
private lateinit var propertiesStorageRepo: PropertiesStorageRepo | ||
|
||
private lateinit var root: Path | ||
|
||
suspend fun init(root: Path, scope: CoroutineScope) { | ||
this.root = root | ||
propertiesStorageRepo = PropertiesStorageRepo(scope) | ||
propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root)) | ||
} | ||
|
||
suspend fun save(note: TextNote) { | ||
write(note) | ||
} | ||
|
||
suspend fun delete(note: TextNote) = withContext(iODispatcher) { | ||
val path = root.resolve("${note.meta?.name}") | ||
delete(path) | ||
propertiesStorage.remove(note.meta?.id!!) | ||
propertiesStorage.persist() | ||
Log.d("text-repo", "${note.meta?.name!!} has been deleted") | ||
} | ||
|
||
suspend fun read(): List<TextNote> = withContext(Dispatchers.IO) { | ||
val notes = mutableListOf<TextNote>() | ||
Files.list(root).forEach { path -> | ||
if (path.fileName.extension == NOTE_EXT) { | ||
val data = StringBuilder() | ||
path.forEachLine { | ||
data.appendLine(it) | ||
} | ||
val size = path.fileSize() | ||
val id = computeId(size, path) | ||
val meta = ResourceMeta( | ||
id, | ||
path.fileName.name, | ||
path.extension, | ||
path.getLastModifiedTime(), | ||
size | ||
) | ||
val titles = propertiesStorage.getProperties(id).titles | ||
val content = TextNote.Content(titles.elementAt(0), data.toString()) | ||
val note = TextNote(content, meta) | ||
notes.add(note) | ||
} | ||
} | ||
Log.d("text-repo", "${notes.size} text note resources found") | ||
notes | ||
} | ||
|
||
private suspend fun write(note: TextNote) = withContext(Dispatchers.IO) { | ||
val tempPath = kotlin.io.path.createTempFile() | ||
val lines = note.content.data.split('\n') | ||
tempPath.writeLines(lines) | ||
val size = tempPath.fileSize() | ||
val id = computeId(size, tempPath) | ||
Log.d("text-repo", "initial resource name ${tempPath.name}") | ||
persistNoteProperties(resourceId = id, noteTitle = note.content.title) | ||
|
||
val resourcePath = root.resolve("$id.$NOTE_EXT") | ||
renameResourceWithNewResourceMeta( | ||
note = note, | ||
tempPath = tempPath, | ||
resourcePath = resourcePath, | ||
resourceId = id, | ||
size = size | ||
) | ||
} | ||
|
||
private suspend fun persistNoteProperties(resourceId: ResourceId, noteTitle: String) { | ||
with(propertiesStorage) { | ||
val properties = Properties(setOf(noteTitle), setOf()) | ||
setProperties(resourceId, properties) | ||
persist() | ||
} | ||
} | ||
|
||
private fun renameResourceWithNewResourceMeta( | ||
note: TextNote, | ||
tempPath: Path, | ||
resourcePath: Path, | ||
resourceId: ResourceId, | ||
size: Long | ||
) { | ||
tempPath.moveTo(resourcePath) | ||
note.meta = ResourceMeta( | ||
id = resourceId, | ||
name = resourcePath.fileName.name, | ||
extension = resourcePath.extension, | ||
modified = resourcePath.getLastModifiedTime(), | ||
size = size | ||
) | ||
Log.d("notes-repo", "resource renamed to ${resourcePath.name} successfully") | ||
} | ||
|
||
private fun delete(path: Path) { | ||
path.deleteIfExists() | ||
} | ||
} | ||
|
||
private const val NOTE_EXT = "note" | ||
|
143 changes: 0 additions & 143 deletions
143
app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepository.kt
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
app/src/main/java/space/taran/arkmemo/data/viewmodels/EditTextNotesViewModel.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.