Add delete accounts and new account screen tracks in AccountTab

This commit is contained in:
Shinokuni 2023-08-07 17:01:19 +02:00
parent c88f6fe2bc
commit f08d3eac71
6 changed files with 95 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package com.readrops.app.compose 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.account.selection.AccountSelectionViewModel
import com.readrops.app.compose.feeds.FeedViewModel import com.readrops.app.compose.feeds.FeedViewModel
import com.readrops.app.compose.repositories.BaseRepository import com.readrops.app.compose.repositories.BaseRepository
@ -17,6 +18,8 @@ val composeAppModule = module {
viewModel { AccountSelectionViewModel(get()) } viewModel { AccountSelectionViewModel(get()) }
viewModel { AccountViewModel(get()) }
// repositories // repositories
factory<BaseRepository> { (account: Account) -> factory<BaseRepository> { (account: Account) ->

View File

@ -1,10 +1,22 @@
package com.readrops.app.compose.account package com.readrops.app.compose.account
import androidx.compose.foundation.layout.Column 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.material3.Text
import androidx.compose.runtime.Composable 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.Tab
import cafe.adriel.voyager.navigator.tab.TabOptions import cafe.adriel.voyager.navigator.tab.TabOptions
import com.readrops.app.compose.account.selection.AccountSelectionScreen
import org.koin.androidx.compose.getViewModel
object AccountTab : Tab { object AccountTab : Tab {
@ -17,8 +29,34 @@ object AccountTab : Tab {
@Composable @Composable
override fun Content() { override fun Content() {
val navigator = LocalNavigator.currentOrThrow
val viewModel = getViewModel<AccountViewModel>()
val closeHome by viewModel.closeHome.collectAsState()
if (closeHome) {
navigator.replaceAll(AccountSelectionScreen())
}
Column { Column {
Text(text = "Account") 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"
)
}
}
} }
} }

View File

@ -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 }
}
}
}

View File

@ -3,10 +3,8 @@ package com.readrops.app.compose.base
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.readrops.app.compose.repositories.BaseRepository import com.readrops.app.compose.repositories.BaseRepository
import com.readrops.app.compose.repositories.LocalRSSRepository
import com.readrops.db.Database import com.readrops.db.Database
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
@ -38,6 +36,7 @@ abstract class TabViewModel(
.selectCurrentAccount() .selectCurrentAccount()
.distinctUntilChanged() .distinctUntilChanged()
.collect { account -> .collect { account ->
if (account != null) {
currentAccount = account currentAccount = account
repository = get(parameters = { parametersOf(account) }) repository = get(parameters = { parametersOf(account) })
@ -45,5 +44,6 @@ abstract class TabViewModel(
} }
} }
} }
}
} }

View File

@ -2,10 +2,14 @@ package com.readrops.app.compose.repositories
import com.readrops.api.localfeed.LocalRSSDataSource import com.readrops.api.localfeed.LocalRSSDataSource
import com.readrops.api.services.SyncResult import com.readrops.api.services.SyncResult
import com.readrops.api.utils.ApiUtils
import com.readrops.db.Database import com.readrops.db.Database
import com.readrops.db.entities.Feed import com.readrops.db.entities.Feed
import com.readrops.db.entities.Item import com.readrops.db.entities.Item
import com.readrops.db.entities.account.Account import com.readrops.db.entities.account.Account
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.Headers
import org.jsoup.Jsoup import org.jsoup.Jsoup
class LocalRSSRepository( class LocalRSSRepository(
@ -29,8 +33,18 @@ class LocalRSSRepository(
} else selectedFeeds } else selectedFeeds
for (feed in feeds) { 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 { try {
val pair = dataSource.queryRSSResource(feed.url!!, null) val pair = dataSource.queryRSSResource(feed.url!!, headers.build())
pair?.let { pair?.let {
insertNewItems(it.second, feed) 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 synchronize(): SyncResult = throw NotImplementedError("This method can't be called here")
override suspend fun insertNewFeeds(urls: List<String>) { override suspend fun insertNewFeeds(urls: List<String>) = withContext(Dispatchers.IO) {
for (url in urls) { for (url in urls) {
try { try {
val result = dataSource.queryRSSResource(url, null)!! val result = dataSource.queryRSSResource(url, null)!!

View File

@ -12,5 +12,8 @@ interface NewAccountDao : NewBaseDao<Account> {
suspend fun selectAccountCount(): Int suspend fun selectAccountCount(): Int
@Query("Select * From Account Where current_account = 1") @Query("Select * From Account Where current_account = 1")
fun selectCurrentAccount(): Flow<Account> fun selectCurrentAccount(): Flow<Account?>
@Query("Delete From Account")
suspend fun deleteAllAccounts()
} }