tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HistoryFragmentExample.kt (2206B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This is example code for the 'Simplified Example' section of
      6 // /docs/architecture-overview.md
      7 class HistoryFragment : Fragment() {
      8 
      9    private val store by storeFragment(HistoryState.initial) { restoredState ->
     10        HistoryStore(
     11            initialState = restoredState,
     12            middleware = listOf(
     13                HistoryNavigationMiddleware(findNavController())
     14                HistoryStorageMiddleware(HistoryStorage()),
     15                HistoryTelemetryMiddleware(),
     16            )
     17        )
     18    }
     19 
     20    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
     21        return ComposeView(requireContext()).apply {
     22            setContent {
     23                HistoryScreen(store)
     24            }
     25        }
     26    }
     27 }
     28 
     29 @Composable
     30 private fun HistoryScreen(store: HistoryStore) {
     31    val state = store.observeAsState(initialValue = HistoryState.initial) { state -> state }
     32    val listState = rememberLazyListState()
     33    LazyColumn(listState) {
     34        if (state.selectedItems.isNotEmpty()) {
     35            HistoryMultiSelectHeader(
     36                onDeleteSelectedClick = {
     37                    store.dispatch(HistoryAction.DeleteItems(state.selectedItems))
     38                }
     39            )
     40        } else {
     41            HistoryHeader(
     42                onDeleteAllClick = { store.dispatch(HistoryAction.DeleteItems(state.items)) }
     43            )
     44        }
     45        items(items = state.displayItems, key = { item -> item.id } ) { item ->
     46            val isSelected = state.selectedItems.find { selectedItem ->
     47                selectdItem == item
     48            }
     49            HistoryItem(
     50                item = item,
     51                isSelected = isSelected,
     52                onClick = { store.dispatch(HistoryAction.OpenItem(item)) },
     53                onLongClick = { store.dispatch(HistoryAction.ToggleItemSelection(item)) },
     54                onDeleteClick = { store.dispatch(HistoryAction.DeleteItems(listOf(item))) },
     55            )
     56        }
     57    }
     58 }