Fix separateState use in ItemScreen
This commit is contained in:
parent
0c3c3d8ddb
commit
65a49e7fc5
@ -2,6 +2,7 @@ package com.readrops.app.item
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.drawable.BitmapDrawable
|
import android.graphics.drawable.BitmapDrawable
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
@ -17,10 +18,13 @@ import com.readrops.app.util.Preferences
|
|||||||
import com.readrops.db.Database
|
import com.readrops.db.Database
|
||||||
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 com.readrops.db.entities.account.AccountType
|
||||||
import com.readrops.db.pojo.ItemWithFeed
|
import com.readrops.db.pojo.ItemWithFeed
|
||||||
import com.readrops.db.queries.ItemSelectionQueryBuilder
|
import com.readrops.db.queries.ItemSelectionQueryBuilder
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
@ -29,6 +33,7 @@ import org.koin.core.parameter.parametersOf
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class ItemScreenModel(
|
class ItemScreenModel(
|
||||||
private val database: Database,
|
private val database: Database,
|
||||||
private val itemId: Int,
|
private val itemId: Int,
|
||||||
@ -43,8 +48,16 @@ class ItemScreenModel(
|
|||||||
init {
|
init {
|
||||||
screenModelScope.launch(dispatcher) {
|
screenModelScope.launch(dispatcher) {
|
||||||
database.accountDao().selectCurrentAccount()
|
database.accountDao().selectCurrentAccount()
|
||||||
.collect { account ->
|
.flatMapLatest { account ->
|
||||||
this@ItemScreenModel.account = account!!
|
this@ItemScreenModel.account = account!!
|
||||||
|
|
||||||
|
if (account.accountType == AccountType.FEVER) {
|
||||||
|
get<SharedPreferences>().apply {
|
||||||
|
account.login = getString(account.loginKey, null)
|
||||||
|
account.password = getString(account.passwordKey, null)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
repository = get { parametersOf(account) }
|
repository = get { parametersOf(account) }
|
||||||
|
|
||||||
val query = ItemSelectionQueryBuilder.buildQuery(
|
val query = ItemSelectionQueryBuilder.buildQuery(
|
||||||
@ -53,17 +66,17 @@ class ItemScreenModel(
|
|||||||
)
|
)
|
||||||
|
|
||||||
database.itemDao().selectItemById(query)
|
database.itemDao().selectItemById(query)
|
||||||
.collect { itemWithFeed ->
|
}
|
||||||
mutableState.update {
|
.collect { itemWithFeed ->
|
||||||
it.copy(
|
mutableState.update {
|
||||||
itemWithFeed = itemWithFeed,
|
it.copy(
|
||||||
bottomBarState = BottomBarState(
|
itemWithFeed = itemWithFeed,
|
||||||
isRead = itemWithFeed.item.isRead,
|
bottomBarState = BottomBarState(
|
||||||
isStarred = itemWithFeed.item.isStarred
|
isRead = itemWithFeed.item.isRead,
|
||||||
)
|
isStarred = itemWithFeed.item.isStarred
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,14 +106,12 @@ class ItemScreenModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun setItemReadState(item: Item) {
|
fun setItemReadState(item: Item) {
|
||||||
//TODO support separateState
|
|
||||||
screenModelScope.launch(dispatcher) {
|
screenModelScope.launch(dispatcher) {
|
||||||
repository.setItemReadState(item)
|
repository.setItemReadState(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setItemStarState(item: Item) {
|
fun setItemStarState(item: Item) {
|
||||||
//TODO support separateState
|
|
||||||
screenModelScope.launch(dispatcher) {
|
screenModelScope.launch(dispatcher) {
|
||||||
repository.setItemStarState(item)
|
repository.setItemStarState(item)
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ object ItemSelectionQueryBuilder {
|
|||||||
"pub_date",
|
"pub_date",
|
||||||
"image_link",
|
"image_link",
|
||||||
"author",
|
"author",
|
||||||
"Item.read",
|
|
||||||
"icon_url",
|
"icon_url",
|
||||||
"color",
|
"color",
|
||||||
"read_time",
|
"read_time",
|
||||||
@ -26,8 +25,10 @@ object ItemSelectionQueryBuilder {
|
|||||||
"Folder.name as folder_name"
|
"Folder.name as folder_name"
|
||||||
)
|
)
|
||||||
|
|
||||||
private val SEPARATE_STATE_COLUMNS =
|
private val SEPARATE_STATE_COLUMNS = arrayOf(
|
||||||
arrayOf("case When ItemState.starred = 1 Then 1 else 0 End starred")
|
"case When ItemState.read = 1 Then 1 else 0 End read",
|
||||||
|
"case When ItemState.starred = 1 Then 1 else 0 End starred"
|
||||||
|
)
|
||||||
|
|
||||||
private const val JOIN =
|
private const val JOIN =
|
||||||
"Item Inner Join Feed On Item.feed_id = Feed.id Left Join Folder on Folder.id = Feed.folder_id"
|
"Item Inner Join Feed On Item.feed_id = Feed.id Left Join Folder on Folder.id = Feed.folder_id"
|
||||||
@ -42,7 +43,10 @@ object ItemSelectionQueryBuilder {
|
|||||||
fun buildQuery(itemId: Int, separateState: Boolean): SupportSQLiteQuery {
|
fun buildQuery(itemId: Int, separateState: Boolean): SupportSQLiteQuery {
|
||||||
val tables = if (separateState) JOIN + SEPARATE_STATE_JOIN else JOIN
|
val tables = if (separateState) JOIN + SEPARATE_STATE_JOIN else JOIN
|
||||||
val columns =
|
val columns =
|
||||||
if (separateState) COLUMNS.plus(SEPARATE_STATE_COLUMNS) else COLUMNS.plus("starred")
|
if (separateState) COLUMNS.plus(SEPARATE_STATE_COLUMNS) else COLUMNS + arrayOf(
|
||||||
|
"Item.read",
|
||||||
|
"starred"
|
||||||
|
)
|
||||||
|
|
||||||
return SupportSQLiteQueryBuilder.builder(tables).run {
|
return SupportSQLiteQueryBuilder.builder(tables).run {
|
||||||
columns(columns)
|
columns(columns)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user