Permalink
Browse files

Make MultiSelectionController a LifecycleObserver

Instead of manually calling through to MultiSelectionController in lifecycle methods, have it handle lifecycle events itself by being registered as a LifecycleObserver.
  • Loading branch information...
ianhanniballake committed Jan 31, 2019
1 parent ee29320 commit dea11e7b3a71aab3b17d19f3740cda58336c306e
@@ -33,6 +33,7 @@ dependencies {
implementation "androidx.recyclerview:recyclerview:$recyclerViewVersion"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
implementation "androidx.exifinterface:exifinterface:$exifInterfaceVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.room:room-coroutines:$roomVersion"
@@ -68,7 +68,6 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
private const val REQUEST_CHOOSE_PHOTOS = 1
private const val REQUEST_CHOOSE_FOLDER = 2
private const val REQUEST_STORAGE_PERMISSION = 3
private const val STATE_SELECTION = "selection"

internal val CHOSEN_PHOTO_DIFF_CALLBACK: DiffUtil.ItemCallback<ChosenPhoto> = object : DiffUtil.ItemCallback<ChosenPhoto>() {
override fun areItemsTheSame(oldItem: ChosenPhoto, newItem: ChosenPhoto): Boolean {
@@ -102,7 +101,7 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
}
private var itemSize = 10

private val multiSelectionController = MultiSelectionController(STATE_SELECTION)
private val multiSelectionController = MultiSelectionController(lifecycle, bundleSavedStateRegistry)

private val placeholderDrawable: ColorDrawable by lazy {
ColorDrawable(ContextCompat.getColor(this,
@@ -125,10 +124,6 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh

private val chosenPhotosAdapter = GalleryAdapter()

init {
bundleSavedStateRegistry.registerSavedStateProvider(STATE_SELECTION, multiSelectionController)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.gallery_activity)
@@ -565,12 +560,6 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
}
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
multiSelectionController.restoreInstanceState(
bundleSavedStateRegistry.consumeRestoredStateForKey(STATE_SELECTION))
}

internal class PhotoViewHolder(val rootView: View) : RecyclerView.ViewHolder(rootView) {
val checkedOverlayView: FrameLayout = rootView.findViewById(R.id.checked_overlay)
val thumbViews = listOf<ImageView>(
@@ -18,24 +18,39 @@ package com.google.android.apps.muzei.util

import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.savedstate.SavedStateRegistry
import java.util.HashSet

/**
* Utilities for storing multiple selection information in collection views.
*/
class MultiSelectionController(private val stateKey: String) : SavedStateRegistry.SavedStateProvider<Bundle> {
class MultiSelectionController(
lifecycle: Lifecycle,
private val bundleSavedStateRegistry: SavedStateRegistry<Bundle>
) : DefaultLifecycleObserver, SavedStateRegistry.SavedStateProvider<Bundle> {

companion object {
private const val STATE_SELECTION = "selection"
}

val selection = HashSet<Long>()
var callbacks: Callbacks? = null

val selectedCount: Int
get() = selection.size

fun restoreInstanceState(savedInstanceState: Bundle?) {
savedInstanceState?.run {
init {
lifecycle.addObserver(this)
bundleSavedStateRegistry.registerSavedStateProvider(STATE_SELECTION, this)
}

override fun onCreate(owner: LifecycleOwner) {
bundleSavedStateRegistry.consumeRestoredStateForKey(STATE_SELECTION)?.run {
selection.clear()
val savedSelection = getLongArray(stateKey)
val savedSelection = getLongArray(STATE_SELECTION)
if (savedSelection?.isNotEmpty() == true) {
for (item in savedSelection) {
selection.add(item)
@@ -46,7 +61,7 @@ class MultiSelectionController(private val stateKey: String) : SavedStateRegistr
callbacks?.onSelectionChanged(true, false)
}

override fun saveState() = bundleOf(stateKey to selection.toLongArray())
override fun saveState() = bundleOf(STATE_SELECTION to selection.toLongArray())

fun toggle(item: Long, fromUser: Boolean) {
if (selection.contains(item)) {

1 comment on commit dea11e7

@16608770879

This comment has been minimized.

Copy link

16608770879 commented on dea11e7 Feb 4, 2019

Please sign in to comment.