From 2d21e093f217b4253162e2ff9ae5cbcbec7726b4 Mon Sep 17 00:00:00 2001 From: Damian Mazurkiewicz Date: Wed, 28 Dec 2016 21:06:45 +0000 Subject: [PATCH] Starting Stretching (#49) --- .../com/bodyweight/fitness/Constants.kt | 5 + .../fitness/adapter/ToolbarSpinnerAdapter.kt | 26 +- .../com/bodyweight/fitness/model/Routine.kt | 4 + .../bodyweight/fitness/model/RoutineJson.kt | 2 + .../fitness/stream/RoutineStream.kt | 5 + .../com/bodyweight/fitness/stream/Stream.kt | 6 + .../bodyweight/fitness/utils/Preferences.kt | 65 ++++ .../fitness/view/HomeViewPresenter.kt | 28 ++ .../progress/ProgressGeneralViewPresenter.kt | 2 +- .../view/workout/NavigationViewPresenter.kt | 72 ++++- .../view/workout/RestTimerViewPresenter.kt | 180 ++++++++++++ .../main/res/drawable/action_close_light.xml | 14 + app/src/main/res/layout-port/view_timer.xml | 75 +++++ app/src/main/res/layout/view_home.xml | 56 ++++ app/src/main/res/layout/view_timer.xml | 61 ++++ ...odyweight_fitness_recommended_routine.json | 2 + .../molding_mobility_flexibility_routine.json | 2 + ...starting_stretching_butterfly_beginner.mp4 | Bin 0 -> 42822 bytes ...ting_stretching_butterfly_intermediate.mp4 | Bin 0 -> 89170 bytes .../res/raw/starting_stretching_camel.mp4 | Bin 0 -> 56208 bytes ...arting_stretching_flexibility_routine.json | 277 ++++++++++++++++++ ...tarting_stretching_full_squat_beginner.mp4 | Bin 0 -> 44290 bytes ...ing_stretching_full_squat_intermediate.mp4 | Bin 0 -> 46473 bytes .../raw/starting_stretching_glute_bridge.mp4 | Bin 0 -> 43957 bytes ...ing_stretching_kneeling_lunge_beginner.mp4 | Bin 0 -> 73333 bytes ...stretching_kneeling_lunge_intermediate.mp4 | Bin 0 -> 40282 bytes ...arting_stretching_lying_twist_beginner.mp4 | Bin 0 -> 601071 bytes ...ng_stretching_lying_twist_intermediate.mp4 | Bin 0 -> 569802 bytes ...ng_stretching_rear_hand_clasp_beginner.mp4 | Bin 0 -> 46141 bytes ...stretching_shoulder_extension_advanced.mp4 | Bin 0 -> 45974 bytes ...stretching_shoulder_extension_beginner.mp4 | Bin 0 -> 209050 bytes ...tching_shoulder_extension_intermediate.mp4 | Bin 0 -> 35627 bytes ...ting_stretching_standing_pike_beginner.mp4 | Bin 0 -> 134526 bytes ..._stretching_standing_pike_intermediate.mp4 | Bin 0 -> 161313 bytes ...ing_underarm_shoulder_stretch_advanced.mp4 | Bin 0 -> 216573 bytes ...ing_underarm_shoulder_stretch_beginner.mp4 | Bin 0 -> 37672 bytes ...underarm_shoulder_stretch_intermediate.mp4 | Bin 0 -> 40939 bytes app/src/main/res/values/strings.xml | 16 + app/src/main/res/xml/settings.xml | 41 ++- 39 files changed, 928 insertions(+), 11 deletions(-) create mode 100644 app/src/main/kotlin/com/bodyweight/fitness/view/workout/RestTimerViewPresenter.kt create mode 100644 app/src/main/res/drawable/action_close_light.xml create mode 100644 app/src/main/res/raw/starting_stretching_butterfly_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_butterfly_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_camel.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_flexibility_routine.json create mode 100644 app/src/main/res/raw/starting_stretching_full_squat_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_full_squat_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_glute_bridge.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_kneeling_lunge_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_kneeling_lunge_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_lying_twist_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_lying_twist_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_rear_hand_clasp_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_shoulder_extension_advanced.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_shoulder_extension_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_shoulder_extension_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_standing_pike_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_standing_pike_intermediate.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_underarm_shoulder_stretch_advanced.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_underarm_shoulder_stretch_beginner.mp4 create mode 100644 app/src/main/res/raw/starting_stretching_underarm_shoulder_stretch_intermediate.mp4 diff --git a/app/src/main/kotlin/com/bodyweight/fitness/Constants.kt b/app/src/main/kotlin/com/bodyweight/fitness/Constants.kt index a3f08140..22c8c886 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/Constants.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/Constants.kt @@ -17,4 +17,9 @@ object Constants { val preferencesTimerKey = "PREFERENCE_TIMER_KEY_" val preferencesNumberOfRepsKey = "PREFERENCE_NUMBER_OF_REPS_KEY_" val preferencesIntroductionShown = "PREFERENCE_INTRODUCTION_SHOWN" + val preferencesShowRestTimer = "PREFERENCE_SHOW_REST_TIMER" + val preferencesRestTimerDefaultSeconds = "PREFERENCE_REST_TIMER_DEFAULT_SECONDS" + val preferencesShowRestTimerAfterWarmup = "PREFERENCE_SHOW_REST_TIMER_WARMUP_EXERCISES" + val preferencesShowRestTimerAfterBodylineDrills = "PREFERENCE_SHOW_REST_TIMER_BODYLINE_DRILLS" + val preferencesShowRestTimerAfterFlexibilityExercises = "PREFERENCE_SHOW_REST_TIMER_FLEXIBILITY_ROUTINES" } \ No newline at end of file diff --git a/app/src/main/kotlin/com/bodyweight/fitness/adapter/ToolbarSpinnerAdapter.kt b/app/src/main/kotlin/com/bodyweight/fitness/adapter/ToolbarSpinnerAdapter.kt index f06bc6ac..14cee871 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/adapter/ToolbarSpinnerAdapter.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/adapter/ToolbarSpinnerAdapter.kt @@ -15,14 +15,32 @@ class ToolbarSpinnerAdapter : BaseAdapter() { val routines: List init { - if (RoutineStream.routine.routineId == "routine0") { + val routineId = RoutineStream.routine.routineId + + if (routineId == "routine0") { + routines = listOf( + SpinnerRoutine(0, "Bodyweight Fitness", "Recommended Routine"), + SpinnerRoutine(1, "Starting Stretching", "Flexibility Routine"), + SpinnerRoutine(2, "Molding Mobility", "Flexibility Routine") + ) + } else if (routineId == "e73593f4-ee17-4b9b-912a-87fa3625f63d") { routines = listOf( + SpinnerRoutine(2, "Molding Mobility", "Flexibility Routine"), SpinnerRoutine(0, "Bodyweight Fitness", "Recommended Routine"), - SpinnerRoutine(1, "Molding Mobility", "Flexibility Routine")) + SpinnerRoutine(1, "Starting Stretching", "Flexibility Routine") + ) + } else if (routineId == "d8a722a0-fae2-4e7e-a751-430348c659fe") { + routines = listOf( + SpinnerRoutine(1, "Starting Stretching", "Flexibility Routine"), + SpinnerRoutine(0, "Bodyweight Fitness", "Recommended Routine"), + SpinnerRoutine(2, "Molding Mobility", "Flexibility Routine") + ) } else { routines = listOf( - SpinnerRoutine(1, "Molding Mobility", "Flexibility Routine"), - SpinnerRoutine(0, "Bodyweight Fitness", "Recommended Routine")) + SpinnerRoutine(0, "Bodyweight Fitness", "Recommended Routine"), + SpinnerRoutine(1, "Starting Stretching", "Flexibility Routine"), + SpinnerRoutine(2, "Molding Mobility", "Flexibility Routine") + ) } } diff --git a/app/src/main/kotlin/com/bodyweight/fitness/model/Routine.kt b/app/src/main/kotlin/com/bodyweight/fitness/model/Routine.kt index 91cd732b..5296cc89 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/model/Routine.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/model/Routine.kt @@ -13,6 +13,8 @@ class Routine(JSONRoutine: JSONRoutine) : Serializable { var routineId: String = "routine0" var title: String = "" var subtitle: String = "" + var shortDescription: String = "" + var url: String = "" val categories = ArrayList() val sections = ArrayList
() @@ -24,6 +26,8 @@ class Routine(JSONRoutine: JSONRoutine) : Serializable { routineId = JSONRoutine.routineId title = JSONRoutine.title subtitle = JSONRoutine.subtitle + shortDescription = JSONRoutine.shortDescription + url = JSONRoutine.url var currentCategory: Category? = null var currentSection: Section? = null diff --git a/app/src/main/kotlin/com/bodyweight/fitness/model/RoutineJson.kt b/app/src/main/kotlin/com/bodyweight/fitness/model/RoutineJson.kt index 12c1626a..f5b4b784 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/model/RoutineJson.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/model/RoutineJson.kt @@ -6,6 +6,8 @@ class JSONRoutine { var routineId: String = "" var title: String = "" var subtitle: String = "" + var shortDescription: String = "" + var url: String = "" var routine = ArrayList() val size: Int by lazy { diff --git a/app/src/main/kotlin/com/bodyweight/fitness/stream/RoutineStream.kt b/app/src/main/kotlin/com/bodyweight/fitness/stream/RoutineStream.kt index 2d898b12..14674672 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/stream/RoutineStream.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/stream/RoutineStream.kt @@ -39,6 +39,8 @@ object RoutineStream { var routine: Routine = if (Preferences.defaultRoutine == "routine0") { JsonRoutineLoader().getRoutine(R.raw.bodyweight_fitness_recommended_routine) + } else if(Preferences.defaultRoutine == "d8a722a0-fae2-4e7e-a751-430348c659fe") { + JsonRoutineLoader().getRoutine(R.raw.starting_stretching_flexibility_routine) } else { JsonRoutineLoader().getRoutine(R.raw.molding_mobility_flexibility_routine) } @@ -72,6 +74,9 @@ object RoutineStream { routine = JsonRoutineLoader().getRoutine(R.raw.bodyweight_fitness_recommended_routine) } 1 -> { + routine = JsonRoutineLoader().getRoutine(R.raw.starting_stretching_flexibility_routine) + } + 2 -> { routine = JsonRoutineLoader().getRoutine(R.raw.molding_mobility_flexibility_routine) } } diff --git a/app/src/main/kotlin/com/bodyweight/fitness/stream/Stream.kt b/app/src/main/kotlin/com/bodyweight/fitness/stream/Stream.kt index cfa81e40..b95b433f 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/stream/Stream.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/stream/Stream.kt @@ -31,6 +31,7 @@ object Stream { private val menuSubject = PublishSubject.create() private val drawerSubject = PublishSubject.create() + private val restTimerSubject = PublishSubject.create() private val loggedSecondsSubject = PublishSubject.create() private val loggedSetRepsSubject = PublishSubject.create() @@ -46,6 +47,7 @@ object Stream { * Observables that should be re-emitted should be functions rather than values. */ val menuObservable: Observable get() = menuSubject + val restTimerObservable: Observable get() = restTimerSubject val loggedSecondsObservable: Observable get() = loggedSecondsSubject val loggedSetRepsObservable: Observable get() = loggedSetRepsSubject @@ -78,6 +80,10 @@ object Stream { menuSubject.onNext(toolbarMenuItemId) } + fun setRestTimer() { + restTimerSubject.onNext(0) + } + fun setDrawer(drawerMenuItemId: Int) { currentDrawerId = drawerMenuItemId diff --git a/app/src/main/kotlin/com/bodyweight/fitness/utils/Preferences.kt b/app/src/main/kotlin/com/bodyweight/fitness/utils/Preferences.kt index cd344bd1..fae0a0ad 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/utils/Preferences.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/utils/Preferences.kt @@ -40,6 +40,71 @@ object Preferences { .commit() } + var showRestTimer: Boolean + get() { + return getSharedPreferences() + .getBoolean(Constants.preferencesShowRestTimer, true) + } + + set(value) { + getSharedPreferences() + .edit() + .putBoolean(Constants.preferencesShowRestTimer, value) + .commit() + } + + var showRestTimerAfterWarmup: Boolean + get() { + return getSharedPreferences() + .getBoolean(Constants.preferencesShowRestTimerAfterWarmup, false) + } + + set(value) { + getSharedPreferences() + .edit() + .putBoolean(Constants.preferencesShowRestTimerAfterWarmup, value) + .commit() + } + + var showRestTimerAfterBodylineDrills: Boolean + get() { + return getSharedPreferences() + .getBoolean(Constants.preferencesShowRestTimerAfterBodylineDrills, true) + } + + set(value) { + getSharedPreferences() + .edit() + .putBoolean(Constants.preferencesShowRestTimerAfterBodylineDrills, value) + .commit() + } + + var showRestTimerAfterFlexibilityExercises: Boolean + get() { + return getSharedPreferences() + .getBoolean(Constants.preferencesShowRestTimerAfterFlexibilityExercises, false) + } + + set(value) { + getSharedPreferences() + .edit() + .putBoolean(Constants.preferencesShowRestTimerAfterFlexibilityExercises, value) + .commit() + } + + var restTimerDefaultSeconds: Int + get() { + return getSharedPreferences() + .getString(Constants.preferencesRestTimerDefaultSeconds, "60").toInt() + } + + set(value) { + getSharedPreferences() + .edit() + .putString(Constants.preferencesRestTimerDefaultSeconds, value.toString()) + .commit() + } + val weightMeasurementUnit: WeightMeasurementUnit get() { val value = getSharedPreferences().getString(Constants.preferencesWeightMeasurementUnitsKey, "kg") diff --git a/app/src/main/kotlin/com/bodyweight/fitness/view/HomeViewPresenter.kt b/app/src/main/kotlin/com/bodyweight/fitness/view/HomeViewPresenter.kt index 533afa8f..344d9fe9 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/view/HomeViewPresenter.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/view/HomeViewPresenter.kt @@ -9,6 +9,7 @@ import android.util.AttributeSet import com.bodyweight.fitness.* import com.bodyweight.fitness.model.RepositoryCategory import com.bodyweight.fitness.model.RepositoryRoutine +import com.bodyweight.fitness.model.Routine import com.bodyweight.fitness.repository.Repository import com.bodyweight.fitness.stream.RoutineStream import com.bodyweight.fitness.stream.Stream @@ -45,11 +46,21 @@ class HomeViewPresenter : AbstractPresenter() { RoutineStream.routineObservable() .bindToLifecycle(view) .subscribe { + updateShortDescription(it) updateTodaysProgress() updateStatistics() } } + fun updateShortDescription(routine: Routine) { + val view = (getView() as HomeView) + + view.setShortDescription( + title = routine.title, + shortDescription = routine.shortDescription, + url = routine.url) + } + fun updateTodaysProgress() { val view = (getView() as HomeView) @@ -186,6 +197,14 @@ class HomeViewPresenter : AbstractPresenter() { putExtra(Intent.EXTRA_EMAIL, "damian@mazur.io") }, "Send Email")) } + + fun readMoreAboutRoutine() { + val routine = RoutineStream.routine + + context().startActivity(Intent(Intent.ACTION_VIEW).apply { + data = Uri.parse(routine.url) + }) + } } open class HomeView : AbstractView { @@ -213,6 +232,15 @@ open class HomeView : AbstractView { send_email.setOnClickListener { (presenter as HomeViewPresenter).sendEmail() } + + read_more_about_routine.setOnClickListener { + (presenter as HomeViewPresenter).readMoreAboutRoutine() + } + } + + fun setShortDescription(title: String, shortDescription: String, url: String) { + routine_title.text = title + short_description.text = shortDescription } fun setStartWorkoutButtonTitle(title: String) { diff --git a/app/src/main/kotlin/com/bodyweight/fitness/view/progress/ProgressGeneralViewPresenter.kt b/app/src/main/kotlin/com/bodyweight/fitness/view/progress/ProgressGeneralViewPresenter.kt index 7d9767b9..83fb9d88 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/view/progress/ProgressGeneralViewPresenter.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/view/progress/ProgressGeneralViewPresenter.kt @@ -102,7 +102,7 @@ class ProgressGeneralViewPresenter : AbstractPresenter() { val layout = parent.inflate(R.layout.activity_progress_general_exercise) layout.exercise_title.text = exercise.title - layout.category_title.text = exercise.category?.title + layout.category_title.text = exercise.category?.title + " - " + exercise.section?.title parent.addView(layout) } diff --git a/app/src/main/kotlin/com/bodyweight/fitness/view/workout/NavigationViewPresenter.kt b/app/src/main/kotlin/com/bodyweight/fitness/view/workout/NavigationViewPresenter.kt index 6aefb800..12dd5d91 100644 --- a/app/src/main/kotlin/com/bodyweight/fitness/view/workout/NavigationViewPresenter.kt +++ b/app/src/main/kotlin/com/bodyweight/fitness/view/workout/NavigationViewPresenter.kt @@ -2,11 +2,14 @@ package com.bodyweight.fitness.view.workout import android.content.Context import android.util.AttributeSet +import android.view.View import com.bodyweight.fitness.setGone import com.bodyweight.fitness.setInvisible import com.bodyweight.fitness.setVisible import com.bodyweight.fitness.stream.RoutineStream +import com.bodyweight.fitness.stream.Stream +import com.bodyweight.fitness.utils.Preferences import com.bodyweight.fitness.view.AbstractPresenter import com.bodyweight.fitness.view.AbstractView @@ -25,6 +28,29 @@ class NavigationPresenter : AbstractPresenter() { view.showTimerOrRepsLogger(it.isTimedSet) view.showPreviousNextButtons(it.isPrevious, it.isNext) } + + + Stream.restTimerObservable + .bindToLifecycle(view) + .subscribe { + restoreView(view) + } + + Stream.loggedSetRepsObservable + .bindToLifecycle(view) + .subscribe { + val view = (view as NavigationView) + + showRestTimer(view) + } + + Stream.loggedSecondsObservable + .bindToLifecycle(view) + .subscribe { + val view = (view as NavigationView) + + showRestTimer(view) + } } override fun restoreView(view: AbstractView) { @@ -38,6 +64,30 @@ class NavigationPresenter : AbstractPresenter() { view.showPreviousNextButtons(exercise.isPrevious, exercise.isNext) } + fun showRestTimer(view: NavigationView) { + if (Preferences.showRestTimer) { + val section = RoutineStream.exercise.section!! + + if (section.sectionId == "section0") { + if (Preferences.showRestTimerAfterWarmup) { + view.showRestTimer() + } + } else if (section.sectionId == "section1") { + if (Preferences.showRestTimerAfterBodylineDrills) { + view.showRestTimer() + } + } else { + if (RoutineStream.routine.routineId != "routine0") { + if (Preferences.showRestTimerAfterFlexibilityExercises) { + view.showRestTimer() + } + } else { + view.showRestTimer() + } + } + } + } + fun previousExercise() { if (RoutineStream.exercise.isPrevious) { RoutineStream.exercise = RoutineStream.exercise.previous!! @@ -68,13 +118,25 @@ open class NavigationView : AbstractView { } } + fun showRestTimer() { + rest_timer_view.setVisible() + timer_view.setGone() + reps_logger_view.setGone() + } + fun showTimerOrRepsLogger(isTimed: Boolean) { - if (isTimed) { - timer_view.setVisible() - reps_logger_view.setGone() + if (!RestTimerShared.isPlaying) { + if (isTimed) { + rest_timer_view.setGone() + timer_view.setVisible() + reps_logger_view.setGone() + } else { + rest_timer_view.setGone() + timer_view.setGone() + reps_logger_view.setVisible() + } } else { - timer_view.setGone() - reps_logger_view.setVisible() + showRestTimer() } } diff --git a/app/src/main/kotlin/com/bodyweight/fitness/view/workout/RestTimerViewPresenter.kt b/app/src/main/kotlin/com/bodyweight/fitness/view/workout/RestTimerViewPresenter.kt new file mode 100644 index 00000000..42b060ab --- /dev/null +++ b/app/src/main/kotlin/com/bodyweight/fitness/view/workout/RestTimerViewPresenter.kt @@ -0,0 +1,180 @@ +package com.bodyweight.fitness.view.workout + +import android.content.Context +import android.media.MediaPlayer +import android.os.CountDownTimer +import android.util.AttributeSet + +import com.bodyweight.fitness.* +import com.bodyweight.fitness.stream.RoutineStream +import com.bodyweight.fitness.stream.Stream +import com.bodyweight.fitness.utils.Preferences +import com.bodyweight.fitness.view.AbstractPresenter +import com.bodyweight.fitness.view.AbstractView + +import com.trello.rxlifecycle.kotlin.bindToLifecycle +import kotlinx.android.synthetic.main.view_timer.view.* + +object RestTimerShared { + var seconds = Preferences.restTimerDefaultSeconds + var currentSeconds = seconds + var startedLoggingSeconds = seconds + var loggedSeconds = 0 + + var isPlaying = false + var restored = false + + var countDownTimer: CountDownTimer? = null +} + +class RestTimerPresenter : AbstractPresenter() { + override fun bindView(view: AbstractView) { + super.bindView(view) + + RestTimerShared.countDownTimer?.cancel() + + if (RestTimerShared.restored) { + RestTimerShared.restored = false + + restartTimer(RestTimerShared.currentSeconds, true, RestTimerShared.isPlaying) + + if (RestTimerShared.isPlaying) { + startTimer() + } + } else { + restartTimer(getSeconds(), false, false) + } + + Stream.loggedSetRepsObservable + .bindToLifecycle(view) + .subscribe { + startTimer() + } + + Stream.loggedSecondsObservable + .bindToLifecycle(view) + .subscribe { + startTimer() + } + } + + override fun saveView() { + RestTimerShared.restored = true + + super.saveView() + } + + fun startTimer() { + if (Preferences.showRestTimer) { + val section = RoutineStream.exercise.section!! + + if (section.sectionId == "section0") { + if (Preferences.showRestTimerAfterWarmup) { + RestTimerShared.countDownTimer?.start() + } + } else if (section.sectionId == "section1") { + if (Preferences.showRestTimerAfterBodylineDrills) { + RestTimerShared.countDownTimer?.start() + } + } else { + if (RoutineStream.routine.routineId != "routine0") { + if (Preferences.showRestTimerAfterFlexibilityExercises) { + RestTimerShared.countDownTimer?.start() + } + } else { + RestTimerShared.countDownTimer?.start() + } + } + } + } + + fun restartTimer(seconds: Int, restored: Boolean, isPlaying: Boolean) { + val view = (mView as RestTimerView) + + RestTimerShared.countDownTimer?.cancel() + + RestTimerShared.isPlaying = isPlaying + RestTimerShared.currentSeconds = seconds + + RestTimerShared.countDownTimer = buildCountDownTimer(seconds, restored) + + view.setMinutes(seconds.formatMinutes()) + view.setSeconds(seconds.formatSeconds()) + } + + fun playSound() { + val view = (mView as RestTimerView) + + if (Preferences.playSoundWhenTimerStops()) { + val mediaPlayer = MediaPlayer.create(view.context, R.raw.finished) + + mediaPlayer.isLooping = false + mediaPlayer.start() + } + } + + fun onClickStartStopTimeButton() { + restartTimer(seconds = getSeconds(), restored = false, isPlaying = false) + + Stream.setRestTimer() + } + + fun buildCountDownTimer(seconds: Int, restored: Boolean): CountDownTimer? { + val view = (mView as RestTimerView) + + if (restored) { + RestTimerShared.loggedSeconds = RestTimerShared.startedLoggingSeconds + } else { + RestTimerShared.startedLoggingSeconds = seconds + RestTimerShared.loggedSeconds = seconds + } + + return object : CountDownTimer((seconds * 1000).toLong(), 100) { + override fun onTick(millisUntilFinished: Long) { + val timerSeconds = millisUntilFinished.toInt() / 1000 + + RestTimerShared.isPlaying = true + RestTimerShared.currentSeconds = timerSeconds + + view.setMinutes(timerSeconds.formatMinutes()) + view.setSeconds(timerSeconds.formatSeconds()) + } + + override fun onFinish() { + restartTimer(getSeconds(), false, false) + + playSound() + + Stream.setRestTimer() + } + } + } + + fun getSeconds(): Int { + return Preferences.restTimerDefaultSeconds + } +} + +open class RestTimerView : AbstractView { + override var presenter: AbstractPresenter = RestTimerPresenter() + + constructor(context: Context) : super(context) + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + + override fun onFinishInflate() { + super.onFinishInflate() + + val presenter = (presenter as RestTimerPresenter) + + stop_rest_timer_button.setOnClickListener { presenter.onClickStartStopTimeButton() } + } + + fun setMinutes(text: String) { + rest_timer_minutes.text = text + } + + fun setSeconds(text: String) { + rest_timer_seconds.text = text + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/action_close_light.xml b/app/src/main/res/drawable/action_close_light.xml new file mode 100644 index 00000000..d950ed95 --- /dev/null +++ b/app/src/main/res/drawable/action_close_light.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout-port/view_timer.xml b/app/src/main/res/layout-port/view_timer.xml index cf836b51..1c45cf80 100644 --- a/app/src/main/res/layout-port/view_timer.xml +++ b/app/src/main/res/layout-port/view_timer.xml @@ -189,6 +189,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +