diff --git a/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt b/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt index e7cfb14..a691607 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/MainActivity.kt @@ -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() @@ -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 @@ -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?) { @@ -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) } } diff --git a/app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt b/app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt index 879249f..a75b98a 100644 --- a/app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt +++ b/app/src/main/java/eu/zderadicka/audioserve/utils/SleepService.kt @@ -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 @@ -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) { @@ -234,7 +242,7 @@ class SleepService() : Service() { nextSound = null } - }) + } } try { if (mediaPlayer?.isPlaying?:false) {