Skip to content

Commit

Permalink
Creating NotesConfigurationRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
leandroBorgesFerreira committed Jul 8, 2023
1 parent c83d4bc commit 7478a98
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.SharedPreferences
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import br.com.leandroferreira.editor.NoteDetailsViewModel
import br.com.leandroferreira.note_menu.data.usecase.NotesConfigurationRepository
import br.com.leandroferreira.note_menu.data.usecase.NotesUseCase
import br.com.leandroferreira.note_menu.viewmodel.ChooseNoteViewModel
import com.github.leandroborgesferreira.storyteller.manager.DocumentRepository
Expand All @@ -24,10 +25,15 @@ class NotesInjection(
database.storyUnitDao()
)

private fun provideNotesConfigurationRepository(): NotesConfigurationRepository =
NotesConfigurationRepository(sharedPreferences)

private fun provideNotesUseCase(
documentRepository: DocumentRepository = provideDocumentRepository()
documentRepository: DocumentRepository = provideDocumentRepository(),
notesConfigurationRepository: NotesConfigurationRepository =
provideNotesConfigurationRepository()
): NotesUseCase {
return NotesUseCase(documentRepository, sharedPreferences)
return NotesUseCase(documentRepository, notesConfigurationRepository)
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package br.com.leandroferreira.note_menu.data.usecase

import android.content.SharedPreferences
import br.com.leandroferreira.note_menu.viewmodel.NotesArrangement
import com.github.leandroborgesferreira.storyteller.persistence.sorting.OrderBy
import com.github.leandroborgesferreira.storyteller.persistence.sorting.toEntityField

/**
* This class is responsible to keep the information of the preferences or the user about the
* notes, like orderBy (creation, last edition, name...) and arrangement (cards, list...).
*/
class NotesConfigurationRepository(
private val sharedPreferences: SharedPreferences
) {

fun saveDocumentArrangementPref(arrangement: NotesArrangement) {
sharedPreferences.edit()
.run { putString(ARRANGE_PREFERENCE, arrangement.type) }
.commit()
}

fun saveDocumentSortingPref(orderBy: OrderBy) {
sharedPreferences.edit()
.run { putString(ORDER_BY_PREFERENCE, orderBy.type.toEntityField()) }
.commit()
}

fun arrangementPref(): String =
sharedPreferences
.getString(ARRANGE_PREFERENCE, NotesArrangement.GRID.type)
?: NotesArrangement.GRID.type

fun getOrderPreference(): String? =
sharedPreferences
.getString(ORDER_BY_PREFERENCE, OrderBy.CREATE.type.toEntityField())


companion object {
private const val ORDER_BY_PREFERENCE = "order_by_preference"
private const val ARRANGE_PREFERENCE = "arrange_preference"
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
package br.com.leandroferreira.note_menu.data.usecase

import android.content.Context
import android.content.SharedPreferences
import br.com.leandroferreira.note_menu.data.supermarketList
import br.com.leandroferreira.note_menu.data.travelHistory
import br.com.leandroferreira.note_menu.viewmodel.NotesArrangement
import com.github.leandroborgesferreira.storyteller.manager.DocumentRepository
import com.github.leandroborgesferreira.storyteller.model.document.Document
import com.github.leandroborgesferreira.storyteller.persistence.repository.DocumentRepositoryImpl
import com.github.leandroborgesferreira.storyteller.persistence.sorting.OrderBy
import com.github.leandroborgesferreira.storyteller.persistence.sorting.toEntityField
import java.util.Date
import java.util.UUID

/**
* UseCase responsible to perform CRUD operations in the Notes (Documents) of the app.
* UseCase responsible to perform CRUD operations in the Notes (Documents) of the app taking in to
* consideration the configuration desired in the app.
*/
class NotesUseCase(
private val documentRepository: DocumentRepository,
private val sharedPreferences: SharedPreferences
private val notesConfig: NotesConfigurationRepository
) {

//Todo: Separate the preferences into another Repository
fun saveDocumentArrangementPref(arrangement: NotesArrangement) {
sharedPreferences.edit()
.run { putString(ARRANGE_PREFERENCE, arrangement.type) }
.commit()
}

//Todo: Separate the preferences into another Repository
fun saveDocumentSortingPref(orderBy: OrderBy) {
sharedPreferences.edit()
.run { putString(ORDER_BY_PREFERENCE, orderBy.type.toEntityField()) }
.commit()
}

//Todo: Separate the preferences into another Repository
fun arrangementPref(): String =
sharedPreferences
.getString(ARRANGE_PREFERENCE, NotesArrangement.GRID.type)
?: NotesArrangement.GRID.type

suspend fun loadDocuments(): List<Document> =
sharedPreferences
.getString(ORDER_BY_PREFERENCE, OrderBy.CREATE.type.toEntityField())
notesConfig.getOrderPreference()
?.let { orderBy -> documentRepository.loadDocuments(orderBy) }!!

suspend fun mockData(context: Context) {
Expand All @@ -67,9 +42,4 @@ class NotesUseCase(
)
)
}

companion object {
private const val ORDER_BY_PREFERENCE = "order_by_preference"
private const val ARRANGE_PREFERENCE = "arrange_preference"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package br.com.leandroferreira.note_menu.viewmodel
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import br.com.leandroferreira.note_menu.data.usecase.NotesConfigurationRepository
import br.com.leandroferreira.utils.ResultData
import br.com.leandroferreira.note_menu.data.usecase.NotesUseCase
import br.com.leandroferreira.note_menu.extensions.toUiCard
Expand All @@ -17,6 +18,7 @@ import kotlinx.coroutines.launch

class ChooseNoteViewModel(
private val notesUseCase: NotesUseCase,
private val notesConfig: NotesConfigurationRepository,
private val previewParser: PreviewParser = PreviewParser()
) : ViewModel() {

Expand Down Expand Up @@ -55,7 +57,7 @@ class ChooseNoteViewModel(
document.toUiCard(previewParser)
}

_notesArrangement.value = NotesArrangement.fromString(notesUseCase.arrangementPref())
_notesArrangement.value = NotesArrangement.fromString(notesConfig.arrangementPref())
_documentsState.value = ResultData.Complete(data)
} catch (e: Exception) {
_documentsState.value = ResultData.Error(e)
Expand All @@ -76,18 +78,18 @@ class ChooseNoteViewModel(
}

fun listArrangementSelected() {
notesUseCase.saveDocumentArrangementPref(NotesArrangement.LIST)
notesConfig.saveDocumentArrangementPref(NotesArrangement.LIST)
_notesArrangement.value = NotesArrangement.LIST
}

fun gridArrangementSelected() {
notesUseCase.saveDocumentArrangementPref(NotesArrangement.GRID)
notesConfig.saveDocumentArrangementPref(NotesArrangement.GRID)
_notesArrangement.value = NotesArrangement.GRID
}

fun sortingSelected(orderBy: OrderBy) {
viewModelScope.launch {
notesUseCase.saveDocumentSortingPref(orderBy)
notesConfig.saveDocumentSortingPref(orderBy)
refreshDocuments()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package com.github.leandroborgesferreira.storyteller.manager
import com.github.leandroborgesferreira.storyteller.model.document.Document
import com.github.leandroborgesferreira.storyteller.model.story.StoryStep

//Todo: Add the methods from DocumentRepositoryImpl
/**
* DocumentRepository is the repository for using simple CRUD operations in [Document].
* The implementations of this interface shouldn't control order (sorting) or oder configurations,
* those need to be passed as parameters.
*/
interface DocumentRepository {

suspend fun loadDocuments(orderBy: String): List<Document>
Expand Down

0 comments on commit 7478a98

Please sign in to comment.