# Reference solution: design notes ## Layers - **Repositories** (`repositories/`) own every Android API call: `DeviceFilesRepository` (folders, SAF-only roots, OTG, root, recursive search, child counts), `MediaStoreRecentsRepository`, `MediaStoreStorageRepository` (volumes, `StorageStatsManager`, media store sizes and files) and `ConfigFileListSettings` (sorting, view type, hidden items). The ViewModels see only the interfaces in `FilesRepository.kt` / `StorageRepository.kt`, so the JVM tests run against fakes with no device. - **ViewModels** (`viewmodels/`), one per tab: `ItemsViewModel`, `RecentsViewModel`, `StorageViewModel`. Each exposes one immutable `StateFlow` of a data class (`ItemsUiState`, `RecentsUiState`, `StorageUiState`), a `Flow` of one-off events (errors, refused SAF access) and plain methods for user actions. File-system work runs in `viewModelScope` on an injected `CoroutineDispatcher` (`Dispatchers.IO` in the app, the test dispatcher in tests). - **Pure rules** with no Android dependency: `FileListRules` (sorting identical to `FileDirItem.compareTo`, the search's case and diacritics insensitive matching, hidden and mime type filters), `ItemsListing` (the Files rows: filter, sort, grid divider, folder child counts, search results grouped under folder titles) and `StorageUsageRules` (which file type a mime type counts as). - **Tab views** (`ItemsFragment`, `RecentsFragment`, `StorageFragment`) collect the state while STARTED, render it and forward clicks, selection changes, search text and refreshes. They keep only "what is on screen now" (`renderedListingPath`, `renderedItems`, …) to touch just the views that changed. ## Where the state lives - The ViewModels are scoped to `MainActivity`, so rotation keeps folder, listing, search, selection and scroll positions without reloading. - Process death: the Files tab's folder and search query, the Recents and Storage queries and the expanded storage volumes go into the `SavedStateHandle`; on recreation the ViewModel reloads from them and `MainActivity.restoreTabsState()` reopens the search bar with the query. Listings, results and selection are not persisted: they are reloaded, which the instruction asks for (folder and query). - The selection is a set of paths in the UI state. `ItemsAdapter` reports changes (`selectionChanged`) and, after recreation, `restoreSelection()` restarts the contextual bar from that set once the list is laid out. - Scroll positions are view memory, not UI state: kept per folder (and per folder's search results) in the ViewModel, restored when the same listing is shown again. ## Behaviour preservation - Sorting, the folders-first order, the grid divider and child counts in list view come from the same code paths as before (`FileListRules.comparator` mirrors `FileDirItem.compareTo`; counts are filled in after the listing is shown, as the old asynchronous binding did, and an in-place update keeps selection and scroll). - Search results keep the old grouping (each file under its humanized parent folder) and the highlighted query (`ItemsUiState.highlightedText`). - The overflow menu depends on the listing (e.g. "Temporarily show hidden" / "Stop showing hidden"): the Files tab refreshes it whenever a new listing is rendered, not only when the folder changes. - SAF-only folders (Android/data, Android/obb) still ask for permission first; a refusal returns to the previous folder and shows the same toast. - Deleting removes the items from the listing and the results immediately; the adapter reports the deletion after removing the rows, so the state never shows rows that are gone. ## Trade-offs - One ViewModel per tab rather than a shared one: each tab has its own lifecycle of loading and searching, and the instruction asks for each tab to own its state. - The Storage search filters the media store files loaded once per refresh instead of querying per keystroke; it matches the old behaviour and keeps typing responsive. - Errors from the Android APIs (`IOException`, `SecurityException`, `IllegalArgumentException`, `IllegalStateException`, `SQLException`) are turned into an event and an empty list by `readOrElse`; other exceptions are bugs and are not caught, and cancellation is always passed on. - No new static-analysis suppressions or baseline entries: the tabs' existing `TooManyFunctions` entries still apply; new classes stay under the thresholds by splitting the listing rules out of `ItemsViewModel`.