diff --git a/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt b/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt index f731524f..0e70bb2c 100644 --- a/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt +++ b/app/src/main/java/be/digitalia/fosdem/fragments/BookmarksListFragment.kt @@ -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() } @@ -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 { @@ -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) diff --git a/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt b/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt index d2ca0b98..1b6e3774 100644 --- a/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt +++ b/app/src/main/java/be/digitalia/fosdem/viewmodels/BookmarksViewModel.kt @@ -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 @@ -61,6 +64,13 @@ class BookmarksViewModel @Inject constructor( bookmarksDao.getBookmarks(minEndTime) } + val isImportExportEnabled: StateFlow = + scheduleDao.latestUpdateTime.map { it != null }.stateIn( + scope = viewModelScope, + started = SharingStarted.Lazily, + initialValue = null + ) + var hidePastEvents: Boolean get() = hidePastEventsStateFlow.value == true set(value) {