From d8867296c2cb77efd045ae118dee855f207879ab Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Wed, 27 Sep 2023 10:01:04 +0200 Subject: [PATCH 01/14] Separating user meta-data --- app/build.gradle | 6 +- app/src/main/java/space/taran/arkmemo/App.kt | 9 +- .../space/taran/arkmemo/data/ResourceMeta.kt | 3 +- .../data/repositories/TextNotesRepository.kt | 197 +++++++++--------- .../data/viewmodels/EditTextNotesViewModel.kt | 11 +- .../data/viewmodels/TextNotesViewModel.kt | 24 ++- .../space/taran/arkmemo/files/FilePicker.kt | 4 +- .../arkmemo/preferences/MemoPreferences.kt | 19 +- .../arkmemo/ui/activities/MainActivity.kt | 10 +- .../ui/fragments/EditTextNotesFragment.kt | 41 ++-- .../arkmemo/ui/fragments/SettingsFragment.kt | 2 +- .../arkmemo/ui/fragments/TextNotesFragment.kt | 11 +- .../taran/arkmemo/ui/views/PathPreference.kt | 2 +- 13 files changed, 194 insertions(+), 145 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 88cf1a46..026f0cc4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,10 +84,8 @@ dependencies { implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' - //implementation 'com.github.ARK-Builders:ark-filepicker:b2bfa01ea7' - - implementation 'com.github.ark-builders:ark-filepicker:b2bfa01ea7' - implementation 'space.taran:arklib:0.1.0-SNAPSHOT-7df9a4e581' + implementation 'com.github.ark-builders:ark-filepicker:ae2c37ea87' + implementation 'dev.arkbuilders:arklib:0.3.1' implementation 'ch.acra:acra-http:5.9.5' implementation 'ch.acra:acra-dialog:5.9.5' diff --git a/app/src/main/java/space/taran/arkmemo/App.kt b/app/src/main/java/space/taran/arkmemo/App.kt index 83839790..fc093011 100644 --- a/app/src/main/java/space/taran/arkmemo/App.kt +++ b/app/src/main/java/space/taran/arkmemo/App.kt @@ -3,6 +3,7 @@ package space.taran.arkmemo import android.app.Application import android.content.Context import dagger.hilt.android.HiltAndroidApp +import dev.arkbuilders.arklib.initArkLib import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -11,13 +12,17 @@ import org.acra.config.httpSender import org.acra.data.StringFormat import org.acra.ktx.initAcra import org.acra.sender.HttpSender +import space.taran.arkfilepicker.folders.FoldersRepo import space.taran.arkmemo.space.taran.arkmemo.utils.Config @HiltAndroidApp class App: Application() { - override fun attachBaseContext(base: Context?) { - super.attachBaseContext(base) + override fun onCreate() { + super.onCreate() + System.loadLibrary("arklib") + initArkLib() + FoldersRepo.init(this) initAcra() } diff --git a/app/src/main/java/space/taran/arkmemo/data/ResourceMeta.kt b/app/src/main/java/space/taran/arkmemo/data/ResourceMeta.kt index d32d62bb..d165bd32 100644 --- a/app/src/main/java/space/taran/arkmemo/data/ResourceMeta.kt +++ b/app/src/main/java/space/taran/arkmemo/data/ResourceMeta.kt @@ -1,9 +1,10 @@ package space.taran.arkmemo.data +import dev.arkbuilders.arklib.ResourceId import java.nio.file.attribute.FileTime data class ResourceMeta( - val id: Long, + val id: ResourceId, val name: String, val extension: String, val modified: FileTime, diff --git a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepository.kt b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepository.kt index 264025f3..a1700f41 100644 --- a/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepository.kt +++ b/app/src/main/java/space/taran/arkmemo/data/repositories/TextNotesRepository.kt @@ -1,139 +1,150 @@ package space.taran.arkmemo.data.repositories -import android.content.Context import android.util.Log -import dagger.hilt.android.qualifiers.ApplicationContext -import space.taran.arklib.computeId +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.launch +import kotlinx.coroutines.withContext import space.taran.arkmemo.data.ResourceMeta -import space.taran.arkmemo.files.parsers.JsonParser import space.taran.arkmemo.models.TextNote -import space.taran.arkmemo.preferences.MemoPreferences -import java.io.* +import java.io.BufferedReader +import java.io.BufferedWriter +import java.io.File +import java.io.FileReader +import java.io.FileWriter import java.nio.file.Files import java.nio.file.Path import javax.inject.Inject +import javax.inject.Singleton import kotlin.io.path.extension import kotlin.io.path.getLastModifiedTime +@Singleton class TextNotesRepository @Inject constructor() { - @Inject @ApplicationContext lateinit var context: Context + private val _textNotes = MutableStateFlow(listOf()) + val textNotes: StateFlow> = _textNotes - fun saveNote(note: TextNote?) { + private val iODispatcher = Dispatchers.IO + + private lateinit var propertiesStorage: PropertiesStorage + private lateinit var propertiesStorageRepo: PropertiesStorageRepo + + private lateinit var root: Path + + fun init(root: Path, scope: CoroutineScope) { + this.root = root + scope.launch(iODispatcher) { + propertiesStorageRepo = PropertiesStorageRepo(this) + propertiesStorage = propertiesStorageRepo.provide(RootIndex.provide(root)) + readAllNotes() + } + } + + suspend fun saveNote(note: TextNote?) { if (note != null) { - val path = getPath() - if (path != null) { - Files.list(path) - createTextNoteFile( - path, - JsonParser.parseNoteToJson(note.content), - ) + withContext(iODispatcher) { + writeTextNoteFile(note.content) + readAllNotes() } } } - fun deleteNote(note: TextNote) { - val filePath = getPath()?.resolve("${note.meta?.name}") + suspend fun deleteNote(note: TextNote) = withContext(iODispatcher) { + val filePath = root.resolve("${note.meta?.name}") removeFileFromMemory(filePath) - Log.d("Deleted", note.meta?.name!!) + propertiesStorage.remove(note.meta?.id!!) + propertiesStorage.persist() + Log.d("notes-repo", "${note.meta.name} has been deleted") + readAllNotes() } - fun getAllNotes(): List { + private suspend fun readAllNotes() { val notes = mutableListOf() - val path = getPath() - var number = 0 - if (path != null) { - Files.list(path).forEach { filePath -> - if (filePath.fileName.extension == NOTE_EXT) { - number += 1 - try { - val jsonFile = filePath.toFile() - val fileReader = FileReader(jsonFile) - val bufferedReader = BufferedReader(fileReader) - val jsonTextNote = StringBuilder() - with(bufferedReader) { - forEachLine { - jsonTextNote.append(it) - } - val content = JsonParser.parseNoteFromJson(jsonTextNote.toString()) - val size = Files.size(filePath) - val id = computeId(size, filePath) - val meta = ResourceMeta( - id, - filePath.fileName.toString(), - filePath.extension, - filePath.getLastModifiedTime(), - size - ) - - val note = TextNote(content, meta) - notes.add(note) - close() + Files.list(root).forEach { filePath -> + if (filePath.fileName.extension == NOTE_EXT) { + try { + val file = filePath.toFile() + val fileReader = FileReader(file) + val bufferedReader = BufferedReader(fileReader) + val data = StringBuilder() + with(bufferedReader) { + forEachLine { + data.append(it) } - } catch (e: Exception) { - e.printStackTrace() + val size = Files.size(filePath) + val id = computeId(size, filePath) + val meta = ResourceMeta( + id, + filePath.fileName.toString(), + filePath.extension, + filePath.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) + close() } + } catch (e: Exception) { + e.printStackTrace() } } } - return notes + Log.d("notes-repo", "notes ${notes.size}") + _textNotes.emit(notes) } - private fun createTextNoteFile(path: Path?, noteString: String?) { - fun writeToFile(bufferedWriter: BufferedWriter) { - with(bufferedWriter) { - write(noteString) - close() - } - } - if (path != null) { - val file = path.toFile() - val noteFile = File(file, "${DUMMY_FILENAME}.${NOTE_EXT}") - if (!noteFile.exists()) { - try { - val fileWriter = FileWriter(noteFile) - val bufferedWriter = BufferedWriter(fileWriter) - writeToFile(bufferedWriter) + private suspend fun writeTextNoteFile(content: TextNote.Content) = withContext(Dispatchers.IO) { + val file = root.toFile() + val noteFile = File(file, "${DUMMY_FILENAME}.${NOTE_EXT}") + if (!noteFile.exists()) { + try { + val fileWriter = FileWriter(noteFile) + val bufferedWriter = BufferedWriter(fileWriter) + writeToFile(bufferedWriter, content.data) - val id = computeId(Files.size(noteFile.toPath()), noteFile.toPath()) + val id = computeId(Files.size(noteFile.toPath()), noteFile.toPath()) - Log.d("Filename", noteFile.name) + val properties = Properties(setOf(content.title), setOf()) - with(noteFile){ - val newFile = File(file, "$id.${NOTE_EXT}") + Log.d("notes-repo", "filename ${noteFile.name}") - if(!newFile.exists()) - if (renameTo(newFile)) - Log.d("New filename", newFile.name) - else - removeFileFromMemory(noteFile.toPath()) + propertiesStorage.setProperties(id, properties) + propertiesStorage.persist() - } + with(noteFile) { + val newFile = File(file, "${id.crc32}.${NOTE_EXT}") - } catch (e: Exception) { - e.printStackTrace() + if (!newFile.exists()) + if (renameTo(newFile)) + Log.d("notes-repo", "new filename ${newFile.name}") + else + removeFileFromMemory(this.toPath()) } + } catch (e: Exception) { + e.printStackTrace() } } } - private fun removeFileFromMemory(path: Path?) { - if (path != null) - Files.deleteIfExists(path) + private fun writeToFile(bufferedWriter: BufferedWriter, string: String) { + with(bufferedWriter) { + write(string) + close() + } } - private fun getPath(): Path? { - val prefs = MemoPreferences.getInstance(context) - val pathString = prefs.getPath() - var path: Path? = null - try { - val file = File(pathString!!) - file.mkdir() - path = file.toPath() - } catch (e: Exception) { - e.printStackTrace() - } - return path + private fun removeFileFromMemory(filePath: Path) { + Files.deleteIfExists(filePath) } companion object { diff --git a/app/src/main/java/space/taran/arkmemo/data/viewmodels/EditTextNotesViewModel.kt b/app/src/main/java/space/taran/arkmemo/data/viewmodels/EditTextNotesViewModel.kt index e1d74424..b1c2c14b 100644 --- a/app/src/main/java/space/taran/arkmemo/data/viewmodels/EditTextNotesViewModel.kt +++ b/app/src/main/java/space/taran/arkmemo/data/viewmodels/EditTextNotesViewModel.kt @@ -1,6 +1,5 @@ package space.taran.arkmemo.data.viewmodels -import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel @@ -8,6 +7,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import space.taran.arkmemo.data.repositories.TextNotesRepository import space.taran.arkmemo.models.TextNote +import space.taran.arkmemo.preferences.MemoPreferences import javax.inject.Inject @HiltViewModel @@ -17,7 +17,14 @@ class EditTextNotesViewModel @Inject constructor(): ViewModel() { @Inject lateinit var repo: TextNotesRepository - fun saveNote(note: TextNote){ + fun init() { + repo.init( + MemoPreferences.getInstance().getPath()!!, + viewModelScope + ) + } + + fun onSaveNoteClick(note: TextNote) { viewModelScope.launch(iODispatcher) { repo.saveNote(note) } diff --git a/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt b/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt index cfc2492f..8cafe3a1 100644 --- a/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt +++ b/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt @@ -1,38 +1,44 @@ package space.taran.arkmemo.data.viewmodels -import android.content.Context import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel +import dev.arkbuilders.arklib.user.properties.PropertiesStorageRepo import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext import space.taran.arkmemo.data.repositories.TextNotesRepository import space.taran.arkmemo.models.TextNote +import space.taran.arkmemo.preferences.MemoPreferences import javax.inject.Inject @HiltViewModel class TextNotesViewModel @Inject constructor(): ViewModel() { @Inject lateinit var textNotesRepo: TextNotesRepository + private val iODispatcher = Dispatchers.IO - private val textNotes: MutableStateFlow> by lazy{ - MutableStateFlow(listOf()) + + fun init() { + textNotesRepo.init( + MemoPreferences.getInstance().getPath()!!, + viewModelScope + ) } fun deleteNote(note: TextNote){ viewModelScope.launch(iODispatcher) { textNotesRepo.deleteNote(note) - textNotes.value = textNotesRepo.getAllNotes() } } - fun getAllNotes(): StateFlow>{ - viewModelScope.launch(iODispatcher) { - textNotes.value = textNotesRepo.getAllNotes() + fun getAllLatestNotes(emit: (List) -> Unit){ + viewModelScope.launch(Dispatchers.Main) { + textNotesRepo.textNotes.collectLatest { + emit(it) + } } - return textNotes } } \ No newline at end of file diff --git a/app/src/main/java/space/taran/arkmemo/files/FilePicker.kt b/app/src/main/java/space/taran/arkmemo/files/FilePicker.kt index 6681f172..0f11ce5b 100644 --- a/app/src/main/java/space/taran/arkmemo/files/FilePicker.kt +++ b/app/src/main/java/space/taran/arkmemo/files/FilePicker.kt @@ -11,8 +11,8 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.fragment.app.FragmentManager import space.taran.arkfilepicker.ArkFilePickerConfig -import space.taran.arkfilepicker.ArkFilePickerFragment -import space.taran.arkfilepicker.ArkFilePickerMode +import space.taran.arkfilepicker.presentation.filepicker.ArkFilePickerFragment +import space.taran.arkfilepicker.presentation.filepicker.ArkFilePickerMode import space.taran.arkmemo.BuildConfig import space.taran.arkmemo.R 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 fb5ccde6..1e7b5448 100644 --- a/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt +++ b/app/src/main/java/space/taran/arkmemo/preferences/MemoPreferences.kt @@ -2,6 +2,8 @@ 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) @@ -14,7 +16,20 @@ class MemoPreferences private constructor(context: Context) { } } - fun getPath() = sharedPreferences.getString(CURRENT_NOTES_PATH, null) + 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" @@ -26,5 +41,7 @@ class MemoPreferences private constructor(context: Context) { preferences = MemoPreferences(context) return preferences!! } + + fun getInstance() = preferences!! } } \ No newline at end of file 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 03b0f77c..a1060e67 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 @@ -13,7 +13,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import by.kirich1409.viewbindingdelegate.viewBinding import dagger.hilt.android.AndroidEntryPoint -import space.taran.arkfilepicker.onArkPathPicked +import space.taran.arkfilepicker.presentation.onArkPathPicked import space.taran.arkmemo.R import space.taran.arkmemo.contracts.PermissionContract import space.taran.arkmemo.databinding.ActivityMainBinding @@ -51,15 +51,13 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - System.loadLibrary("arklib") - setContentView(binding.root) setSupportActionBar(binding.toolbar) binding.toolbar.setNavigationOnClickListener { - onBackPressed() + onBackPressedDispatcher.onBackPressed() } - fun showFragment(){ + fun showFragment() { val textDataFromIntent = intent?.getStringExtra(Intent.EXTRA_TEXT) if (textDataFromIntent != null) { fragment = EditTextNotesFragment.newInstance(textDataFromIntent) @@ -85,7 +83,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } - if (MemoPreferences.getInstance(this).getPath() == null) { + if (MemoPreferences.getInstance(this).getPathString() == null) { FilePicker.show(this, supportFragmentManager) supportFragmentManager.onArkPathPicked(this) { diff --git a/app/src/main/java/space/taran/arkmemo/ui/fragments/EditTextNotesFragment.kt b/app/src/main/java/space/taran/arkmemo/ui/fragments/EditTextNotesFragment.kt index 70ca2c7b..88b4a0e0 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/fragments/EditTextNotesFragment.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/fragments/EditTextNotesFragment.kt @@ -3,17 +3,14 @@ package space.taran.arkmemo.ui.fragments import android.os.Bundle import android.text.Editable import android.text.TextWatcher -import android.util.Log import android.view.View import android.widget.Toast import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels -import androidx.fragment.app.viewModels 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.data.viewmodels.TextNotesViewModel import space.taran.arkmemo.databinding.FragmentEditTextNotesBinding import space.taran.arkmemo.models.TextNote import space.taran.arkmemo.ui.activities.MainActivity @@ -25,16 +22,21 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) { requireActivity() as MainActivity } - private val editViewModel: EditTextNotesViewModel by viewModels() + private val editViewModel: EditTextNotesViewModel by activityViewModels() private val binding by viewBinding(FragmentEditTextNotesBinding::bind) - private var note: TextNote? = null + private var note = TextNote(TextNote.Content("", "")) private var noteStr: String? = null + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + editViewModel.init() + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - var note:TextNote? = null + var note = this.note val editTextListener = object: TextWatcher{ override fun afterTextChanged(s: Editable?) = Unit @@ -42,9 +44,9 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) { override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { val noteString = s?.toString() - var title = "" + var title = this@EditTextNotesFragment.note.content.title if(noteString != null){ - for(char in noteString){ + if (title == "") for(char in noteString){ if(char != '\n'){ title += char } @@ -64,8 +66,9 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) { val saveNoteButton = binding.saveNote if(arguments != null) { - this.note = requireArguments().getParcelable(NOTE_KEY) - //Log.d("Note", "${this.note?.content}") + requireArguments().getParcelable(NOTE_KEY)?.let { + this.note = it + } noteStr = requireArguments().getString(NOTE_STRING_KEY) } @@ -75,22 +78,18 @@ class EditTextNotesFragment: Fragment(R.layout.fragment_edit_text_notes) { editNote.requestFocus() editNote.addTextChangedListener(editTextListener) - - if(this.note != null) - editNote.setText(this.note?.content?.data!!) + editNote.setText(this.note.content.data) if(noteStr != null) editNote.setText(noteStr) saveNoteButton.setOnClickListener { - if(note != null) { - with(editViewModel){ - saveNote(note!!) - Toast.makeText(requireContext(), getString(R.string.ark_memo_note_saved), - Toast.LENGTH_SHORT) - .show() - activity.onBackPressed() - } + with(editViewModel){ + onSaveNoteClick(note) + Toast.makeText(requireContext(), getString(R.string.ark_memo_note_saved), + Toast.LENGTH_SHORT) + .show() + activity.onBackPressed() } } } 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 3f2441ae..4e58be77 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,7 +3,7 @@ package space.taran.arkmemo.ui.fragments import android.os.Bundle import android.view.View import androidx.preference.PreferenceFragmentCompat -import space.taran.arkfilepicker.onArkPathPicked +import space.taran.arkfilepicker.presentation.onArkPathPicked import space.taran.arkmemo.R import space.taran.arkmemo.files.FilePicker import space.taran.arkmemo.preferences.MemoPreferences diff --git a/app/src/main/java/space/taran/arkmemo/ui/fragments/TextNotesFragment.kt b/app/src/main/java/space/taran/arkmemo/ui/fragments/TextNotesFragment.kt index f7e49a7b..3384afb9 100644 --- a/app/src/main/java/space/taran/arkmemo/ui/fragments/TextNotesFragment.kt +++ b/app/src/main/java/space/taran/arkmemo/ui/fragments/TextNotesFragment.kt @@ -5,6 +5,7 @@ import android.view.View 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 @@ -32,7 +33,8 @@ class TextNotesFragment: Fragment(R.layout.fragment_text_notes) { private val activity: MainActivity by lazy { requireActivity() as MainActivity } - private val textNotesViewModel: TextNotesViewModel by viewModels() + + private val textNotesViewModel: TextNotesViewModel by activityViewModels() private lateinit var newNoteButton: FloatingActionButton private lateinit var pasteNoteButton: Button @@ -53,6 +55,11 @@ class TextNotesFragment: Fragment(R.layout.fragment_text_notes) { else Toast.makeText(requireContext(), getString(R.string.nothing_to_paste), Toast.LENGTH_SHORT).show() } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + textNotesViewModel.init() + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) recyclerView = binding.include.recyclerView @@ -65,7 +72,7 @@ class TextNotesFragment: Fragment(R.layout.fragment_text_notes) { lifecycleScope.launch { viewLifecycleOwner.apply{ repeatOnLifecycle(Lifecycle.State.STARTED) { - textNotesViewModel.getAllNotes().collect { + textNotesViewModel.getAllLatestNotes { val adapter = TextNotesListAdapter(it) val layoutManager = LinearLayoutManager(requireContext()) adapter.setActivity(activity) 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 8aad0d23..f4421907 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 @@ -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).getPath()) + setPath(MemoPreferences.getInstance(context).getPathString()) } } \ No newline at end of file From c79892ae7745d1c5f67fc1df6858670cb2e8e34a Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Wed, 27 Sep 2023 17:31:22 +0200 Subject: [PATCH 02/14] Fix duplicate class --- app/build.gradle | 2 +- .../space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 026f0cc4..83bdf5ee 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,8 +84,8 @@ dependencies { implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' - implementation 'com.github.ark-builders:ark-filepicker:ae2c37ea87' implementation 'dev.arkbuilders:arklib:0.3.1' + implementation 'com.github.ark-builders:ark-filepicker:ae2c37ea87' implementation 'ch.acra:acra-http:5.9.5' implementation 'ch.acra:acra-dialog:5.9.5' diff --git a/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt b/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt index 8cafe3a1..b953a6c8 100644 --- a/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt +++ b/app/src/main/java/space/taran/arkmemo/data/viewmodels/TextNotesViewModel.kt @@ -3,10 +3,7 @@ package space.taran.arkmemo.data.viewmodels import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel -import dev.arkbuilders.arklib.user.properties.PropertiesStorageRepo import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.launch import space.taran.arkmemo.data.repositories.TextNotesRepository From c8da6dcf79fb3c676c88d593e600822475a71272 Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Wed, 27 Sep 2023 22:49:43 +0200 Subject: [PATCH 03/14] Fix duplicate class --- build.gradle | 29 +++------------------- gradle/wrapper/gradle-wrapper.properties | 6 ++--- settings.gradle | 31 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/build.gradle b/build.gradle index 7ada9689..274ca322 100644 --- a/build.gradle +++ b/build.gradle @@ -5,33 +5,12 @@ buildscript { ext.kotlin_version="1.7.10" } // Top-level build file where you can add configuration options common to all sub-projects/modules. -buildscript { - repositories { - google() - mavenCentral() - maven{url 'https://jitpack.io'} - maven { url "https://plugins.gradle.org/m2/" } - gradlePluginPortal() - } - dependencies { - classpath 'com.android.tools.build:gradle:7.2.2' - classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10' - classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42' - classpath 'io.github.0ffz:gpr-for-gradle:1.2.1' - } +plugins { + id 'com.android.application' version '7.4.2' apply false + id 'org.jetbrains.kotlin.android' version '1.7.10' apply false + id 'com.google.dagger.hilt.android' version '2.42' apply false } -allprojects { - apply plugin: "io.github.0ffz.github-packages" - repositories{ - google() - mavenCentral() - maven{url 'https://jitpack.io'} - maven { url "https://plugins.gradle.org/m2/" } - gradlePluginPortal() - maven githubPackage.invoke("ARK-Builders/arklib-android") - } -} task clean(type: Delete) { delete rootProject.buildDir diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index be3e1bc0..cf628239 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 05 16:34:57 CAT 2022 +#Wed Sep 27 22:27:08 SAST 2023 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip distributionPath=wrapper/dists -zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index 5086bb2c..849a9b6c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,33 @@ +import org.gradle.api.initialization.resolve.RepositoriesMode + +pluginManagement { + repositories { + google() + mavenCentral() + maven{url 'https://jitpack.io'} + maven { url "https://plugins.gradle.org/m2/" } + gradlePluginPortal() + } +} + +dependencyResolutionManagement { + repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) + repositories { + google() + mavenCentral() + maven{url 'https://jitpack.io'} + maven { url "https://plugins.gradle.org/m2/" } + gradlePluginPortal() + maven { + name = "GitHubPackages" + url = "https://maven.pkg.github.com/ARK-Builders/arklib-android" + credentials { + username = "token" + password = "\u0037\u0066\u0066\u0036\u0030\u0039\u0033\u0066\u0032\u0037\u0033\u0036\u0033\u0037\u0064\u0036\u0037\u0066\u0038\u0030\u0034\u0039\u0062\u0030\u0039\u0038\u0039\u0038\u0066\u0034\u0066\u0034\u0031\u0064\u0062\u0033\u0064\u0033\u0038\u0065" + } + } + } +} + rootProject.name = "ARK Memo" include ':app' From 184e9bdfe9b84e3facb67797fdf74cc3bd915944 Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Fri, 29 Sep 2023 12:47:32 +0200 Subject: [PATCH 04/14] Fix duplicate class --- app/build.gradle | 2 +- .../main/java/space/taran/arkmemo/files/parsers/JsonParser.kt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 83bdf5ee..10f74a2d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,8 +84,8 @@ dependencies { implementation 'androidx.preference:preference-ktx:1.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1' + implementation 'com.github.ARK-Builders:ark-filepicker:c6d66141c1' implementation 'dev.arkbuilders:arklib:0.3.1' - implementation 'com.github.ark-builders:ark-filepicker:ae2c37ea87' implementation 'ch.acra:acra-http:5.9.5' implementation 'ch.acra:acra-dialog:5.9.5' diff --git a/app/src/main/java/space/taran/arkmemo/files/parsers/JsonParser.kt b/app/src/main/java/space/taran/arkmemo/files/parsers/JsonParser.kt index 60e6668d..ab1e5282 100644 --- a/app/src/main/java/space/taran/arkmemo/files/parsers/JsonParser.kt +++ b/app/src/main/java/space/taran/arkmemo/files/parsers/JsonParser.kt @@ -1,5 +1,6 @@ package space.taran.arkmemo.files.parsers +import android.graphics.Path import com.google.gson.Gson import space.taran.arkmemo.models.TextNote @@ -14,4 +15,4 @@ class JsonParser { gson.fromJson(json, TextNote.Content::class.java) } -} \ No newline at end of file +} From 46739590eca0068c8e4b4ba8f298ee303cb32aa4 Mon Sep 17 00:00:00 2001 From: ShubertMunthali Date: Mon, 2 Oct 2023 08:36:19 +0200 Subject: [PATCH 05/14] Light theme --- app/src/main/res/values-night/themes.xml | 18 ------------------ app/src/main/res/values/themes.xml | 2 +- 2 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 app/src/main/res/values-night/themes.xml diff --git a/app/src/main/res/values-night/themes.xml b/app/src/main/res/values-night/themes.xml deleted file mode 100644 index 2f82d9cf..00000000 --- a/app/src/main/res/values-night/themes.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 5a494a66..f2c16aea 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,6 +1,6 @@ -