-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5672 from Bnyro/master
refactor: simplify watch position timer tasks
- Loading branch information
Showing
5 changed files
with
71 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/github/libretube/util/PauseableTimer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.libretube.util | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import java.util.Timer | ||
import java.util.TimerTask | ||
|
||
class PauseableTimer( | ||
private val onTick: () -> Unit, | ||
private val delayMillis: Long = 1000L | ||
) { | ||
val handler: Handler = Handler(Looper.getMainLooper()) | ||
|
||
var timer: Timer? = null | ||
|
||
init { | ||
resume() | ||
} | ||
|
||
fun resume() { | ||
if (timer == null) timer = Timer() | ||
|
||
timer?.scheduleAtFixedRate(object : TimerTask() { | ||
override fun run() { | ||
handler.post(onTick) | ||
} | ||
}, delayMillis, delayMillis) | ||
} | ||
|
||
fun pause() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
|
||
fun destroy() { | ||
timer?.cancel() | ||
timer = null | ||
} | ||
} |