Skip to content

Commit

Permalink
activities: MainActivity: enhancements
Browse files Browse the repository at this point in the history
* bind service on resume
* if the bound service is cached, avoid rebinding
* use glide to set playback state drawables
  • Loading branch information
uditkarode committed Mar 31, 2020
1 parent dcff36a commit e98bf95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Expand Up @@ -30,10 +30,10 @@ import android.os.IBinder
import android.text.Html
import android.view.TouchDelegate
import android.view.View
import android.view.ViewParent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.HtmlCompat
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.flurry.android.FlurryAgent
import com.google.android.material.bottomnavigation.BottomNavigationView
import io.github.inflationx.calligraphy3.CalligraphyConfig
Expand Down Expand Up @@ -159,21 +159,30 @@ class MainActivity : AppCompatActivity(), Search.SongCallback {

override fun onServiceDisconnected(name: ComponentName) { }
}

bindEvent(BindServiceEvent())
}

@Subscribe
private fun bindEvent(bindServiceEvent: BindServiceEvent){
bindServiceEvent.toString() /* because the IDE doesn't like it unused */
if(Shared.serviceRunning(MusicService::class.java, this@MainActivity)) bindService(Intent(this@MainActivity, MusicService::class.java), serviceConn, Context.BIND_IMPORTANT)
if(!Shared.serviceLinked()){
bindServiceEvent.toString() /* because the IDE doesn't like it unused */
if(Shared.serviceRunning(MusicService::class.java, this@MainActivity))
bindService(Intent(this@MainActivity, MusicService::class.java),
serviceConn, Context.BIND_IMPORTANT)
} else {
mService = Shared.mService
songChange(GetSongChangedEvent())
playPauseEvent(GetPlayPauseEvent(mService!!.mediaPlayer.run {
if(this.isPlaying) SongState.playing
else SongState.paused
}))
}
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun playPauseEvent(playPauseEvent: GetPlayPauseEvent){
playing = playPauseEvent.state == SongState.playing
if(playing) bb_icon.setImageDrawable(getDrawable(R.drawable.pause))
else bb_icon.setImageDrawable(getDrawable(R.drawable.play))
if(playing) Glide.with(this).load(R.drawable.pause).into(bb_icon)
else Glide.with(this).load(R.drawable.play).into(bb_icon)

if(playing) startSeekbarUpdates()
else {
Expand Down Expand Up @@ -243,6 +252,7 @@ class MainActivity : AppCompatActivity(), Search.SongCallback {

override fun onResume() {
super.onResume()
bindEvent(BindServiceEvent())
if(!EventBus.getDefault().isRegistered(this))
EventBus.getDefault().register(this)
}
Expand Down
Expand Up @@ -26,7 +26,6 @@ import android.content.Intent
import android.content.IntentFilter
import android.graphics.BitmapFactory
import android.media.*
import android.media.session.MediaController
import android.media.session.MediaSession
import android.media.session.PlaybackState
import android.os.Binder
Expand Down

0 comments on commit e98bf95

Please sign in to comment.