-
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.
- Loading branch information
Showing
22 changed files
with
313 additions
and
314 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
app/src/main/java/space/taran/arkmemo/data/repositories/GraphicNotesRepo.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,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" | ||
} | ||
} |
98 changes: 0 additions & 98 deletions
98
app/src/main/java/space/taran/arkmemo/data/repositories/GraphicalNotesRepo.kt
This file was deleted.
Oops, something went wrong.
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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/space/taran/arkmemo/models/GraphicNote.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,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 |
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
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
Oops, something went wrong.