Skip to content

Commit

Permalink
list should remember its state
Browse files Browse the repository at this point in the history
  • Loading branch information
y20k committed Dec 13, 2019
1 parent eac9195 commit 3b11aff
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/org/y20k/escapepod/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object Keys {

// keys
const val KEY_DOWNLOAD_WORK_REQUEST: String = "DOWNLOAD_WORK_REQUEST"
const val KEY_SAVE_INSTANCE_STATE_PODCAST_LIST: String = "SAVE_INSTANCE_STATE_PODCAST_LIST"

// custom MediaController commands
const val CMD_RELOAD_PLAYER_STATE: String = "RELOAD_PLAYER_STATE"
Expand Down
33 changes: 31 additions & 2 deletions app/src/main/java/org/y20k/escapepod/PodcastPlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
private var playerServiceConnected: Boolean = false
private var onboarding: Boolean = false
private var playerState: PlayerState = PlayerState()
private var listLayoutState: Parcelable? = null
private var opmlCreatedObserver: FileObserver? = null
private var tempOpmlUriString: String = String()
private val handler: Handler = Handler()
Expand Down Expand Up @@ -125,15 +126,35 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onSaveInstanceState */
override fun onSaveInstanceState(outState: Bundle) {
// save current state of podcast list
listLayoutState = layout.layoutManager.onSaveInstanceState()
outState.putParcelable(Keys.KEY_SAVE_INSTANCE_STATE_PODCAST_LIST, listLayoutState)
// always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(outState)
}


/* Overrides onRestoreInstanceState */
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
// always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState)
// restore state of podcast list
listLayoutState = savedInstanceState.getParcelable(Keys.KEY_SAVE_INSTANCE_STATE_PODCAST_LIST)
}


/* Overrides onResume */
override fun onResume() {
super.onResume()
// assign volume buttons to music volume
volumeControlStream = AudioManager.STREAM_MUSIC
// try to recreate player state
playerState = PreferencesHelper.loadPlayerState(this)
// setup player ui
// setup ui
setupPlayer()
setupList()
// start watching for changes in shared preferences
PreferencesHelper.registerPreferenceChangeListener(this)
// toggle download progress indicator
Expand Down Expand Up @@ -535,7 +556,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
private fun setupPlayer() {
// toggle player visibility
if (layout.togglePlayerVisibility(this, playerState.playbackState)) {
// CASE: player is visible
// CASE: player is visible - update player views
layout.togglePlayButtons(playerState.playbackState)
if (playerState.episodeMediaId.isNotEmpty()) {
val episode: Episode = CollectionHelper.getEpisode(collection, playerState.episodeMediaId)
Expand All @@ -547,6 +568,14 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Sets up state of list podcast list */
private fun setupList() {
if (listLayoutState != null) {
layout.layoutManager.onRestoreInstanceState(listLayoutState)
}
}


/* Starts / pauses playback */
private fun togglePlayback(startPlayback: Boolean, mediaId: String, playbackState: Int) {
playerState.episodeMediaId = mediaId
Expand Down
44 changes: 29 additions & 15 deletions app/src/main/java/org/y20k/escapepod/ui/LayoutHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.y20k.escapepod.ui
import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Parcelable
import android.os.Vibrator
import android.support.v4.media.session.PlaybackStateCompat
import android.view.View
Expand Down Expand Up @@ -55,6 +56,7 @@ data class LayoutHolder(var activity: Activity) {
/* Main class variables */
var swipeRefreshLayout: SwipeRefreshLayout
var recyclerView: RecyclerView
val layoutManager: LinearLayoutManager
private var bottomSheet: ConstraintLayout
private var playerViews: Group
private var upNextViews: Group
Expand Down Expand Up @@ -120,9 +122,12 @@ data class LayoutHolder(var activity: Activity) {
bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
displayTimeRemaining = false

// set up RecyclerView
layoutManager = CustomLayoutManager(activity)
recyclerView.layoutManager = layoutManager
recyclerView.itemAnimator = DefaultItemAnimator()

// set layouts for list and player
setupRecyclerView()
// set layout for player
setupBottomSheet()
}

Expand Down Expand Up @@ -340,19 +345,6 @@ data class LayoutHolder(var activity: Activity) {
}



/* Sets up list of podcasts (RecyclerView) */
private fun setupRecyclerView() {
val layoutManager: LinearLayoutManager = object: LinearLayoutManager(activity) {
override fun supportsPredictiveItemAnimations(): Boolean {
return true
}
}
recyclerView.layoutManager = layoutManager
recyclerView.itemAnimator = DefaultItemAnimator()
}


/* Sets up the player (BottomSheet) */
private fun setupBottomSheet() {
// show / hide the small player
Expand Down Expand Up @@ -406,4 +398,26 @@ data class LayoutHolder(var activity: Activity) {
}
}


/*
* Inner class: custom LinearLayoutManager
*/
private inner class CustomLayoutManager(context: Context): LinearLayoutManager(context, VERTICAL, false) {
var listState: Parcelable? = null
override fun supportsPredictiveItemAnimations(): Boolean {
return true
}
// override fun onLayoutCompleted(state: RecyclerView.State?) {
// if (state == null) scrollToPosition(0)
// }
// override fun onRestoreInstanceState(state: Parcelable?) {
// listState = state
// super.onRestoreInstanceState(state)
// }
}
/*
* End of inner class
*/


}

0 comments on commit 3b11aff

Please sign in to comment.