Skip to content

Commit

Permalink
feat: disable bookmarks import and export when the database is unava…
Browse files Browse the repository at this point in the history
…ilable
  • Loading branch information
cbeyls committed Jan 11, 2024
1 parent 97ec211 commit 924ed6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,21 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) {
private inner class BookmarksMenuProvider : MenuProvider {
private var filterMenuItem: MenuItem? = null
private var hidePastEventsMenuItem: MenuItem? = null
private var exportMenuItem: MenuItem? = null
private var importMenuItem: MenuItem? = null

var isImportExportEnabled: Boolean = true
set(value) {
field = value
updateMenuItems()
}

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.bookmarks, menu)
filterMenuItem = menu.findItem(R.id.filter)
hidePastEventsMenuItem = menu.findItem(R.id.hide_past_events)
exportMenuItem = menu.findItem(R.id.export_bookmarks)
importMenuItem = menu.findItem(R.id.import_bookmarks)
updateMenuItems()
}

Expand Down Expand Up @@ -139,13 +149,16 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) {
val hidePastEvents = viewModel.hidePastEvents
filterMenuItem?.setIcon(if (hidePastEvents) R.drawable.ic_filter_list_selected_white_24dp else R.drawable.ic_filter_list_white_24dp)
hidePastEventsMenuItem?.isChecked = hidePastEvents
exportMenuItem?.isEnabled = isImportExportEnabled
importMenuItem?.isEnabled = isImportExportEnabled
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

requireActivity().addMenuProvider(BookmarksMenuProvider(), viewLifecycleOwner)
val menuProvider = BookmarksMenuProvider()
requireActivity().addMenuProvider(menuProvider, viewLifecycleOwner)

val adapter = BookmarksAdapter(view.context, multiChoiceHelper)
val holder = RecyclerViewViewHolder(view).apply {
Expand All @@ -169,6 +182,11 @@ class BookmarksListFragment : Fragment(R.layout.recyclerview) {
adapter.roomStatuses = statuses
}
}
launch {
viewModel.isImportExportEnabled.filterNotNull().collect { isEnabled ->
menuProvider.isImportExportEnabled = isEnabled
}
}
launch {
viewModel.bookmarks.filterNotNull().collect { bookmarks ->
adapter.submitList(bookmarks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okio.buffer
Expand Down Expand Up @@ -61,6 +64,13 @@ class BookmarksViewModel @Inject constructor(
bookmarksDao.getBookmarks(minEndTime)
}

val isImportExportEnabled: StateFlow<Boolean?> =
scheduleDao.latestUpdateTime.map { it != null }.stateIn(
scope = viewModelScope,
started = SharingStarted.Lazily,
initialValue = null
)

var hidePastEvents: Boolean
get() = hidePastEventsStateFlow.value == true
set(value) {
Expand Down

0 comments on commit 924ed6f

Please sign in to comment.