Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Oct 24, 2023
1 parent 6053864 commit b374c36
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 172 deletions.
162 changes: 49 additions & 113 deletions app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
Expand All @@ -24,15 +25,13 @@ 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 _textNotes = MutableStateFlow(listOf<TextNote>())
val textNotes: StateFlow<List<TextNote>> = _textNotes

private val iODispatcher = Dispatchers.IO

private lateinit var propertiesStorage: PropertiesStorage
Expand All @@ -47,77 +46,61 @@ class TextNotesRepo @Inject constructor() {
}

suspend fun save(note: TextNote) {
add(note)
write(note)
}

suspend fun delete(note: TextNote) = withContext(iODispatcher) {
val path = root.resolve("${note.meta?.name}")
remove(note)
delete(path)
propertiesStorage.remove(note.meta?.id!!)
propertiesStorage.persist()
Log.d("text-repo", "${note.meta?.name!!} has been deleted")
}

suspend fun read() = withContext(Dispatchers.IO) {
suspend fun read(): List<TextNote> = withContext(Dispatchers.IO) {
val notes = mutableListOf<TextNote>()
Files.list(root).forEach { path ->
if (path.fileName.extension == NOTE_EXT) {
try {
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)
} catch (e: Exception) {
e.printStackTrace()
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")
_textNotes.value = notes
notes
}


private suspend fun write(note: TextNote) {
withContext(Dispatchers.IO) {
val path = root.resolve("${DUMMY_FILENAME}.${NOTE_EXT}")
if (path.exists()) return@withContext
try {
val lines = note.content.data.split(NEWLINE)
path.writeLines(lines)
val size = path.fileSize()
val id = computeId(size, path)
Log.d("text-repo", "initial resource name ${path.name}")
persistNoteProperties(resourceId = id, noteTitle = note.content.title)

val newPath = root.resolve("$id.$NOTE_EXT")
if (!newPath.exists()) {
renameResourceWithNewResourceMeta(
note = note,
path = path,
newPath = newPath,
resourceId = id,
size = size
)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
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) {
Expand All @@ -127,76 +110,29 @@ class TextNotesRepo @Inject constructor() {
persist()
}
}

private fun renameResourceWithNewResourceMeta(
note: TextNote,
path: Path,
newPath: Path,
tempPath: Path,
resourcePath: Path,
resourceId: ResourceId,
size: Long
) {
if (path.toFile().renameTo(newPath.toFile())) {
note.meta = ResourceMeta(
id = resourceId,
name = newPath.fileName.name,
extension = newPath.extension,
modified = newPath.getLastModifiedTime(),
size = size
)
Log.d("notes-repo", "resource renamed to ${newPath.name} successfully")
} else delete(path)
}
val path = root.resolve("${DUMMY_FILENAME}.${NOTE_EXT}")
if (!path.exists()) {
try {
val lines = note.content.data.split(NEWLINE)
path.writeLines(lines)
val size = path.fileSize()
val id = computeId(size, path)
val properties = Properties(setOf(note.content.title), setOf())

Log.d("text-repo", "initial resource name ${path.name}")

propertiesStorage.setProperties(id, properties)
propertiesStorage.persist()

val newPath = root.resolve("$id.$NOTE_EXT")
if (!newPath.exists()) {
if (path.toFile().renameTo(newPath.toFile())) {
note.meta = ResourceMeta(
id,
newPath.fileName.name,
newPath.extension,
newPath.getLastModifiedTime(),
size
)
Log.d("notes-repo", "resource renamed to ${newPath.name} successfully")
} else delete(path)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
this.coroutineContext.job
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 fun add(note: TextNote) {
val notes = textNotes.value.toMutableList()
notes.add(note)
_textNotes.value = notes
}

private fun remove(note: TextNote) {
val notes = textNotes.value.toMutableList()
notes.remove(note)
_textNotes.value = notes
}
}

private const val NOTE_EXT = "note"
private const val DUMMY_FILENAME = "Note"
private const val NEWLINE = "\n"

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TextNotesListAdapter(private val notes: List<TextNote>): RecyclerView.Adap

override fun onBindViewHolder(holder: NoteViewHolder, position: Int) {
holder.title.text = notes[position].content.title
holder.date.text = notes[position].meta?.modified.toString()
holder.date.text = notes[position].meta?.modified?.toString() ?: "Just now"
}

override fun getItemCount() = notes.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import space.taran.arkmemo.R
import space.taran.arkmemo.models.TextNote
import space.taran.arkmemo.ui.fragments.deleteTextNote
import space.taran.arkmemo.ui.fragments.deleteNote

class NoteDeleteDialogFragment: DialogFragment() {

Expand All @@ -27,7 +27,7 @@ class NoteDeleteDialogFragment: DialogFragment() {
}
.setPositiveButton(R.string.ark_memo_ok){ dialog, _ ->
if(note != null) {
parentFragment?.deleteTextNote(note!!)
parentFragment?.deleteNote(note!!)
Toast.makeText(
requireContext(), getString(R.string.note_deleted),
Toast.LENGTH_SHORT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.fragment.app.activityViewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import space.taran.arkmemo.R
import space.taran.arkmemo.data.viewmodels.EditTextNotesViewModel
import space.taran.arkmemo.ui.viewmodels.NotesViewModel
import space.taran.arkmemo.databinding.FragmentEditTextNotesBinding
import space.taran.arkmemo.models.TextNote
import space.taran.arkmemo.ui.activities.MainActivity
Expand All @@ -22,7 +22,7 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) {
requireActivity() as MainActivity
}

private val editViewModel: EditTextNotesViewModel by activityViewModels()
private val notesViewModel: NotesViewModel by activityViewModels()

private val binding by viewBinding(FragmentEditTextNotesBinding::bind)

Expand All @@ -31,7 +31,7 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
editViewModel.init()
notesViewModel.init()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -84,7 +84,7 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) {
editNote.setText(noteStr)

saveNoteButton.setOnClickListener {
editViewModel.onSaveClick(note) { show ->
notesViewModel.onSaveClick(note) { show ->
activity.showProgressBar(show)
if (!show) {
Toast.makeText(requireContext(), getString(R.string.ark_memo_note_saved),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.widget.Button
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
Expand All @@ -17,7 +16,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import space.taran.arkmemo.R
import space.taran.arkmemo.data.viewmodels.TextNotesViewModel
import space.taran.arkmemo.ui.viewmodels.NotesViewModel
import space.taran.arkmemo.databinding.FragmentTextNotesBinding
import space.taran.arkmemo.models.TextNote
import space.taran.arkmemo.ui.activities.MainActivity
Expand All @@ -34,7 +33,7 @@ class TextNotesFragment: Fragment(R.layout.fragment_text_notes) {
requireActivity() as MainActivity
}

private val textNotesViewModel: TextNotesViewModel by activityViewModels()
private val textNotesViewModel: NotesViewModel by activityViewModels()

private lateinit var newNoteButton: FloatingActionButton
private lateinit var pasteNoteButton: Button
Expand Down Expand Up @@ -95,7 +94,7 @@ class TextNotesFragment: Fragment(R.layout.fragment_text_notes) {
}
}

fun Fragment.deleteTextNote(note: TextNote){
val viewModel: TextNotesViewModel by viewModels()
fun Fragment.deleteNote(note: TextNote){
val viewModel: NotesViewModel by activityViewModels()
viewModel.onDelete(note)
}
Loading

0 comments on commit b374c36

Please sign in to comment.