Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.writeopia.core.folders.repository.folder

import io.writeopia.auth.core.manager.AuthRepository
import io.writeopia.common.utils.NotesNavigation
import io.writeopia.common.utils.collections.merge
import io.writeopia.commonui.dtos.MenuItemUi
Expand All @@ -16,7 +15,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.collections.mapKeys

/**
* UseCase responsible to perform CRUD operations in the Notes (Documents) of the app taking in to
Expand All @@ -26,11 +24,10 @@ class NotesUseCase private constructor(
private val documentRepository: DocumentRepository,
private val notesConfig: ConfigurationRepository,
private val folderRepository: FolderRepository,
private val authRepository: AuthRepository
) {

suspend fun createFolder(name: String, workspaceId: String) {
folderRepository.createFolder(Folder.fromName(name, workspaceId))
suspend fun createFolder(name: String, workspaceId: String, parentId: String) {
folderRepository.createFolder(Folder.fromName(name, workspaceId).copy(parentId = parentId))
}

suspend fun updateFolder(folder: Folder) {
Expand Down Expand Up @@ -317,13 +314,11 @@ class NotesUseCase private constructor(
documentRepository: DocumentRepository,
notesConfig: ConfigurationRepository,
folderRepository: FolderRepository,
authRepository: AuthRepository
): NotesUseCase =
instance ?: NotesUseCase(
documentRepository,
notesConfig,
folderRepository,
authRepository
).also {
instance = it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class FolderSqlDelightDao(database: WriteopiaDb?) : FolderSearch {
folderEntityQueries?.selectFolderById(id)?.executeAsOneOrNull()

suspend fun createFolder(folder: FolderEntity) {
println("createFolder. folder: $folder")

folderEntityQueries?.insert(
id = folder.id,
parent_id = folder.parent_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class SideMenuKmpInjector(
documentRepository,
configurationRepository,
folderRepository,
authCoreInjection.provideAuthRepository()
)

private fun provideWorkspaceApi() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class NotesMenuKmpInjection private constructor(
documentRepository,
configurationRepository,
folderRepository,
authCoreInjection.provideAuthRepository()
)

private fun provideFolderStateController(): FolderStateController =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import io.writeopia.auth.core.manager.AuthRepository
import io.writeopia.common.utils.DISCONNECTED_USER_ID
import io.writeopia.common.utils.NotesNavigation
import io.writeopia.common.utils.NotesNavigationType
import io.writeopia.common.utils.file.FileUtils
import io.writeopia.common.utils.file.SaveImage
import io.writeopia.sdk.models.utils.map
Expand Down Expand Up @@ -114,11 +115,11 @@ internal class ChooseNoteKmpViewModel(
}.stateIn(viewModelScope, SharingStarted.Lazily, ResultData.Loading())
}

private val _user: MutableStateFlow<UserState<WriteopiaUser>> =
private val user: MutableStateFlow<UserState<WriteopiaUser>> =
MutableStateFlow(UserState.Idle())

override val userName: StateFlow<UserState<String>> by lazy {
_user.map { userState ->
user.map { userState ->
userState.map { user ->
user.name
}
Expand All @@ -143,8 +144,8 @@ internal class ChooseNoteKmpViewModel(
.stateIn(viewModelScope, SharingStarted.Lazily, OrderBy.UPDATE)
}

private val _showLocalSyncConfig = MutableStateFlow<ConfigState>(ConfigState.Idle)
override val showLocalSyncConfigState = _showLocalSyncConfig.asStateFlow()
private val _showLocalSyncConfigState = MutableStateFlow<ConfigState>(ConfigState.Idle)
override val showLocalSyncConfigState = _showLocalSyncConfigState.asStateFlow()

private val _editState = MutableStateFlow(false)
override val editState: StateFlow<Boolean> = _editState.asStateFlow()
Expand All @@ -155,11 +156,11 @@ internal class ChooseNoteKmpViewModel(
private val _showSortMenuState = MutableStateFlow(false)
override val showSortMenuState: StateFlow<Boolean> = _showSortMenuState.asStateFlow()

private val _askToDelete = MutableStateFlow(false)
private val askToDelete = MutableStateFlow(false)

override val titlesToDelete: StateFlow<List<String>> =
combine(
_askToDelete,
askToDelete,
selectedNotes,
menuItemsState
) { shouldAsk, selectedIds, itemsState ->
Expand Down Expand Up @@ -253,7 +254,7 @@ internal class ChooseNoteKmpViewModel(

override suspend fun requestUser() {
try {
_user.value = if (authRepository.isLoggedIn()) {
user.value = if (authRepository.isLoggedIn()) {
val user = authRepository.getUser()

if (user.id != DISCONNECTED_USER_ID) {
Expand Down Expand Up @@ -326,7 +327,7 @@ internal class ChooseNoteKmpViewModel(
viewModelScope.launch(Dispatchers.Default) {
notesUseCase.deleteNotes(selected)
clearSelection()
_askToDelete.value = false
askToDelete.value = false
}
}

Expand Down Expand Up @@ -358,7 +359,7 @@ internal class ChooseNoteKmpViewModel(

override fun configureDirectory() {
viewModelScope.launch(Dispatchers.Default) {
_showLocalSyncConfig.value =
_showLocalSyncConfigState.value =
ConfigState.Configure(
path = notesConfig.loadWorkspacePath(getUserId()) ?: "",
syncRequest = SyncRequest.CONFIGURE
Expand Down Expand Up @@ -387,17 +388,17 @@ internal class ChooseNoteKmpViewModel(
}

override fun hideConfigSyncMenu() {
_showLocalSyncConfig.value = ConfigState.Idle
_showLocalSyncConfigState.value = ConfigState.Idle
}

override fun confirmWorkplacePath() {
val path = _showLocalSyncConfig.value.getPath()
val path = _showLocalSyncConfigState.value.getPath()

if (path != null) {
viewModelScope.launch(Dispatchers.Default) {
notesConfig.saveWorkspacePath(path = path, userId = getUserId())

when (_showLocalSyncConfig.value.getSyncRequest()) {
when (_showLocalSyncConfigState.value.getSyncRequest()) {
SyncRequest.WRITE -> {
writeWorkspaceLocally(path)
}
Expand All @@ -409,13 +410,13 @@ internal class ChooseNoteKmpViewModel(
SyncRequest.CONFIGURE, null -> {}
}

_showLocalSyncConfig.value = ConfigState.Idle
_showLocalSyncConfigState.value = ConfigState.Idle
}
}
}

override fun pathSelected(path: String) {
_showLocalSyncConfig.value = _showLocalSyncConfig.value.setPath { path }
_showLocalSyncConfigState.value = _showLocalSyncConfigState.value.setPath { path }
}

override fun onSyncLocallySelected() {
Expand All @@ -427,11 +428,11 @@ internal class ChooseNoteKmpViewModel(
}

override fun requestPermissionToDeleteSelection() {
_askToDelete.value = true
askToDelete.value = true
}

override fun cancelDeletion() {
_askToDelete.value = false
askToDelete.value = false
}

override fun requestInitFlow(flow: () -> Unit) {
Expand Down Expand Up @@ -483,7 +484,13 @@ internal class ChooseNoteKmpViewModel(

override fun newFolder() {
viewModelScope.launch(Dispatchers.Default) {
folderController.addFolder()
val parentId = if (notesNavigation.navigationType == NotesNavigationType.FOLDER) {
notesNavigation.id
} else {
Folder.ROOT_PATH
}

folderController.addFolder(parentId = parentId)
}
}

Expand Down Expand Up @@ -587,7 +594,7 @@ internal class ChooseNoteKmpViewModel(
if (workspacePath != null && FileUtils.folderExists(workspacePath)) {
workspaceFunc(workspacePath)
} else {
_showLocalSyncConfig.value = ConfigState.Configure("", syncRequest)
_showLocalSyncConfigState.value = ConfigState.Configure("", syncRequest)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.StateFlow
interface FolderController {
val selectedNotes: StateFlow<Set<String>>

fun addFolder()
fun addFolder(parentId: String = "root")

fun editFolder(folder: MenuItemUi.FolderUi)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class FolderStateController private constructor(
this.coroutineScope = coroutineScope
}

override fun addFolder() {
override fun addFolder(parentId: String) {
coroutineScope.launch(Dispatchers.Default) {
val workspace = authRepository.getWorkspace() ?: Workspace.disconnectedWorkspace()
notesUseCase.createFolder("Untitled", workspace.id)
notesUseCase.createFolder("Untitled", workspace.id, parentId)
}
}

Expand Down