mirror of https://github.com/readrops/Readrops.git
Add delete accounts and new account screen tracks in AccountTab
This commit is contained in:
parent
c88f6fe2bc
commit
f08d3eac71
|
@ -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<BaseRepository> { (account: Account) ->
|
||||
|
|
|
@ -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<AccountViewModel>()
|
||||
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"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String>) {
|
||||
override suspend fun insertNewFeeds(urls: List<String>) = withContext(Dispatchers.IO) {
|
||||
for (url in urls) {
|
||||
try {
|
||||
val result = dataSource.queryRSSResource(url, null)!!
|
||||
|
|
|
@ -12,5 +12,8 @@ interface NewAccountDao : NewBaseDao<Account> {
|
|||
suspend fun selectAccountCount(): Int
|
||||
|
||||
@Query("Select * From Account Where current_account = 1")
|
||||
fun selectCurrentAccount(): Flow<Account>
|
||||
fun selectCurrentAccount(): Flow<Account?>
|
||||
|
||||
@Query("Delete From Account")
|
||||
suspend fun deleteAllAccounts()
|
||||
}
|
Loading…
Reference in New Issue