From f08d3eac7189e98481b32d43396303497c83fced Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Mon, 7 Aug 2023 17:01:19 +0200 Subject: [PATCH] Add delete accounts and new account screen tracks in AccountTab --- .../readrops/app/compose/ComposeAppModule.kt | 3 ++ .../app/compose/account/AccountTab.kt | 38 +++++++++++++++++++ .../app/compose/account/AccountViewModel.kt | 29 ++++++++++++++ .../readrops/app/compose/base/TabViewModel.kt | 10 ++--- .../repositories/LocalRSSRepository.kt | 18 ++++++++- .../readrops/db/dao/newdao/NewAccountDao.kt | 5 ++- 6 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt diff --git a/appcompose/src/main/java/com/readrops/app/compose/ComposeAppModule.kt b/appcompose/src/main/java/com/readrops/app/compose/ComposeAppModule.kt index 5a76a6a2..4741a29e 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/ComposeAppModule.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/ComposeAppModule.kt @@ -1,5 +1,6 @@ package com.readrops.app.compose +import com.readrops.app.compose.account.AccountViewModel import com.readrops.app.compose.account.selection.AccountSelectionViewModel import com.readrops.app.compose.feeds.FeedViewModel import com.readrops.app.compose.repositories.BaseRepository @@ -17,6 +18,8 @@ val composeAppModule = module { viewModel { AccountSelectionViewModel(get()) } + viewModel { AccountViewModel(get()) } + // repositories factory { (account: Account) -> diff --git a/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt b/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt index ced0aba9..5164a69d 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/account/AccountTab.kt @@ -1,10 +1,22 @@ package com.readrops.app.compose.account import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.size +import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import cafe.adriel.voyager.navigator.LocalNavigator +import cafe.adriel.voyager.navigator.currentOrThrow import cafe.adriel.voyager.navigator.tab.Tab import cafe.adriel.voyager.navigator.tab.TabOptions +import com.readrops.app.compose.account.selection.AccountSelectionScreen +import org.koin.androidx.compose.getViewModel object AccountTab : Tab { @@ -17,8 +29,34 @@ object AccountTab : Tab { @Composable override fun Content() { + val navigator = LocalNavigator.currentOrThrow + val viewModel = getViewModel() + val closeHome by viewModel.closeHome.collectAsState() + + if (closeHome) { + navigator.replaceAll(AccountSelectionScreen()) + } + Column { Text(text = "Account") + + Spacer(modifier = Modifier.size(16.dp)) + + Row { + Button(onClick = { viewModel.deleteAccount() }) { + Text( + text = "Delete" + ) + } + + Spacer(modifier = Modifier.size(16.dp)) + + Button(onClick = { navigator.push(AccountSelectionScreen()) }) { + Text( + text = "New" + ) + } + } } } diff --git a/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt b/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt new file mode 100644 index 00000000..2f4d6efb --- /dev/null +++ b/appcompose/src/main/java/com/readrops/app/compose/account/AccountViewModel.kt @@ -0,0 +1,29 @@ +package com.readrops.app.compose.account + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.readrops.db.Database +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.update +import kotlinx.coroutines.launch + +class AccountViewModel( + private val database: Database +) : ViewModel() { + + private val _closeHome = MutableStateFlow(false) + val closeHome = _closeHome.asStateFlow() + + + fun deleteAccount() { + viewModelScope.launch(Dispatchers.IO) { + database.newAccountDao() + .deleteAllAccounts() + + _closeHome.update { true } + } + } + +} \ No newline at end of file diff --git a/appcompose/src/main/java/com/readrops/app/compose/base/TabViewModel.kt b/appcompose/src/main/java/com/readrops/app/compose/base/TabViewModel.kt index 7d73073f..05887ce8 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/base/TabViewModel.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/base/TabViewModel.kt @@ -3,10 +3,8 @@ package com.readrops.app.compose.base import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.readrops.app.compose.repositories.BaseRepository -import com.readrops.app.compose.repositories.LocalRSSRepository import com.readrops.db.Database import com.readrops.db.entities.account.Account -import kotlinx.coroutines.async import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent @@ -38,10 +36,12 @@ abstract class TabViewModel( .selectCurrentAccount() .distinctUntilChanged() .collect { account -> - currentAccount = account - repository = get(parameters = { parametersOf(account) }) + if (account != null) { + currentAccount = account + repository = get(parameters = { parametersOf(account) }) - invalidate() + invalidate() + } } } } diff --git a/appcompose/src/main/java/com/readrops/app/compose/repositories/LocalRSSRepository.kt b/appcompose/src/main/java/com/readrops/app/compose/repositories/LocalRSSRepository.kt index de0824c4..61c590ba 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/repositories/LocalRSSRepository.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/repositories/LocalRSSRepository.kt @@ -2,10 +2,14 @@ package com.readrops.app.compose.repositories import com.readrops.api.localfeed.LocalRSSDataSource import com.readrops.api.services.SyncResult +import com.readrops.api.utils.ApiUtils import com.readrops.db.Database import com.readrops.db.entities.Feed import com.readrops.db.entities.Item import com.readrops.db.entities.account.Account +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import okhttp3.Headers import org.jsoup.Jsoup class LocalRSSRepository( @@ -29,8 +33,18 @@ class LocalRSSRepository( } else selectedFeeds for (feed in feeds) { + onUpdate(feed) + + val headers = Headers.Builder() + if (feed.etag != null) { + headers[ApiUtils.IF_NONE_MATCH_HEADER] = feed.etag!! + } + if (feed.lastModified != null) { + headers[ApiUtils.IF_MODIFIED_HEADER] = feed.lastModified!! + } + try { - val pair = dataSource.queryRSSResource(feed.url!!, null) + val pair = dataSource.queryRSSResource(feed.url!!, headers.build()) pair?.let { insertNewItems(it.second, feed) @@ -48,7 +62,7 @@ class LocalRSSRepository( override suspend fun synchronize(): SyncResult = throw NotImplementedError("This method can't be called here") - override suspend fun insertNewFeeds(urls: List) { + override suspend fun insertNewFeeds(urls: List) = withContext(Dispatchers.IO) { for (url in urls) { try { val result = dataSource.queryRSSResource(url, null)!! diff --git a/db/src/main/java/com/readrops/db/dao/newdao/NewAccountDao.kt b/db/src/main/java/com/readrops/db/dao/newdao/NewAccountDao.kt index 9356ac42..b62cef67 100644 --- a/db/src/main/java/com/readrops/db/dao/newdao/NewAccountDao.kt +++ b/db/src/main/java/com/readrops/db/dao/newdao/NewAccountDao.kt @@ -12,5 +12,8 @@ interface NewAccountDao : NewBaseDao { suspend fun selectAccountCount(): Int @Query("Select * From Account Where current_account = 1") - fun selectCurrentAccount(): Flow + fun selectCurrentAccount(): Flow + + @Query("Delete From Account") + suspend fun deleteAllAccounts() } \ No newline at end of file