Skip to content

Commit 032a865

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Adding folder in right place (#532)
* Adding folder in current place instead of root * ktlint --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent 954b1d7 commit 032a865

7 files changed

Lines changed: 30 additions & 32 deletions

File tree

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/repository/folder/NotesUseCase.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.writeopia.core.folders.repository.folder
22

3-
import io.writeopia.auth.core.manager.AuthRepository
43
import io.writeopia.common.utils.NotesNavigation
54
import io.writeopia.common.utils.collections.merge
65
import io.writeopia.commonui.dtos.MenuItemUi
@@ -16,7 +15,6 @@ import kotlinx.coroutines.flow.combine
1615
import kotlinx.coroutines.flow.map
1716
import kotlinx.datetime.Clock
1817
import kotlinx.datetime.Instant
19-
import kotlin.collections.mapKeys
2018

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

32-
suspend fun createFolder(name: String, workspaceId: String) {
33-
folderRepository.createFolder(Folder.fromName(name, workspaceId))
29+
suspend fun createFolder(name: String, workspaceId: String, parentId: String) {
30+
folderRepository.createFolder(Folder.fromName(name, workspaceId).copy(parentId = parentId))
3431
}
3532

3633
suspend fun updateFolder(folder: Folder) {
@@ -317,13 +314,11 @@ class NotesUseCase private constructor(
317314
documentRepository: DocumentRepository,
318315
notesConfig: ConfigurationRepository,
319316
folderRepository: FolderRepository,
320-
authRepository: AuthRepository
321317
): NotesUseCase =
322318
instance ?: NotesUseCase(
323319
documentRepository,
324320
notesConfig,
325321
folderRepository,
326-
authRepository
327322
).also {
328323
instance = it
329324
}

application/core/persistence_sqldelight/src/commonMain/kotlin/io/writeopia/sqldelight/dao/FolderSqlDelightDao.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ class FolderSqlDelightDao(database: WriteopiaDb?) : FolderSearch {
2222
folderEntityQueries?.selectFolderById(id)?.executeAsOneOrNull()
2323

2424
suspend fun createFolder(folder: FolderEntity) {
25-
println("createFolder. folder: $folder")
26-
2725
folderEntityQueries?.insert(
2826
id = folder.id,
2927
parent_id = folder.parent_id,

application/features/global_shell/src/commonMain/kotlin/io/writeopia/global/shell/di/SideMenuKmpInjector.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class SideMenuKmpInjector(
4848
documentRepository,
4949
configurationRepository,
5050
folderRepository,
51-
authCoreInjection.provideAuthRepository()
5251
)
5352

5453
private fun provideWorkspaceApi() =

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/di/NotesMenuKmpInjection.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class NotesMenuKmpInjection private constructor(
5151
documentRepository,
5252
configurationRepository,
5353
folderRepository,
54-
authCoreInjection.provideAuthRepository()
5554
)
5655

5756
private fun provideFolderStateController(): FolderStateController =

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/viewmodel/ChooseNoteKmpViewModel.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
55
import io.writeopia.auth.core.manager.AuthRepository
66
import io.writeopia.common.utils.DISCONNECTED_USER_ID
77
import io.writeopia.common.utils.NotesNavigation
8+
import io.writeopia.common.utils.NotesNavigationType
89
import io.writeopia.common.utils.file.FileUtils
910
import io.writeopia.common.utils.file.SaveImage
1011
import io.writeopia.sdk.models.utils.map
@@ -114,11 +115,11 @@ internal class ChooseNoteKmpViewModel(
114115
}.stateIn(viewModelScope, SharingStarted.Lazily, ResultData.Loading())
115116
}
116117

117-
private val _user: MutableStateFlow<UserState<WriteopiaUser>> =
118+
private val user: MutableStateFlow<UserState<WriteopiaUser>> =
118119
MutableStateFlow(UserState.Idle())
119120

120121
override val userName: StateFlow<UserState<String>> by lazy {
121-
_user.map { userState ->
122+
user.map { userState ->
122123
userState.map { user ->
123124
user.name
124125
}
@@ -143,8 +144,8 @@ internal class ChooseNoteKmpViewModel(
143144
.stateIn(viewModelScope, SharingStarted.Lazily, OrderBy.UPDATE)
144145
}
145146

146-
private val _showLocalSyncConfig = MutableStateFlow<ConfigState>(ConfigState.Idle)
147-
override val showLocalSyncConfigState = _showLocalSyncConfig.asStateFlow()
147+
private val _showLocalSyncConfigState = MutableStateFlow<ConfigState>(ConfigState.Idle)
148+
override val showLocalSyncConfigState = _showLocalSyncConfigState.asStateFlow()
148149

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

158-
private val _askToDelete = MutableStateFlow(false)
159+
private val askToDelete = MutableStateFlow(false)
159160

160161
override val titlesToDelete: StateFlow<List<String>> =
161162
combine(
162-
_askToDelete,
163+
askToDelete,
163164
selectedNotes,
164165
menuItemsState
165166
) { shouldAsk, selectedIds, itemsState ->
@@ -253,7 +254,7 @@ internal class ChooseNoteKmpViewModel(
253254

254255
override suspend fun requestUser() {
255256
try {
256-
_user.value = if (authRepository.isLoggedIn()) {
257+
user.value = if (authRepository.isLoggedIn()) {
257258
val user = authRepository.getUser()
258259

259260
if (user.id != DISCONNECTED_USER_ID) {
@@ -326,7 +327,7 @@ internal class ChooseNoteKmpViewModel(
326327
viewModelScope.launch(Dispatchers.Default) {
327328
notesUseCase.deleteNotes(selected)
328329
clearSelection()
329-
_askToDelete.value = false
330+
askToDelete.value = false
330331
}
331332
}
332333

@@ -358,7 +359,7 @@ internal class ChooseNoteKmpViewModel(
358359

359360
override fun configureDirectory() {
360361
viewModelScope.launch(Dispatchers.Default) {
361-
_showLocalSyncConfig.value =
362+
_showLocalSyncConfigState.value =
362363
ConfigState.Configure(
363364
path = notesConfig.loadWorkspacePath(getUserId()) ?: "",
364365
syncRequest = SyncRequest.CONFIGURE
@@ -387,17 +388,17 @@ internal class ChooseNoteKmpViewModel(
387388
}
388389

389390
override fun hideConfigSyncMenu() {
390-
_showLocalSyncConfig.value = ConfigState.Idle
391+
_showLocalSyncConfigState.value = ConfigState.Idle
391392
}
392393

393394
override fun confirmWorkplacePath() {
394-
val path = _showLocalSyncConfig.value.getPath()
395+
val path = _showLocalSyncConfigState.value.getPath()
395396

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

400-
when (_showLocalSyncConfig.value.getSyncRequest()) {
401+
when (_showLocalSyncConfigState.value.getSyncRequest()) {
401402
SyncRequest.WRITE -> {
402403
writeWorkspaceLocally(path)
403404
}
@@ -409,13 +410,13 @@ internal class ChooseNoteKmpViewModel(
409410
SyncRequest.CONFIGURE, null -> {}
410411
}
411412

412-
_showLocalSyncConfig.value = ConfigState.Idle
413+
_showLocalSyncConfigState.value = ConfigState.Idle
413414
}
414415
}
415416
}
416417

417418
override fun pathSelected(path: String) {
418-
_showLocalSyncConfig.value = _showLocalSyncConfig.value.setPath { path }
419+
_showLocalSyncConfigState.value = _showLocalSyncConfigState.value.setPath { path }
419420
}
420421

421422
override fun onSyncLocallySelected() {
@@ -427,11 +428,11 @@ internal class ChooseNoteKmpViewModel(
427428
}
428429

429430
override fun requestPermissionToDeleteSelection() {
430-
_askToDelete.value = true
431+
askToDelete.value = true
431432
}
432433

433434
override fun cancelDeletion() {
434-
_askToDelete.value = false
435+
askToDelete.value = false
435436
}
436437

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

484485
override fun newFolder() {
485486
viewModelScope.launch(Dispatchers.Default) {
486-
folderController.addFolder()
487+
val parentId = if (notesNavigation.navigationType == NotesNavigationType.FOLDER) {
488+
notesNavigation.id
489+
} else {
490+
Folder.ROOT_PATH
491+
}
492+
493+
folderController.addFolder(parentId = parentId)
487494
}
488495
}
489496

@@ -587,7 +594,7 @@ internal class ChooseNoteKmpViewModel(
587594
if (workspacePath != null && FileUtils.folderExists(workspacePath)) {
588595
workspaceFunc(workspacePath)
589596
} else {
590-
_showLocalSyncConfig.value = ConfigState.Configure("", syncRequest)
597+
_showLocalSyncConfigState.value = ConfigState.Configure("", syncRequest)
591598
}
592599
}
593600
}

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/viewmodel/FolderController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.StateFlow
88
interface FolderController {
99
val selectedNotes: StateFlow<Set<String>>
1010

11-
fun addFolder()
11+
fun addFolder(parentId: String = "root")
1212

1313
fun editFolder(folder: MenuItemUi.FolderUi)
1414

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/viewmodel/FolderStateController.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ class FolderStateController private constructor(
3535
this.coroutineScope = coroutineScope
3636
}
3737

38-
override fun addFolder() {
38+
override fun addFolder(parentId: String) {
3939
coroutineScope.launch(Dispatchers.Default) {
4040
val workspace = authRepository.getWorkspace() ?: Workspace.disconnectedWorkspace()
41-
notesUseCase.createFolder("Untitled", workspace.id)
41+
notesUseCase.createFolder("Untitled", workspace.id, parentId)
4242
}
4343
}
4444

0 commit comments

Comments
 (0)