Skip to content

Commit

Permalink
Add support for multiple accounts on FeedSyncIosWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Jul 29, 2024
1 parent 5a7e93c commit 570a17e
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class DropboxDataSourceIos: DropboxDataSource {
downloadParam: DropboxDownloadParam,
completionHandler: @escaping (DropboxDownloadResult?, Error?) -> Void
) {
// TODO: download direclty in the database file?
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destURL = directoryURL.appendingPathComponent(downloadParam.outputName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import com.prof18.feedflow.feedsync.icloud.ICloudSettings
import com.prof18.feedflow.shared.data.SettingsHelper
import com.prof18.feedflow.shared.domain.DateFormatter
import com.prof18.feedflow.shared.domain.HtmlRetriever
import com.prof18.feedflow.shared.domain.accounts.AccountsRepository
import com.prof18.feedflow.shared.domain.browser.BrowserSettingsRepository
import com.prof18.feedflow.shared.domain.feed.FeedSourceLogoRetriever
import com.prof18.feedflow.shared.domain.feed.FeedUrlRetriever
import com.prof18.feedflow.shared.domain.feed.manager.FeedManagerRepository
import com.prof18.feedflow.shared.domain.feed.retriever.FeedRetrieverRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncAccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncMessageQueue
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncer
Expand Down Expand Up @@ -258,12 +257,6 @@ private val coreModule = module {

singleOf(::FeedSyncMessageQueue)

factory {
FeedSyncAccountsRepository(
dropboxSettings = get(),
)
}

singleOf(::AccountsRepository)

factory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.prof18.feedflow.shared.domain.accounts
package com.prof18.feedflow.shared.domain.feedsync

import com.prof18.feedflow.core.model.SyncAccounts
import com.prof18.feedflow.feedsync.dropbox.DropboxSettings
import com.prof18.feedflow.feedsync.icloud.ICloudSettings
import com.prof18.feedflow.shared.domain.model.CurrentOS
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

internal class AccountsRepository(
private val currentOS: CurrentOS,
Expand Down Expand Up @@ -51,16 +52,26 @@ internal class AccountsRepository(
currentAccountMutableState.value = SyncAccounts.LOCAL
}

private fun restoreAccounts() {
fun getCurrentSyncAccount(): SyncAccounts {
val dropboxSettings = dropboxSettings.getDropboxData()
if (dropboxSettings != null) {
currentAccountMutableState.value = SyncAccounts.DROPBOX
return SyncAccounts.DROPBOX
}
if (currentOS == CurrentOS.Ios) {
val useICloud = icloudSettings.getUseICloud()
if (useICloud) {
currentAccountMutableState.value = SyncAccounts.ICLOUD
return SyncAccounts.ICLOUD
}
}
return SyncAccounts.LOCAL
}

fun isSyncEnabled(): Boolean =
getCurrentSyncAccount() != SyncAccounts.LOCAL

private fun restoreAccounts() {
currentAccountMutableState.update {
getCurrentSyncAccount()
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.datetime.Clock
class FeedSyncRepository internal constructor(
private val syncedDatabaseHelper: SyncedDatabaseHelper,
private val feedSyncWorker: FeedSyncWorker,
private val feedSyncAccountRepository: FeedSyncAccountsRepository,
private val feedSyncAccountRepository: AccountsRepository,
private val feedSyncMessageQueue: FeedSyncMessageQueue,
private val dropboxSettings: DropboxSettings,
private val logger: Logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.prof18.feedflow.shared.presentation

import com.prof18.feedflow.core.model.SyncAccounts
import com.prof18.feedflow.shared.domain.accounts.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.AccountsRepository
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState

class AccountsViewModel internal constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.prof18.feedflow.feedsync.dropbox.DropboxSettings
import com.prof18.feedflow.feedsync.dropbox.DropboxStringCredentials
import com.prof18.feedflow.feedsync.dropbox.getDxCredentialsAsString
import com.prof18.feedflow.shared.domain.DateFormatter
import com.prof18.feedflow.shared.domain.accounts.AccountsRepository
import com.prof18.feedflow.shared.domain.feed.retriever.FeedRetrieverRepository
import com.prof18.feedflow.shared.domain.feedsync.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncMessageQueue
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncRepository
import com.rickclephas.kmp.nativecoroutines.NativeCoroutines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ internal actual fun getPlatformModule(appEnvironment: AppEnvironment): Module =
appEnvironment = appEnvironment,
dropboxSettings = get(),
settingsRepository = get(),
accountsRepository = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prof18.feedflow.shared.domain.feedsync

import co.touchlab.kermit.Logger
import com.prof18.feedflow.core.model.SyncAccounts
import com.prof18.feedflow.core.utils.AppEnvironment
import com.prof18.feedflow.core.utils.DispatcherProvider
import com.prof18.feedflow.feedsync.database.data.SyncedDatabaseHelper.Companion.SYNC_DATABASE_NAME_DEBUG
Expand Down Expand Up @@ -43,6 +44,7 @@ internal class FeedSyncIosWorker(
private val appEnvironment: AppEnvironment,
private val dropboxSettings: DropboxSettings,
private val settingsRepository: SettingsRepository,
private val accountsRepository: AccountsRepository,
) : FeedSyncWorker {
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)

Expand All @@ -59,8 +61,6 @@ internal class FeedSyncIosWorker(
}

private suspend fun performUpload() = withContext(dispatcherProvider.io) {
restoreDropboxClient()

try {
feedSyncer.populateSyncDbIfEmpty()
feedSyncer.updateFeedItemsToSyncDatabase()
Expand All @@ -71,13 +71,7 @@ internal class FeedSyncIosWorker(
emitErrorMessage()
return@withContext
}
val dropboxUploadParam = DropboxUploadParam(
path = "/${getDatabaseName()}.db",
url = databasePath,
)
dropboxDataSource.performUpload(dropboxUploadParam)
dropboxSettings.setLastUploadTimestamp(Clock.System.now().toEpochMilliseconds())
logger.d { "Upload to dropbox successfully" }
accountSpecificUpload(databasePath)
settingsRepository.setIsSyncUploadRequired(false)
emitSuccessMessage()
} catch (e: Exception) {
Expand All @@ -87,24 +81,9 @@ internal class FeedSyncIosWorker(
}

override suspend fun download(): SyncResult = withContext(dispatcherProvider.io) {
val dropboxDownloadParam = DropboxDownloadParam(
path = "/${getDatabaseName()}.db",
outputName = "${getDatabaseName()}.db",
)

restoreDropboxClient()

return@withContext try {
feedSyncer.closeDB()
val result = dropboxDataSource.performDownload(dropboxDownloadParam)
val destinationUrl = result.destinationUrl
if (destinationUrl == null) {
logger.e { "Error downloading database" }
return@withContext SyncResult.Error
}
replaceDatabase(destinationUrl.url)
dropboxSettings.setLastDownloadTimestamp(Clock.System.now().toEpochMilliseconds())
SyncResult.Success
accountSpecificDownload()
} catch (e: Exception) {
logger.e("Download from dropbox failed", e)
SyncResult.Error
Expand Down Expand Up @@ -205,4 +184,60 @@ internal class FeedSyncIosWorker(
}
return false
}

private suspend fun accountSpecificUpload(databasePath: NSURL) =
when (accountsRepository.getCurrentSyncAccount()) {
SyncAccounts.DROPBOX -> {
restoreDropboxClient()
val dropboxUploadParam = DropboxUploadParam(
path = "/${getDatabaseName()}.db",
url = databasePath,
)
dropboxDataSource.performUpload(dropboxUploadParam)
dropboxSettings.setLastUploadTimestamp(Clock.System.now().toEpochMilliseconds())
logger.d { "Upload to dropbox successfully" }
}

SyncAccounts.ICLOUD -> {
// TODO
logger.d { "TODO: Upload to iCloud" }
}

SyncAccounts.LOCAL -> {
// Do nothing
}
}

private suspend fun accountSpecificDownload(): SyncResult {
return when (accountsRepository.getCurrentSyncAccount()) {
SyncAccounts.DROPBOX -> {
val dropboxDownloadParam = DropboxDownloadParam(
path = "/${getDatabaseName()}.db",
outputName = "${getDatabaseName()}.db",
)

restoreDropboxClient()
val result = dropboxDataSource.performDownload(dropboxDownloadParam)
val destinationUrl = result.destinationUrl
if (destinationUrl == null) {
logger.e { "Error downloading database" }
return SyncResult.Error
}
replaceDatabase(destinationUrl.url)
dropboxSettings.setLastDownloadTimestamp(Clock.System.now().toEpochMilliseconds())
SyncResult.Success
}

SyncAccounts.ICLOUD -> {
// TODO
logger.d { "TODO: Download from iCloud" }
SyncResult.Success
}

SyncAccounts.LOCAL -> {
// Do nothing
SyncResult.Success
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.prof18.feedflow.core.model.AccountConnectionUiState
import com.prof18.feedflow.core.model.AccountSyncUIState
import com.prof18.feedflow.feedsync.icloud.ICloudSettings
import com.prof18.feedflow.shared.domain.DateFormatter
import com.prof18.feedflow.shared.domain.accounts.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncMessageQueue
import com.rickclephas.kmp.nativecoroutines.NativeCoroutines
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import com.prof18.feedflow.feedsync.dropbox.DropboxException
import com.prof18.feedflow.feedsync.dropbox.DropboxSettings
import com.prof18.feedflow.feedsync.dropbox.DropboxStringCredentials
import com.prof18.feedflow.shared.domain.DateFormatter
import com.prof18.feedflow.shared.domain.accounts.AccountsRepository
import com.prof18.feedflow.shared.domain.feed.retriever.FeedRetrieverRepository
import com.prof18.feedflow.shared.domain.feedsync.AccountsRepository
import com.prof18.feedflow.shared.domain.feedsync.FeedSyncRepository
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down

0 comments on commit 570a17e

Please sign in to comment.