Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Filtering state is lost across process death #663

Closed
Zhuinden opened this issue Jul 15, 2019 · 5 comments
Closed

[master] Filtering state is lost across process death #663

Zhuinden opened this issue Jul 15, 2019 · 5 comments
Labels

Comments

@Zhuinden
Copy link

Value of _currentFiltering is never persisted to Bundle. If one were to use SavedStateHandle to solve, this, the solution would look like this:

    class TasksViewModel(
        private val tasksRepository: TasksRepository,
        private val savedStateHandle: SavedStateHandle
    ): ViewModel() {
        private val selectedFilter: MutableLiveData<TasksFilterType> = savedStateHandle.getLiveData("filterType")

        val items: LiveData<List<Task>> = selectedFilter.switchMap { filterType ->
            tasksRepository.getTasks(filterType)
        }        
    }

The Repository (or getTaskUseCase, whichever is used) should select the right DAO method to call for getting filtered results from Room DAO

    class TaskRepository(
        private val taskDao: TaskDao
    ) {
        fun getTasks(filterType: TasksFilterType): LiveData<List<Task>> = when(filterType) {
            ALL -> taskDao.getAllTasks()
            ACTIVE_ONLY -> taskDao.getActiveTasks()
            COMPLETED_ONLY -> tasks.getCompletedTasks()
        }
    }

And the DAO should expose LiveData with the correct filters

    @Query("SELECT * FROM Tasks")
    fun getAllTasks(): LiveData<List<Task>>

    @Query("SELECT * FROM Tasks WHERE completed = 1")
    fun getCompletedTasks(): LiveData<List<Task>>

    @Query("SELECT * FROM Tasks WHERE completed = 0")
    fun getActiveTasks(): LiveData<List<Task>>
@JoseAlcerreca
Copy link
Contributor

On SavedStateHandle: good point, will update that.

On filtering in the data layer: obvious thing to do in a real app, but in blueprints we use filtering as business logic to showcase how to do operations off the main thread. Same in the usecases branch. Not ideal, but adding more complexity would make the sample harder to follow.

Many thanks!

@Zhuinden
Copy link
Author

Now with the ViewModel-SavedState module reaching 1.0.0, the filtering can be persisted using SavedStateHandle.

@JoseAlcerreca
Copy link
Contributor

Added in #700 without LiveData and converting enum to ints since apparently there's no support for enums.

@Zhuinden
Copy link
Author

Zhuinden commented Jan 24, 2020

No support for enums? that's odd, enums should (or I would expect them to) fall back to putSerializable. 🤔

If this is intended, then that is quite an unfortunate limitation. One could use the enum name or ordinals I guess but that is still inconvenient.

@JoseAlcerreca
Copy link
Contributor

They are, my bad. I was simulating process death incorrectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants