From ae037d1c40d59cf68c992c40abbe677159cdf1ae Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:44:21 +0700 Subject: [PATCH 1/8] Extract methods to interfaces and provide dependencies --- .../data/repositories/TextNotesRepo.kt | 138 +----------------- .../data/repositories/TextNotesRepoImpl.kt | 138 ++++++++++++++++++ .../taran/arkmemo/di/PreferencesModule.kt | 4 + .../taran/arkmemo/di/RepositoryModule.kt | 4 + .../arkmemo/preferences/MemoPreferences.kt | 45 +----- .../preferences/MemoPreferencesImpl.kt | 47 ++++++ 6 files changed, 196 insertions(+), 180 deletions(-) create mode 100644 app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt create mode 100644 app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt create mode 100644 app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt create mode 100644 app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt diff --git a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt index 10b9e947..b5b00de6 100644 --- a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt +++ b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt @@ -1,138 +1,4 @@ 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 = withContext(Dispatchers.IO) { - val notes = mutableListOf() - 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" - +interface TextNotesRepo { +} \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt new file mode 100644 index 00000000..10b9e947 --- /dev/null +++ b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt @@ -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 = withContext(Dispatchers.IO) { + val notes = mutableListOf() + 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" + diff --git a/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt new file mode 100644 index 00000000..e8dd7fc9 --- /dev/null +++ b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt @@ -0,0 +1,4 @@ +package space.taran.arkmemo.di + +class PreferencesModule { +} \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt b/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt new file mode 100644 index 00000000..a62f3a51 --- /dev/null +++ b/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt @@ -0,0 +1,4 @@ +package space.taran.arkmemo.di + +class RepositoryModule { +} \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt index 1e7b5448..ca1033f2 100644 --- a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt +++ b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt @@ -1,47 +1,4 @@ package space.taran.arkmemo.preferences -import android.content.Context -import android.content.Context.MODE_PRIVATE -import java.io.File -import java.nio.file.Path - -class MemoPreferences private constructor(context: Context) { - private val sharedPreferences = context.getSharedPreferences(NAME, MODE_PRIVATE) - private val prefEditor = sharedPreferences.edit() - - fun storePath(path: String){ - prefEditor.apply{ - putString(CURRENT_NOTES_PATH, path) - apply() - } - } - - fun getPathString() = sharedPreferences.getString(CURRENT_NOTES_PATH, null) - - fun getPath(): Path? { - val pathString = getPathString() - var path: Path? = null - try { - val file = File(pathString!!) - file.mkdir() - path = file.toPath() - } catch (e: Exception) { - e.printStackTrace() - } - return path - } - - companion object{ - private const val NAME = "memo_prefs" - private const val CURRENT_NOTES_PATH = "current_notes_path" - private var preferences: MemoPreferences? = null - - fun getInstance(context: Context): MemoPreferences{ - if(preferences == null) - preferences = MemoPreferences(context) - return preferences!! - } - - fun getInstance() = preferences!! - } +interface MemoPreferences { } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt new file mode 100644 index 00000000..1e7b5448 --- /dev/null +++ b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt @@ -0,0 +1,47 @@ +package space.taran.arkmemo.preferences + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import java.io.File +import java.nio.file.Path + +class MemoPreferences private constructor(context: Context) { + private val sharedPreferences = context.getSharedPreferences(NAME, MODE_PRIVATE) + private val prefEditor = sharedPreferences.edit() + + fun storePath(path: String){ + prefEditor.apply{ + putString(CURRENT_NOTES_PATH, path) + apply() + } + } + + fun getPathString() = sharedPreferences.getString(CURRENT_NOTES_PATH, null) + + fun getPath(): Path? { + val pathString = getPathString() + var path: Path? = null + try { + val file = File(pathString!!) + file.mkdir() + path = file.toPath() + } catch (e: Exception) { + e.printStackTrace() + } + return path + } + + companion object{ + private const val NAME = "memo_prefs" + private const val CURRENT_NOTES_PATH = "current_notes_path" + private var preferences: MemoPreferences? = null + + fun getInstance(context: Context): MemoPreferences{ + if(preferences == null) + preferences = MemoPreferences(context) + return preferences!! + } + + fun getInstance() = preferences!! + } +} \ No newline at end of file From 5b9e55256c72e193d2bf9e8382b3bae40026e1d5 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:47:31 +0700 Subject: [PATCH 2/8] Provide preferences and replace singleton --- .../arkmemo/preferences/MemoPreferences.kt | 7 +++++ .../preferences/MemoPreferencesImpl.kt | 31 +++++++------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt index ca1033f2..b35a3f87 100644 --- a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt +++ b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt @@ -1,4 +1,11 @@ package space.taran.arkmemo.preferences +import java.nio.file.Path + interface MemoPreferences { + fun storePath(path: String) + + fun getPathString(): String? + + fun getPath(): Path? } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt index 1e7b5448..0011bf69 100644 --- a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt +++ b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferencesImpl.kt @@ -2,23 +2,30 @@ package space.taran.arkmemo.preferences import android.content.Context import android.content.Context.MODE_PRIVATE +import dagger.hilt.android.qualifiers.ApplicationContext import java.io.File import java.nio.file.Path +import javax.inject.Inject -class MemoPreferences private constructor(context: Context) { + +private const val NAME = "memo_prefs" +private const val CURRENT_NOTES_PATH = "current_notes_path" + +class MemoPreferencesImpl @Inject constructor(@ApplicationContext context: Context) : + MemoPreferences { private val sharedPreferences = context.getSharedPreferences(NAME, MODE_PRIVATE) private val prefEditor = sharedPreferences.edit() - fun storePath(path: String){ - prefEditor.apply{ + override fun storePath(path: String) { + prefEditor.apply { putString(CURRENT_NOTES_PATH, path) apply() } } - fun getPathString() = sharedPreferences.getString(CURRENT_NOTES_PATH, null) + override fun getPathString() = sharedPreferences.getString(CURRENT_NOTES_PATH, null) - fun getPath(): Path? { + override fun getPath(): Path? { val pathString = getPathString() var path: Path? = null try { @@ -30,18 +37,4 @@ class MemoPreferences private constructor(context: Context) { } return path } - - companion object{ - private const val NAME = "memo_prefs" - private const val CURRENT_NOTES_PATH = "current_notes_path" - private var preferences: MemoPreferences? = null - - fun getInstance(context: Context): MemoPreferences{ - if(preferences == null) - preferences = MemoPreferences(context) - return preferences!! - } - - fun getInstance() = preferences!! - } } \ No newline at end of file From baabfb16b0059a36666085a00194e697a189b63e Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:47:43 +0700 Subject: [PATCH 3/8] Create module --- .../space/taran/arkmemo/di/PreferencesModule.kt | 13 ++++++++++++- .../space/taran/arkmemo/di/RepositoryModule.kt | 14 +++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt index e8dd7fc9..18cffff2 100644 --- a/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt +++ b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt @@ -1,4 +1,15 @@ package space.taran.arkmemo.di -class PreferencesModule { +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import space.taran.arkmemo.preferences.MemoPreferences +import space.taran.arkmemo.preferences.MemoPreferencesImpl + +@InstallIn(SingletonComponent::class) +@Module +abstract class PreferencesModule { + @Binds + abstract fun bindRepository(impl: MemoPreferencesImpl): MemoPreferences } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt b/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt index a62f3a51..a4fadb03 100644 --- a/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt +++ b/app/src/main/java/space/taran/arkmemo/di/RepositoryModule.kt @@ -1,4 +1,16 @@ package space.taran.arkmemo.di -class RepositoryModule { +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import space.taran.arkmemo.data.repositories.TextNotesRepo +import space.taran.arkmemo.data.repositories.TextNotesRepoImpl + + +@InstallIn(SingletonComponent::class) +@Module +abstract class RepositoryModule { + @Binds + abstract fun bindRepository(impl: TextNotesRepoImpl): TextNotesRepo } \ No newline at end of file From 1aa2c26ac499847511fb34c46e129677de2b2702 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:47:54 +0700 Subject: [PATCH 4/8] Extract methods to interfaces --- .../arkmemo/data/repositories/TextNotesRepo.kt | 9 +++++++++ .../data/repositories/TextNotesRepoImpl.kt | 16 +++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt index b5b00de6..18e8144d 100644 --- a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt +++ b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepo.kt @@ -1,4 +1,13 @@ package space.taran.arkmemo.data.repositories +import kotlinx.coroutines.CoroutineScope +import space.taran.arkmemo.models.TextNote +import java.nio.file.Path + interface TextNotesRepo { + suspend fun init(root: Path, scope: CoroutineScope) + suspend fun save(note: TextNote) + suspend fun delete(note: TextNote): Int + suspend fun read(): List + } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt index 10b9e947..a5e08123 100644 --- a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt +++ b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepoImpl.kt @@ -9,18 +9,13 @@ 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 @@ -29,8 +24,7 @@ import kotlin.io.path.moveTo import kotlin.io.path.name import kotlin.io.path.writeLines -@Singleton -class TextNotesRepo @Inject constructor() { +class TextNotesRepoImpl @Inject constructor(): TextNotesRepo { private val iODispatcher = Dispatchers.IO @@ -39,17 +33,17 @@ class TextNotesRepo @Inject constructor() { private lateinit var root: Path - suspend fun init(root: Path, scope: CoroutineScope) { + override suspend fun init(root: Path, scope: CoroutineScope) { this.root = root propertiesStorageRepo = PropertiesStorageRepo(scope) propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root)) } - suspend fun save(note: TextNote) { + override suspend fun save(note: TextNote) { write(note) } - suspend fun delete(note: TextNote) = withContext(iODispatcher) { + override suspend fun delete(note: TextNote) = withContext(iODispatcher) { val path = root.resolve("${note.meta?.name}") delete(path) propertiesStorage.remove(note.meta?.id!!) @@ -57,7 +51,7 @@ class TextNotesRepo @Inject constructor() { Log.d("text-repo", "${note.meta?.name!!} has been deleted") } - suspend fun read(): List = withContext(Dispatchers.IO) { + override suspend fun read(): List = withContext(Dispatchers.IO) { val notes = mutableListOf() Files.list(root).forEach { path -> if (path.fileName.extension == NOTE_EXT) { From f5e7a0963e7b494388539e329bbbce536de32416 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:48:03 +0700 Subject: [PATCH 5/8] Replace singleton --- .../space/taran/arkmemo/ui/activities/MainActivity.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/ui/activities/MainActivity.kt b/app/src/main/java/space/taran/arkmemo/ui/activities/MainActivity.kt index dba5cdcc..f49928b0 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/activities/MainActivity.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/activities/MainActivity.kt @@ -23,12 +23,16 @@ import space.taran.arkmemo.preferences.MemoPreferences import space.taran.arkmemo.ui.fragments.EditTextNotesFragment import space.taran.arkmemo.ui.fragments.SettingsFragment import space.taran.arkmemo.ui.fragments.TextNotesFragment +import javax.inject.Inject @AndroidEntryPoint class MainActivity : AppCompatActivity(R.layout.activity_main) { private val binding by viewBinding(ActivityMainBinding::bind) + @Inject + lateinit var memoPreferences: MemoPreferences + @IdRes private val fragContainer = R.id.container @@ -84,11 +88,11 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } - if (MemoPreferences.getInstance(this).getPathString() == null) { + if (memoPreferences.getPathString() == null) { FilePicker.show(this, supportFragmentManager) supportFragmentManager.onArkPathPicked(this) { - MemoPreferences.getInstance(this).storePath(it.toString()) + memoPreferences.storePath(it.toString()) showFragment() } } From 60f47246936d675cef7dd703c651e34cf640024e Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:48:19 +0700 Subject: [PATCH 6/8] Use callback instead of using singleton --- .../main/java/space/taran/arkmemo/ui/views/PathPreference.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/ui/views/PathPreference.kt b/app/src/main/java/space/taran/arkmemo/ui/views/PathPreference.kt index f4421907..f3e13d19 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/views/PathPreference.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/views/PathPreference.kt @@ -6,11 +6,11 @@ import android.widget.TextView import androidx.preference.Preference import androidx.preference.PreferenceViewHolder import space.taran.arkmemo.R -import space.taran.arkmemo.preferences.MemoPreferences class PathPreference(context: Context, attrs: AttributeSet): Preference(context, attrs) { private var title: TextView? = null private var path: TextView? = null + var onBindView: () -> Unit = {} fun setPath(path: String?){ if(path != null) @@ -26,6 +26,6 @@ class PathPreference(context: Context, attrs: AttributeSet): Preference(context, super.onBindViewHolder(holder) title = holder.findViewById(R.id.title) as TextView path = holder.findViewById(R.id.pathValue) as TextView - setPath(MemoPreferences.getInstance(context).getPathString()) + onBindView() } } \ No newline at end of file From dd28e5143a8cd6f16414fa45e287a1c9569f17f4 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 11:48:37 +0700 Subject: [PATCH 7/8] Refactor and hook callback --- .../arkmemo/ui/fragments/SettingsFragment.kt | 15 +++++++++++++-- .../taran/arkmemo/ui/viewmodels/NotesViewModel.kt | 8 +++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/space/taran/arkmemo/ui/fragments/SettingsFragment.kt b/app/src/main/java/space/taran/arkmemo/ui/fragments/SettingsFragment.kt index ea6f2727..020b164e 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/fragments/SettingsFragment.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/fragments/SettingsFragment.kt @@ -3,15 +3,22 @@ package space.taran.arkmemo.ui.fragments import android.os.Bundle import android.view.View import androidx.preference.PreferenceFragmentCompat +import dagger.hilt.android.AndroidEntryPoint import dev.arkbuilders.arkfilepicker.presentation.onArkPathPicked import space.taran.arkmemo.R import space.taran.arkmemo.files.FilePicker import space.taran.arkmemo.preferences.MemoPreferences import space.taran.arkmemo.ui.activities.MainActivity import space.taran.arkmemo.ui.views.PathPreference +import javax.inject.Inject + +@AndroidEntryPoint class SettingsFragment : PreferenceFragmentCompat() { + @Inject + lateinit var memoPreferences: MemoPreferences + private val activity: MainActivity by lazy{ requireActivity() as MainActivity } @@ -23,7 +30,11 @@ class SettingsFragment : PreferenceFragmentCompat() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val pathKey = getString(R.string.path_pref_key) - val pathPref: PathPreference? = findPreference(pathKey) + val pathPref: PathPreference? = findPreference(pathKey)?.apply { + onBindView = { + setPath(memoPreferences.getPathString()) + } + } activity.title = getString(R.string.settings) activity.showSettingsButton(false) activity.supportActionBar?.setDisplayHomeAsUpEnabled(true) @@ -35,7 +46,7 @@ class SettingsFragment : PreferenceFragmentCompat() { parentFragmentManager.onArkPathPicked(viewLifecycleOwner) { val pathString = it.toString() - MemoPreferences.getInstance(requireContext()).storePath(pathString) + memoPreferences.storePath(pathString) pathPref?.setPath(pathString) } } diff --git a/app/src/main/java/space/taran/arkmemo/ui/viewmodels/NotesViewModel.kt b/app/src/main/java/space/taran/arkmemo/ui/viewmodels/NotesViewModel.kt index 89649dd1..882a3b61 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/viewmodels/NotesViewModel.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/viewmodels/NotesViewModel.kt @@ -13,9 +13,11 @@ import space.taran.arkmemo.preferences.MemoPreferences import javax.inject.Inject @HiltViewModel -class NotesViewModel @Inject constructor(): ViewModel() { +class NotesViewModel @Inject constructor( + private val textNotesRepo: TextNotesRepo, + private val memoPreferences: MemoPreferences +) : ViewModel() { - @Inject lateinit var textNotesRepo: TextNotesRepo private val iODispatcher = Dispatchers.IO @@ -24,7 +26,7 @@ class NotesViewModel @Inject constructor(): ViewModel() { fun init() { viewModelScope.launch(iODispatcher) { textNotesRepo.init( - MemoPreferences.getInstance().getPath()!!, + memoPreferences.getPath()!!, viewModelScope ) notes.value = textNotesRepo.read() From 1dadf9a0eb0df5000f04b1c568615e5a17bb4670 Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Sat, 28 Oct 2023 12:21:06 +0700 Subject: [PATCH 8/8] Rename method --- app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt index 18cffff2..e28e3d80 100644 --- a/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt +++ b/app/src/main/java/space/taran/arkmemo/di/PreferencesModule.kt @@ -11,5 +11,5 @@ import space.taran.arkmemo.preferences.MemoPreferencesImpl @Module abstract class PreferencesModule { @Binds - abstract fun bindRepository(impl: MemoPreferencesImpl): MemoPreferences + abstract fun bindMemoPreferences(impl: MemoPreferencesImpl): MemoPreferences } \ No newline at end of file