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
1 change: 1 addition & 0 deletions application/core/auth_core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation(project(":writeopia"))
implementation(project(":writeopia_models"))
implementation(project(":application:core:models"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package io.writeopia.auth.core.manager

import io.writeopia.common.utils.ResultData
import io.writeopia.sdk.models.user.WriteopiaUser
import io.writeopia.sdk.repository.UserRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

interface AuthRepository {
interface AuthRepository : UserRepository {

fun listenForUser(): Flow<WriteopiaUser> = flow { emit(getUser()) }
override fun listenForUser(): Flow<WriteopiaUser> = flow { emit(getUser()) }

suspend fun getUser(): WriteopiaUser
override suspend fun getUser(): WriteopiaUser

suspend fun isLoggedIn(): Boolean

Expand Down
1 change: 1 addition & 0 deletions application/core/documents/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ kotlin {
implementation(project(":plugins:writeopia_serialization"))

implementation(project(":application:core:utils"))
implementation(project(":application:core:auth_core"))
implementation(project(":application:core:models"))
implementation(project(":application:core:common_ui"))
implementation(project(":application:core:persistence_bridge"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.writeopia.core.folders.repository

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 @@ -23,6 +24,7 @@ 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, userId: String) {
Expand All @@ -43,8 +45,12 @@ class NotesUseCase private constructor(
suspend fun updateDocumentById(id: String, documentChange: (Document) -> Document) {
documentRepository.loadDocumentById(id)
?.let(documentChange)
?.let { newDocument -> documentRepository.saveDocumentMetadata(newDocument) }
?.also { documentRepository.refreshDocuments() }
?.let { newDocument ->
documentRepository.saveDocumentMetadata(
newDocument,
authRepository.getUser().id
)
}?.also { documentRepository.refreshDocuments() }
}

suspend fun moveItem(menuItem: MenuItemUi, parentId: String) {
Expand Down Expand Up @@ -142,7 +148,7 @@ class NotesUseCase private constructor(
}

suspend fun saveDocumentDb(document: Document) {
documentRepository.saveDocument(document)
documentRepository.saveDocument(document, authRepository.getUser().id)
documentRepository.refreshDocuments()
}

Expand Down Expand Up @@ -180,7 +186,7 @@ class NotesUseCase private constructor(
}.map { document ->
document.duplicateWithNewIds()
}.forEach { document ->
documentRepository.saveDocument(document)
documentRepository.saveDocument(document, authRepository.getUser().id)
}

documentRepository.refreshDocuments()
Expand Down Expand Up @@ -231,7 +237,7 @@ class NotesUseCase private constructor(
parentId = newFolder.id
)
}.forEach { document ->
documentRepository.saveDocument(document)
documentRepository.saveDocument(document, authRepository.getUser().id)
}

newFolder
Expand Down Expand Up @@ -270,11 +276,13 @@ 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
@@ -1,10 +1,14 @@
package io.writeopia.core.folders.sync

import io.writeopia.auth.core.manager.AuthRepository
import io.writeopia.sdk.models.document.Document
import io.writeopia.sdk.repository.DocumentRepository
import kotlinx.datetime.Clock

class DocumentConflictHandler(private val documentRepository: DocumentRepository) {
class DocumentConflictHandler(
private val documentRepository: DocumentRepository,
private val authRepository: AuthRepository
) {

/**
* Handle conflicts with documents that were updated both locally and in the backend.
Expand All @@ -19,11 +23,13 @@ class DocumentConflictHandler(private val documentRepository: DocumentRepository
externalDocuments: List<Document>
): List<Document> {
val now = Clock.System.now()

// Todo: Implement!! Save external documents and remove localDocuments. A more complex
// handling of conflicts can be implemented in the future.
externalDocuments.forEach { document ->
documentRepository.saveDocument(document.copy(lastSyncedAt = now))
documentRepository.saveDocument(
document.copy(lastSyncedAt = now),
authRepository.getUser().id
)
}

return (localDocuments.toSet() - externalDocuments.toSet()).toList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DocumentsSync(
* This logic is atomic. If it fails, the whole process must be tried again in a future time.
* The sync time of the folder will only be updated with everything works correctly.
*/
suspend fun syncFolder(folderId: String) {
suspend fun syncFolder(folderId: String, userId: String) {
val lastSync = documentRepository.loadDocumentById(folderId)?.lastSyncedAt

// First, receive the documents for the backend.
Expand All @@ -43,7 +43,7 @@ class DocumentsSync(
// If everything ran accordingly, update the sync time of the folder.
documentsNotSent.forEach { document ->
val newDocument = document.copy(lastSyncedAt = now)
documentRepository.saveDocumentMetadata(newDocument)
documentRepository.saveDocumentMetadata(newDocument, userId)
}

documentRepository.refreshDocuments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class OllamaRepository(
}

suspend fun getConfiguredOllamaUrl(id: String = "disconnected_user"): String? =
ollamaDao?.getConfiguration()?.url
ollamaDao?.getConfiguration(id)?.url

suspend fun deleteModel(model: String, url: String): ResultData<Boolean> =
ollamaApi.removeModel(model, url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,22 @@ class UiConfigurationSqlDelightDao(database: WriteopiaDb?) {
)
}

// if (userId == uiConfiguration.user_id) {
if (userId == uiConfiguration.user_id) {
configurationState.value = uiConfiguration
// }
}
}

suspend fun getConfigurationByUserId(userId: String): UiConfigurationEntity? =
uiConfigurationQueries?.selectConfigurationByUserId(
"disconnected_user"
// userId
)?.awaitAsOneOrNull()
uiConfigurationQueries?.selectConfigurationByUserId(userId)?.awaitAsOneOrNull()

fun listenForConfigurationByUserId(
getUserId: suspend () -> String,
userId: String,
coroutineScope: CoroutineScope
): Flow<UiConfigurationEntity?> {
coroutineScope.launch(Dispatchers.Default) {
val id = "disconnected_user"
// getUserId()
this.userId = userId

userId = id
val config = getConfigurationByUserId(id)
coroutineScope.launch(Dispatchers.Default) {
val config = getConfigurationByUserId(userId)
configurationState.value = config
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class UiConfigurationMemoryRepository : UiConfigurationRepository {
}

override fun listenForUiConfiguration(
getUserId: suspend () -> String,
getUserId: String,
coroutineScope: CoroutineScope
): Flow<UiConfiguration?> = uiConfiguration
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface UiConfigurationRepository {
suspend fun updateConfiguration(userId: String, change: (UiConfiguration) -> UiConfiguration)

fun listenForUiConfiguration(
getUserId: suspend () -> String,
getUserId: String,
coroutineScope: CoroutineScope
): Flow<UiConfiguration?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UiConfigurationRepositoryImpl(
}

override fun listenForUiConfiguration(
getUserId: suspend () -> String,
getUserId: String,
coroutineScope: CoroutineScope
): Flow<UiConfiguration?> =
uiConfigurationDao.listenForConfigurationByUserId("disconnected_user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class UiConfigurationSqlDelightRepository internal constructor(
}

override fun listenForUiConfiguration(
getUserId: suspend () -> String,
getUserId: String,
coroutineScope: CoroutineScope
): Flow<UiConfiguration?> =
uiConfigurationDao.listenForConfigurationByUserId(getUserId, coroutineScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch

class UiConfigurationKmpViewModel(
private val uiConfigurationSqlDelightRepository: UiConfigurationRepository
private val uiConfigurationSqlDelightRepository: UiConfigurationRepository,
) : ViewModel(), UiConfigurationViewModel {

override fun listenForColorTheme(
getUserId: suspend () -> String
): StateFlow<ColorThemeOption?> =
uiConfigurationSqlDelightRepository.listenForUiConfiguration(getUserId, viewModelScope)
// Todo: Add support for multiple configurations per user in a later moment
uiConfigurationSqlDelightRepository.listenForUiConfiguration("disconnected_user", viewModelScope)
.map { uiConfiguration ->
uiConfiguration?.colorThemeOption ?: ColorThemeOption.SYSTEM
}.stateIn(viewModelScope, SharingStarted.Lazily, null)
Expand Down
1 change: 1 addition & 0 deletions application/features/account/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(project(":writeopia_models"))
implementation(project(":writeopia"))
implementation(project(":application:core:utils"))
implementation(project(":application:core:auth_core"))
implementation(project(":application:core:theme"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class AccountMenuKmpInjector private constructor() {

private fun provideAccountMenuKmpViewModel(): AccountMenuKmpViewModel =
AccountMenuKmpViewModel(
authManager = AuthCoreInjectionNeo.singleton().provideAuthRepository(),
authRepository = AuthCoreInjectionNeo.singleton().provideAuthRepository()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

internal class AccountMenuKmpViewModel(
private val authManager: AuthRepository,
private val authRepository: AuthRepository,
) : AccountMenuViewModel, ViewModel() {
private val _isLoggedIn: MutableStateFlow<ResultData<Boolean>> by lazy {
Expand All @@ -30,7 +29,7 @@ internal class AccountMenuKmpViewModel(

override fun logout(onLogOutSuccess: () -> Unit) {
viewModelScope.launch {
val result = authManager.logout()
val result = authRepository.logout()

if (result.toBoolean()) {
onLogOutSuccess()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class EditorKmpInjector private constructor(
authRepository: AuthRepository = authCoreInjection.provideAuthRepository(),
writeopiaManager: WriteopiaManager = provideWriteopiaManager()
) = WriteopiaStateManager.create(
userId = { authRepository.getUser().id },
dispatcher = Dispatchers.Default,
writeopiaManager = writeopiaManager,
selectionState = selectionState,
keyboardEventFlow = keyboardEventFlow,
documentRepository = repositoryInjection.provideDocumentRepository()
documentRepository = repositoryInjection.provideDocumentRepository(),
userRepository = authRepository
)

private fun provideNoteEditorViewModel(
Expand All @@ -73,6 +73,7 @@ class EditorKmpInjector private constructor(
keyboardEventFlow = keyboardEventFlow,
copyManager = copyManager,
workspaceConfigRepository = appConfigurationInjector.provideWorkspaceConfigRepository(),
authRepository = authCoreInjection.provideAuthRepository()
)

@Composable
Expand Down
Loading