Skip to content

Commit

Permalink
Fix svg read/write + some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Oct 26, 2023
1 parent 39febc4 commit 07c404e
Show file tree
Hide file tree
Showing 22 changed files with 313 additions and 314 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package space.taran.arkmemo.data.repositories

import android.util.Log
import dev.arkbuilders.arklib.computeId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import space.taran.arkmemo.data.ResourceMeta
import space.taran.arkmemo.models.Content
import space.taran.arkmemo.models.GraphicNote
import space.taran.arkmemo.preferences.MemoPreferences
import space.taran.arkmemo.utils.SVG
import java.nio.file.Files
import java.nio.file.Path
import javax.inject.Inject
import kotlin.io.path.extension
import kotlin.io.path.fileSize
import kotlin.io.path.getLastModifiedTime
import kotlin.io.path.name
import kotlin.io.path.readText
import kotlin.io.path.createTempFile
import kotlin.io.path.deleteIfExists
import kotlin.io.path.moveTo

class GraphicNotesRepo @Inject constructor() {

private val iODispatcher = Dispatchers.IO
private val root = MemoPreferences.getNotesStorage()

suspend fun save(note: GraphicNote) = withContext(iODispatcher) {
write(note.svg!!)
}

suspend fun delete(note: GraphicNote) = withContext(iODispatcher) {
if (root != null && note.meta != null) {
val path = root.resolve(note.meta.name)
delete(path)
}
}

suspend fun read(): List<GraphicNote> = withContext(Dispatchers.IO) {
readStorage()
}

private fun write(svg: SVG) {
if (root != null) {
val tempPath = createTempFile()
svg.generate(tempPath)
val id = computeId(tempPath.fileSize(), tempPath)
val resourcePath = root.resolve("${id}.$GRAPHICAL_NOTE_EXT")
tempPath.moveTo(resourcePath)

Log.d("graphics-repo", "file renamed to $resourcePath successfully")
}
}

private fun delete(path: Path) {
path.deleteIfExists()
}

private suspend fun readStorage() = withContext(Dispatchers.IO) {
val notes = mutableListOf<GraphicNote>()
if (root != null) {
var i = 0
Files.list(root).forEach { path ->
if (path.extension == GRAPHICAL_NOTE_EXT) {
val svg = SVG.parse(path)
val size = path.fileSize()
val meta = ResourceMeta(
computeId(size, path),
path.fileName.name,
path.extension,
path.getLastModifiedTime(),
size
)
val note = GraphicNote(
content = Content("Note ${i++}", svg.pathData),
svg = svg,
meta = meta
)
notes.add(note)
}
}
}
notes
}

companion object {
private const val GRAPHICAL_NOTE_EXT = "note.svg"
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ 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
Expand All @@ -20,7 +17,6 @@ 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import kotlinx.parcelize.Parcelize
import space.taran.arkmemo.data.ResourceMeta

@Parcelize
data class GraphicalNote(
val svgData: String,
open class BaseNote(
val resourceContent: Content,
@IgnoredOnParcel
val meta: ResourceMeta? = null
) : Parcelable
var resourceMeta: ResourceMeta? = null
) : Parcelable

@Parcelize
data class Content(
val title: String,
val data: String
): Parcelable
16 changes: 16 additions & 0 deletions app/src/main/java/space/taran/arkmemo/models/GraphicNote.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package space.taran.arkmemo.models

import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import space.taran.arkmemo.data.ResourceMeta
import space.taran.arkmemo.utils.SVG

@Parcelize
data class GraphicNote(
val content: Content,
@IgnoredOnParcel
val svg: SVG? = null,
@IgnoredOnParcel
var meta: ResourceMeta? = null
) : BaseNote(content, meta), Parcelable
9 changes: 1 addition & 8 deletions app/src/main/java/space/taran/arkmemo/models/TextNote.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ data class TextNote (
val content: Content,
@IgnoredOnParcel
var meta: ResourceMeta? = null
): Parcelable
{
@Parcelize
data class Content(
val title: String,
val data: String
): Parcelable
}
): BaseNote(content, meta), Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -84,7 +85,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

}

if (MemoPreferences.getInstance(this).getPathString() == null) {
if (MemoPreferences.getInstance(this).getPath() == null) {
FilePicker.show(this, supportFragmentManager)

supportFragmentManager.onArkPathPicked(this) {
Expand Down Expand Up @@ -127,6 +128,12 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

fun showProgressBar(show: Boolean) {
binding.progressBar.isVisible = show
if (!show) {
Toast.makeText(this, getString(R.string.ark_memo_note_saved),
Toast.LENGTH_SHORT)
.show()
onBackPressedDispatcher.onBackPressed()
}
}
companion object{
private const val CURRENT_FRAGMENT_TAG = "current fragment tag"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import androidx.recyclerview.widget.RecyclerView
import by.kirich1409.viewbindingdelegate.viewBinding
import space.taran.arkmemo.R
import space.taran.arkmemo.databinding.TextNoteBinding
import space.taran.arkmemo.models.BaseNote
import space.taran.arkmemo.models.GraphicNote
import space.taran.arkmemo.models.TextNote
import space.taran.arkmemo.ui.activities.MainActivity
import space.taran.arkmemo.ui.activities.replaceFragment
import space.taran.arkmemo.ui.dialogs.NoteDeleteDialogFragment
import space.taran.arkmemo.ui.fragments.EditGraphicNotesFragment
import space.taran.arkmemo.ui.fragments.EditTextNotesFragment

class TextNotesListAdapter(private val notes: List<TextNote>): RecyclerView.Adapter<TextNotesListAdapter.NoteViewHolder>() {
class NotesListAdapter(private val notes: List<BaseNote>): RecyclerView.Adapter<NotesListAdapter.NoteViewHolder>() {

private var activity: MainActivity? = null
private var fragmentManager: FragmentManager? = null
Expand All @@ -29,13 +32,13 @@ class TextNotesListAdapter(private val notes: List<TextNote>): RecyclerView.Adap
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.text_note, parent, false)
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.note, parent, false)
return NoteViewHolder(itemView)
}

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

override fun getItemCount() = notes.size
Expand All @@ -49,8 +52,11 @@ class TextNotesListAdapter(private val notes: List<TextNote>): RecyclerView.Adap
val date = binding.noteDate

private val clickNoteToEditListener = View.OnClickListener {
val selectedNote = notes[bindingAdapterPosition]
activity?.fragment = EditTextNotesFragment.newInstance(selectedNote)
when (val selectedNote = notes[bindingAdapterPosition]) {
is TextNote -> activity?.fragment = EditTextNotesFragment.newInstance(selectedNote)
is GraphicNote ->
activity?.fragment = EditGraphicNotesFragment.newInstance(selectedNote)
}
activity?.replaceFragment(activity?.fragment!!, EditTextNotesFragment.TAG)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import android.widget.Toast
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.models.BaseNote
import space.taran.arkmemo.ui.fragments.deleteNote

class NoteDeleteDialogFragment: DialogFragment() {

private var note: TextNote? = null
private var note: BaseNote? = null

fun setNoteToBeDeleted(note: TextNote): NoteDeleteDialogFragment{
fun setNoteToBeDeleted(note: BaseNote): NoteDeleteDialogFragment{
this.note = note
return this
}
Expand Down
Loading

0 comments on commit 07c404e

Please sign in to comment.