Skip to content

Commit

Permalink
Fix to time displayed on sleep overlay - get correct time after rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Mar 15, 2019
1 parent 52a493e commit 87a06ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 11 additions & 5 deletions app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class MainActivity : AppCompatActivity(),

collection = collectionFromFolderId(folderId)?: collectionFromSearchId(folderId)?: collectionFromModifiedId(folderId)
searchPrefix = if (collection == null || isOffline) null else "${AudioService.SEARCH_PREFIX}${collection}_"
Log.d(LOG_TAG, "Loaded folder ${folderId} in collection ${collection}")
Log.d(LOG_TAG, "Loaded folder $folderId in collection $collection")
this.folderDetails = folderDetails
invalidateOptionsMenu()
updateDrawerMenu()
Expand Down Expand Up @@ -405,7 +405,7 @@ class MainActivity : AppCompatActivity(),
field = value
}
}
private fun onTimerChange(isRunning:Boolean, remains_mins: Int) {
private fun onTimerChange(isRunning:Boolean, remainsMins: Int) {

timerOn = isRunning
val menu = nav_view.menu
Expand All @@ -414,13 +414,18 @@ class MainActivity : AppCompatActivity(),
startTimer.isVisible = !isRunning
stopTimer.isVisible = isRunning
sleepOverlayView.visibility = if (isRunning) View.VISIBLE else View.GONE
val hours = (remains_mins / 60).toString().padStart(2,'0')
val mins = (remains_mins % 60).toString().padStart(2,'0')
sleepCounterView.text = "${hours}:${mins}"
setTimerRemaingTime(remainsMins)


}

@SuppressLint("SetTextI18n")
private fun setTimerRemaingTime(remainsMins: Int) {
val hours = (remainsMins / 60).toString().padStart(2,'0')
val mins = (remainsMins % 60).toString().padStart(2,'0')
sleepCounterView.text = "$hours:$mins"
}

private val timerServiceConnection = object: ServiceConnection {
private var svc: SleepService? = null
override fun onServiceDisconnected(name: ComponentName?) {
Expand All @@ -432,6 +437,7 @@ class MainActivity : AppCompatActivity(),
val svc = (service as SleepService.LocalBinder).service
svc.statusListener = this@MainActivity::onTimerChange
this.svc = svc
setTimerRemaingTime(svc.remainsMins)
}

}
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ fun extendSleepTimer(context: Context) {

class SleepService() : Service() {

private val remains = -1
val remainsMins: Int
get() {
return if (timer?.remains?:-1>= 0) {
timer?.remains?:0
} else {
0
}
}

var statusListener: ((Boolean, Int) -> Unit)? = null
set(v) {
field = v
Expand Down Expand Up @@ -225,7 +233,7 @@ class SleepService() : Service() {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer()
mediaPlayer?.setOnPreparedListener({ mp -> mp.start() })
mediaPlayer?.setOnCompletionListener({ mp ->
mediaPlayer?.setOnCompletionListener{ mp ->
mp.stop()
mp.reset()
if (nextSound!= null) {
Expand All @@ -234,7 +242,7 @@ class SleepService() : Service() {
nextSound = null

}
})
}
}
try {
if (mediaPlayer?.isPlaying?:false) {
Expand Down

0 comments on commit 87a06ff

Please sign in to comment.