Skip to content

Commit

Permalink
Fix UI state sync issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ismartcoding committed Jun 4, 2024
1 parent f3a1fd1 commit f0790f8
Show file tree
Hide file tree
Showing 50 changed files with 400 additions and 227 deletions.
21 changes: 10 additions & 11 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,17 @@ dependencies {

// https://github.com/google/accompanist/releases
implementation(libs.compose.lifecycle.runtime)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.runtime)
implementation(libs.androidx.ui)
implementation(libs.androidx.foundation)
implementation(libs.androidx.foundation.layout)
implementation(libs.androidx.material3)
implementation(libs.androidx.material.icons.extended)
implementation(libs.compose.activity)
implementation(libs.compose.runtime)
implementation(libs.compose.ui)
implementation(libs.compose.foundation)
implementation(libs.compose.foundation.layout)
implementation(libs.compose.viewmodel)
implementation(libs.compose.material3)
implementation(libs.compose.material.icons.extended)
implementation(libs.accompanist.drawablepainter)
// implementation("androidx.constraintlayout:constraintlayout-compose:1.1.0-alpha12")

// https://developer.android.com/jetpack/androidx/releases/navigation
implementation(libs.androidx.navigation.compose)
implementation(libs.compose.navigation)

releaseImplementation(platform(libs.firebase.bom))
releaseImplementation(libs.firebase.crashlytics.ktx)
Expand Down Expand Up @@ -228,7 +227,7 @@ dependencies {
implementation(libs.coil.compose)
implementation(libs.coil.network.ktor)

implementation(libs.core)
implementation(libs.zxing.core)
implementation(libs.paging.runtime)

implementation(libs.androidx.work.runtime.ktx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object NoteHelper {
fun addOrUpdateAsync(
id: String,
updateItem: DNote.() -> Unit,
): String {
): DNote {
var item = if (id.isNotEmpty()) noteDao.getById(id) else null
var isInsert = false
if (item == null) {
Expand All @@ -113,7 +113,7 @@ object NoteHelper {
noteDao.update(item)
}

return item.id
return item
}

fun trashAsync(ids: Set<String>) {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/ismartcoding/plain/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,14 @@ import com.ismartcoding.plain.receivers.PlugInControlReceiver
import com.ismartcoding.plain.services.PNotificationListenerService
import com.ismartcoding.plain.services.ScreenMirrorService
import com.ismartcoding.plain.ui.audio.AudioPlayerDialog
import com.ismartcoding.plain.ui.extensions.navigate
import com.ismartcoding.plain.ui.extensions.navigatePdf
import com.ismartcoding.plain.ui.nav.navigate
import com.ismartcoding.plain.ui.nav.navigatePdf
import com.ismartcoding.plain.ui.helpers.DialogHelper
import com.ismartcoding.plain.ui.helpers.FilePickHelper
import com.ismartcoding.plain.ui.helpers.WebHelper
import com.ismartcoding.plain.ui.models.MainViewModel
import com.ismartcoding.plain.ui.page.Main
import com.ismartcoding.plain.ui.page.RouteName
import com.ismartcoding.plain.ui.nav.RouteName
import com.ismartcoding.plain.web.HttpServerManager
import com.ismartcoding.plain.web.models.toModel
import com.ismartcoding.plain.web.websocket.EventType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import com.ismartcoding.plain.ui.components.chat.ChatFiles
import com.ismartcoding.plain.ui.components.chat.ChatImages
import com.ismartcoding.plain.ui.components.chat.ChatName
import com.ismartcoding.plain.ui.components.chat.ChatText
import com.ismartcoding.plain.ui.extensions.navigateChatEditText
import com.ismartcoding.plain.ui.extensions.navigateChatText
import com.ismartcoding.plain.ui.nav.navigateChatEditText
import com.ismartcoding.plain.ui.nav.navigateChatText
import com.ismartcoding.plain.ui.helpers.DialogHelper
import com.ismartcoding.plain.ui.models.ChatViewModel
import com.ismartcoding.plain.ui.models.VChat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import androidx.navigation.NavHostController
Expand All @@ -27,12 +26,11 @@ import com.ismartcoding.lib.extensions.isTextFile
import com.ismartcoding.plain.extensions.formatDateTime
import com.ismartcoding.plain.features.file.DFile
import com.ismartcoding.plain.helpers.AppHelper
import com.ismartcoding.plain.helpers.FormatHelper
import com.ismartcoding.plain.ui.base.HorizontalSpace
import com.ismartcoding.plain.ui.base.VerticalSpace
import com.ismartcoding.plain.ui.extensions.navigateOtherFile
import com.ismartcoding.plain.ui.extensions.navigatePdf
import com.ismartcoding.plain.ui.extensions.navigateTextFile
import com.ismartcoding.plain.ui.nav.navigateOtherFile
import com.ismartcoding.plain.ui.nav.navigatePdf
import com.ismartcoding.plain.ui.nav.navigateTextFile
import com.ismartcoding.plain.ui.models.DocsViewModel
import com.ismartcoding.plain.ui.models.select
import com.ismartcoding.plain.ui.theme.PlainTheme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ import com.ismartcoding.plain.ui.theme.listItemTitle
@Composable
fun FeedEntryListItem(
viewModel: FeedEntriesViewModel,
tagsViewModel: TagsViewModel,
index: Int,
m: DFeedEntry,
feed: DFeed?,
tags: List<DTag>,
onClick: () -> Unit,
onLongClick: () -> Unit,
onClickTag: (DTag) -> Unit
) {
Row {
if (viewModel.selectMode.value) {
Expand Down Expand Up @@ -122,14 +122,7 @@ fun FeedEntryListItem(
.align(Alignment.Bottom),
style = MaterialTheme.typography.listItemTag(),
onClick = {
if (viewModel.selectMode.value) {
return@ClickableText
}
viewModel.filterType = FeedEntryFilterType.DEFAULT
viewModel.tag.value = tag
coIO {
viewModel.loadAsync(tagsViewModel)
}
onClickTag(tag)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ import com.ismartcoding.lib.logcat.LogCat
import com.ismartcoding.plain.R
import com.ismartcoding.plain.data.DMediaBucket
import com.ismartcoding.plain.enums.DataType
import com.ismartcoding.plain.helpers.FormatHelper
import com.ismartcoding.plain.ui.base.VerticalSpace
import com.ismartcoding.plain.ui.extensions.navigateImages
import com.ismartcoding.plain.ui.extensions.navigateVideos
import com.ismartcoding.plain.ui.nav.navigateImages
import com.ismartcoding.plain.ui.nav.navigateVideos
import com.ismartcoding.plain.ui.theme.listItemSubtitle
import com.ismartcoding.plain.ui.theme.listItemTitle
import com.ismartcoding.plain.ui.views.mergeimages.CombineBitmapTools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ import com.ismartcoding.plain.ui.theme.listItemTag
@Composable
fun NoteListItem(
viewModel: NotesViewModel,
tagsViewModel: TagsViewModel,
m: DNote,
tags: List<DTag>,
onClick: () -> Unit,
onLongClick: () -> Unit,
onClickTag: (DTag) -> Unit
) {
Row {
if (viewModel.selectMode.value) {
Expand Down Expand Up @@ -91,14 +91,7 @@ fun NoteListItem(
.align(Alignment.Bottom),
style = MaterialTheme.typography.listItemTag(),
onClick = {
if (viewModel.selectMode.value) {
return@ClickableText
}
viewModel.trash.value = false
viewModel.tag.value = tag
coIO {
viewModel.loadAsync(tagsViewModel)
}
onClickTag(tag)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fun TagSelector(
tagsViewModel: TagsViewModel,
tagsMap: Map<String, List<DTagRelation>>,
tagsState: List<DTag>,
onChanged: () -> Unit
) {
val tagIds = remember {
mutableStateListOf<String>()
Expand All @@ -51,6 +52,7 @@ fun TagSelector(
} else {
tagIds.add(tag.id)
}
onChanged()
},
text = tag.name
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ import com.ismartcoding.plain.enums.TextFileType
import com.ismartcoding.plain.features.Permissions
import com.ismartcoding.plain.features.audio.AudioPlayer
import com.ismartcoding.plain.helpers.AppHelper
import com.ismartcoding.plain.helpers.FormatHelper
import com.ismartcoding.plain.ui.audio.AudioPlayerDialog
import com.ismartcoding.plain.ui.components.mediaviewer.previewer.MediaPreviewerState
import com.ismartcoding.plain.ui.components.mediaviewer.previewer.TransformImageView
import com.ismartcoding.plain.ui.components.mediaviewer.previewer.rememberTransformItemState
import com.ismartcoding.plain.ui.extensions.navigateOtherFile
import com.ismartcoding.plain.ui.extensions.navigatePdf
import com.ismartcoding.plain.ui.extensions.navigateTextFile
import com.ismartcoding.plain.ui.nav.navigateOtherFile
import com.ismartcoding.plain.ui.nav.navigatePdf
import com.ismartcoding.plain.ui.nav.navigateTextFile
import com.ismartcoding.plain.ui.models.MediaPreviewData
import com.ismartcoding.plain.ui.models.VChat
import java.io.File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import com.ismartcoding.plain.enums.AppFeatureType
import com.ismartcoding.plain.ui.audio.AudiosDialog
import com.ismartcoding.plain.ui.base.PCard
import com.ismartcoding.plain.ui.base.PIconTextButton
import com.ismartcoding.plain.ui.extensions.navigate
import com.ismartcoding.plain.ui.extensions.navigateImages
import com.ismartcoding.plain.ui.nav.navigate
import com.ismartcoding.plain.ui.nav.navigateImages
import com.ismartcoding.plain.ui.file.FilesDialog
import com.ismartcoding.plain.ui.page.RouteName
import com.ismartcoding.plain.ui.nav.RouteName

@OptIn(ExperimentalLayoutApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import com.ismartcoding.plain.ui.base.PListItem
import com.ismartcoding.plain.ui.base.PMainSwitch
import com.ismartcoding.plain.ui.base.VerticalSpace
import com.ismartcoding.plain.ui.components.WebAddress
import com.ismartcoding.plain.ui.extensions.navigate
import com.ismartcoding.plain.ui.nav.navigate
import com.ismartcoding.plain.ui.models.MainViewModel
import com.ismartcoding.plain.ui.models.VClickText
import com.ismartcoding.plain.ui.page.RouteName
import com.ismartcoding.plain.ui.nav.RouteName

@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun ViewImageBottomSheet(
onDismiss: () -> Unit = {},
onRenamed: () -> Unit = {},
deleteAction: () -> Unit = {},
onTagsChanged: () -> Unit = {},
) {
var showRenameDialog by remember {
mutableStateOf(false)
Expand Down Expand Up @@ -105,6 +106,9 @@ fun ViewImageBottomSheet(
tagsViewModel = tagsViewModel!!,
tagsMap = tagsMap!!,
tagsState = tagsState,
onChanged = {
onTagsChanged()
}
)
VerticalSpace(dp = 24.dp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.saveable
import com.ismartcoding.lib.logcat.LogCat
import com.ismartcoding.plain.R
import com.ismartcoding.plain.enums.DataType
import com.ismartcoding.plain.enums.FeedEntryFilterType
import com.ismartcoding.plain.db.DFeedEntry
import com.ismartcoding.plain.db.DTag
import com.ismartcoding.plain.features.feed.FeedEntryHelper
import com.ismartcoding.plain.features.TagHelper
import com.ismartcoding.plain.features.file.DFile
import com.ismartcoding.plain.features.locale.LocaleHelper
import com.ismartcoding.plain.workers.FeedFetchWorker
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -64,31 +64,35 @@ class FeedEntriesViewModel(private val savedStateHandle: SavedStateHandle) :
offset.value = 0
val query = getQuery()
_itemsFlow.value = FeedEntryHelper.search(query, limit.value, offset.value).toMutableStateList()
refreshTabsAsync(tagsViewModel)
noMore.value = _itemsFlow.value.size < limit.value
showLoading.value = false
}

fun sync() {
FeedFetchWorker.oneTimeRequest(feedId.value)
}

// for updating tags, delete items
fun refreshTabsAsync(tagsViewModel: TagsViewModel) {
tagsViewModel.loadAsync(_itemsFlow.value.map { it.id }.toSet())
total.value = FeedEntryHelper.count(getTotalAllQuery())
totalToday.value = FeedEntryHelper.count(getTotalTodayQuery())
noMore.value = _itemsFlow.value.size < limit.value
tabs.value = listOf(
VTabData(LocaleHelper.getString(R.string.all), "all", total.value),
VTabData(LocaleHelper.getString(R.string.today), "today", totalToday.value),
* tagsViewModel.itemsFlow.value.map { VTabData(it.name, it.id, it.count) }.toTypedArray()
)
showLoading.value = false
}

fun sync() {
FeedFetchWorker.oneTimeRequest(feedId.value)
}

fun delete(ids: Set<String>) {
fun delete(tagsViewModel: TagsViewModel, ids: Set<String>) {
viewModelScope.launch(Dispatchers.IO) {
TagHelper.deleteTagRelationByKeys(
ids,
dataType,
)
FeedEntryHelper.deleteAsync(ids)
total.value = FeedEntryHelper.count(getTotalAllQuery())
totalToday.value = FeedEntryHelper.count(getTotalTodayQuery())
refreshTabsAsync(tagsViewModel)
_itemsFlow.update {
val mutableList = it.toMutableStateList()
mutableList.removeIf { m -> ids.contains(m.id) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,26 @@ class ImagesViewModel(private val savedStateHandle: SavedStateHandle) :
fun loadAsync(context: Context, tagsViewModel: TagsViewModel) {
offset.value = 0
_itemsFlow.value = ImageMediaStoreHelper.search(context, getQuery(), limit.value, offset.value, sortBy.value).toMutableStateList()
refreshTabsAsync(context, tagsViewModel)
noMore.value = _itemsFlow.value.size < limit.value
showLoading.value = false
}

fun refreshTabsAsync(context: Context, tagsViewModel: TagsViewModel) {
tagsViewModel.loadAsync(_itemsFlow.value.map { it.id }.toSet())
total.value = ImageMediaStoreHelper.count(context, getTotalQuery())
noMore.value = _itemsFlow.value.size < limit.value
tabs.value = listOf(
VTabData(LocaleHelper.getString(R.string.all), "all", total.value),
* tagsViewModel.itemsFlow.value.map { VTabData(it.name, it.id, it.count) }.toTypedArray()
)
showLoading.value = false
}

fun delete(context: Context, ids: Set<String>) {
fun delete(context: Context, tagsViewModel: TagsViewModel, ids: Set<String>) {
viewModelScope.launch(Dispatchers.IO) {
DialogHelper.showLoading()
TagHelper.deleteTagRelationByKeys(ids, dataType)
ImageMediaStoreHelper.deleteRecordsAndFilesByIds(context, ids)
refreshTabsAsync(context, tagsViewModel)
DialogHelper.hideLoading()
_itemsFlow.update {
it.toMutableStateList().apply {
Expand Down
Loading

0 comments on commit f0790f8

Please sign in to comment.