Skip to content

Commit

Permalink
initial list state fixed (list was not scrolled to top before) & fixe…
Browse files Browse the repository at this point in the history
…d the canBeDeleted check
  • Loading branch information
y20k committed Dec 13, 2019
1 parent 1a16e8f commit e5f8252
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/org/y20k/escapepod/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PlayerService(): MediaBrowserServiceCompat(), Player.EventListener, Corout
override val coroutineContext: CoroutineContext get() = backgroundJob + Dispatchers.Main


/* Overrides onCreate */
/* Overrides onCreate from Service */
override fun onCreate() {
super.onCreate()

Expand Down Expand Up @@ -138,22 +138,22 @@ class PlayerService(): MediaBrowserServiceCompat(), Player.EventListener, Corout
}


/* Overrides onStartCommand */
/* Overrides onStartCommand from Service */
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
MediaButtonReceiver.handleIntent(mediaSession, intent)
return Service.START_STICKY
}


/* Overrides onTaskRemoved */
/* Overrides onTaskRemoved from Service */
override fun onTaskRemoved(rootIntent: Intent) {
super.onTaskRemoved(rootIntent)
stopSelf()
}


/* Overrides onDestroy */
/* Overrides onDestroy from Service */
override fun onDestroy() {
// stop playback
stopPlayback()
Expand All @@ -172,7 +172,7 @@ class PlayerService(): MediaBrowserServiceCompat(), Player.EventListener, Corout
}


/* Overrides onGetRoot */ // todo: implement a hierarchical structure -> https://github.com/googlesamples/android-UniversalMusicPlayer/blob/47da058112cee0b70442bcd0370c1e46e830c66b/media/src/main/java/com/example/android/uamp/media/library/BrowseTree.kt
/* Overrides onGetRoot from MediaBrowserService */ // todo: implement a hierarchical structure -> https://github.com/googlesamples/android-UniversalMusicPlayer/blob/47da058112cee0b70442bcd0370c1e46e830c66b/media/src/main/java/com/example/android/uamp/media/library/BrowseTree.kt
override fun onGetRoot(clientPackageName: String, clientUid: Int, rootHints: Bundle?): BrowserRoot {
// Credit: https://github.com/googlesamples/android-UniversalMusicPlayer (-> MusicService)
// LogHelper.d(TAG, "OnGetRoot: clientPackageName=$clientPackageName; clientUid=$clientUid ; rootHints=$rootHints")
Expand All @@ -199,7 +199,7 @@ class PlayerService(): MediaBrowserServiceCompat(), Player.EventListener, Corout
}


/* Overrides onLoadChildren */
/* Overrides onLoadChildren from MediaBrowserService */
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem>>) {
if (!collectionProvider.isInitialized()) {
// use result.detach to allow calling result.sendResult from another thread:
Expand All @@ -218,7 +218,7 @@ class PlayerService(): MediaBrowserServiceCompat(), Player.EventListener, Corout
}


/* Overrides onPlayerStateChanged (Player.EventListener) */
/* Overrides onPlayerStateChanged from Player.EventListener */
override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
when (playWhenReady) {
// CASE: playWhenReady = true
Expand Down
22 changes: 14 additions & 8 deletions app/src/main/java/org/y20k/escapepod/PodcastPlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
override val coroutineContext: CoroutineContext get() = backgroundJob + Dispatchers.Main


/* Overrides onCreate */
/* Overrides onCreate from Activity*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -118,15 +118,15 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onResume */
/* Overrides onResume from Activity */
public override fun onStart() {
super.onStart()
// connect to PlayerService
mediaBrowser.connect()
}


/* Overrides onSaveInstanceState */
/* Overrides onSaveInstanceState from Activity */
override fun onSaveInstanceState(outState: Bundle) {
// save current state of podcast list
listLayoutState = layout.layoutManager.onSaveInstanceState()
Expand All @@ -136,7 +136,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onRestoreInstanceState */
/* Overrides onRestoreInstanceState from Activity */
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
// always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState)
Expand All @@ -145,7 +145,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onResume */
/* Overrides onResume from Activity */
override fun onResume() {
super.onResume()
// assign volume buttons to music volume
Expand All @@ -162,7 +162,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onPause */
/* Overrides onPause from Activity */
override fun onPause() {
super.onPause()
// save player state
Expand All @@ -177,7 +177,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onStop */
/* Overrides onStop from Activity */
public override fun onStop() {
super.onStop()
// (see "stay in sync with the MediaSession")
Expand All @@ -187,7 +187,7 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onRequestPermissionsResult */
/* Overrides onRequestPermissionsResult from Activity */
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
Expand Down Expand Up @@ -238,6 +238,12 @@ class PodcastPlayerActivity: AppCompatActivity(), CoroutineScope,
}


/* Overrides onDataSetInitialized from CollectionAdapterListener */
override fun onDataSetInitialized() {
layout.recyclerView.scrollToPosition(0)
}


/* Overrides onPlayButtonTapped from CollectionAdapterListener */
override fun onPlayButtonTapped(mediaId: String, playbackState: Int) {
when (playerState.playbackState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class CollectionAdapter(private val activity: Activity) : RecyclerView.Adapter<R
fun onDeleteButtonTapped(episode: Episode)
fun onDeleteAllButtonTapped()
fun onAddNewButtonTapped()
fun onDataSetInitialized()
}


Expand Down Expand Up @@ -398,6 +399,10 @@ class CollectionAdapter(private val activity: Activity) : RecyclerView.Adapter<R
val diffResult = DiffUtil.calculateDiff(CollectionDiffCallback(oldCollection, newCollection), true)
// inform this adapter about the changes
diffResult.dispatchUpdatesTo(this@CollectionAdapter)
// notify activity that data set has been initialized - and scroll to top in activity
if (oldCollection.podcasts.size == 0) {
collectionAdapterListener.onDataSetInitialized()
}
// update collection
collection = newCollection
}
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/org/y20k/escapepod/core/Episode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ data class Episode (@Expose var guid: String = "",
}


/* Return if an eposide has been listened to end */
/* Return if an episode has been listened to end */
fun isFinished(): Boolean {
return playbackPosition >= duration
}


/* Return if an episode has been started listening to */
fun hasBeenStarted(): Boolean {
return playbackPosition > 0L
}


/* Creates a deep copy of an Episode */
fun deepCopy(): Episode {
return Episode(guid = guid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ object CollectionHelper {
if (episode.getMediaId() == PreferencesHelper.loadUpNextMediaId(context)) {
// episode is in Up Next queue
return false
} else if (!episode.isFinished()) {
// episode not finished
} else if (episode.hasBeenStarted() && !episode.isFinished()) {
// episode has been started but not finished
return false
} else if (episode.playbackState != PlaybackStateCompat.STATE_STOPPED && !episode.isFinished()) {
// episode is paused or playing
Expand Down
11 changes: 1 addition & 10 deletions app/src/main/java/org/y20k/escapepod/ui/LayoutHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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 @@ -400,20 +399,12 @@ data class LayoutHolder(var activity: Activity) {


/*
* Inner class: custom LinearLayoutManager
* 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
Expand Down

0 comments on commit e5f8252

Please sign in to comment.