Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#110 / 타이머 리팩토링 #111

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/java/sopt/uni/UniApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class UniApplication : Application() {
override fun onCreate() {
super.onCreate()
SparkleStorage.init(this)
SparkleStorage.timerClear()
KakaoSdk.init(this, BuildConfig.KAKAO_APP_KEY)
if (BuildConfig.DEBUG) Timber.plant(Timber.DebugTree())
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import sopt.uni.BuildConfig

object SparkleStorage {
private lateinit var pref: SharedPreferences
private lateinit var prefTimer: SharedPreferences

fun init(context: Context) {
val masterKeyAlias = MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
Expand All @@ -28,6 +29,11 @@ object SparkleStorage {
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
}

prefTimer = context.getSharedPreferences(
NAME,
Context.MODE_PRIVATE
)
}

var accessToken: String?
Expand All @@ -47,13 +53,38 @@ object SparkleStorage {
get() = pref.getInt(PARTNER_ID, -1)
set(value) = pref.edit { putInt(PARTNER_ID, value ?: -1).apply() }

var isActive: Boolean
get() = prefTimer.getBoolean(ACTIVEKEY, false)
set(value) = prefTimer.edit { putBoolean(ACTIVEKEY, value).apply() }

var totalTime: Float
get() = prefTimer.getFloat(TOTALTIMEKEY, 0F)
set(value) = prefTimer.edit { putFloat(TOTALTIMEKEY, value).apply() }

var isPause: Boolean
get() = prefTimer.getBoolean(PAUSEKEY, false)
set(value) = prefTimer.edit { putBoolean(PAUSEKEY, value).apply() }

var remainTime: Long
get() = prefTimer.getLong(REMAINTIMEKEY, totalTime.toLong())
set(value) = prefTimer.edit { putLong(REMAINTIMEKEY, value).apply() }

fun clear() {
pref.edit().clear().apply()
}

fun timerClear() {
prefTimer.edit().clear().apply()
}
}

const val ACCESS_TOKEN = "accessToken"
const val REFRESH_TOKEN = "refreshToken"
const val AUTH = "auth"
const val USER_ID = "userId"
const val PARTNER_ID = "partnerId"
const val NAME = "timer_prefs"
const val ACTIVEKEY = "isTimerActive"
const val TOTALTIMEKEY = "totalTime"
const val PAUSEKEY = "isTimerPause"
const val REMAINTIMEKEY = "remainingSeconds"
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package sopt.uni.presentation.timer

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.FragmentTimerActiveBinding
import sopt.uni.util.binding.BindingFragment
import sopt.uni.util.extension.setOnSingleClickListener
Expand All @@ -20,9 +19,6 @@ class TimerActiveFragment(total: Float) :
private val viewModel by activityViewModels<TimerViewModel>()
private val updateIntervalMillis = INTERVAL
private var totalTime = total
private val sharedPreferences: SharedPreferences by lazy {
requireContext().getSharedPreferences(NAME, Context.MODE_PRIVATE)
}

private val updateTimer =
object : CountDownTimer(
Expand All @@ -49,7 +45,7 @@ class TimerActiveFragment(total: Float) :
}

private fun stateTimer() {
val isPause = sharedPreferences.getBoolean(PAUSEKEY, false)
val isPause = SparkleStorage.isPause

if (isPause) {
binding.btnTimerStop.visibility = View.GONE
Expand All @@ -65,7 +61,7 @@ class TimerActiveFragment(total: Float) :
private fun pauseTimer() {
binding.btnTimerStop.setOnSingleClickListener {
stopTimer()
sharedPreferences.edit().putBoolean(PAUSEKEY, true).apply()
SparkleStorage.isPause = true
binding.btnTimerStop.visibility = View.GONE
binding.btnTimerContinue.visibility = View.VISIBLE
binding.circularProgressBar.progressBarColor =
Expand All @@ -75,7 +71,7 @@ class TimerActiveFragment(total: Float) :

private fun resumeTimer() {
binding.btnTimerContinue.setOnSingleClickListener {
sharedPreferences.edit().putBoolean(PAUSEKEY, false).apply()
SparkleStorage.isPause = false
startTimer()
binding.btnTimerContinue.visibility = View.GONE
binding.btnTimerStop.visibility = View.VISIBLE
Expand All @@ -86,10 +82,7 @@ class TimerActiveFragment(total: Float) :

private fun deleteTimer() {
binding.btnTimerLeft.setOnSingleClickListener {
sharedPreferences.edit().putBoolean(ACTIVEKEY, false).apply()
sharedPreferences.edit().remove(REMAINTIMEKEY).apply()
sharedPreferences.edit().remove(TOTALTIMEKEY).apply()

SparkleStorage.timerClear()
stopTimer()
goTimerSettingFragment()
}
Expand All @@ -101,12 +94,12 @@ class TimerActiveFragment(total: Float) :
}

private fun getLeftTime() {
val totalSeconds = sharedPreferences.getLong(REMAINTIMEKEY, totalTime.toLong())
val totalSeconds = SparkleStorage.remainTime
viewModel.updateLeftTime(totalSeconds)
}

private fun initCircularProgressBar() {
val totalTime = sharedPreferences.getFloat(TOTALTIMEKEY, ZEROFLOAT)
val totalTime = SparkleStorage.totalTime

viewModel.setMaxTime(totalTime)
viewModel.leftTime.observe(viewLifecycleOwner) { time ->
Expand Down Expand Up @@ -143,14 +136,8 @@ class TimerActiveFragment(total: Float) :

companion object {
private const val SNACKBARMESSAGE = "타이머가 종료되었어요."
private const val NAME = "timer_prefs"
private const val PAUSEKEY = "isTimerPause"
private const val ACTIVEKEY = "isTimerActive"
private const val TOTALTIMEKEY = "totalTime"
private const val REMAINTIMEKEY = "remainingSeconds"
private const val INTERVAL = 100L
private const val MILLISECONDS = 1000
private const val ZEROFLOAT = 0F
private const val ZEROLONG = 0L
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sopt.uni.presentation.timer

import android.content.Context
import android.content.SharedPreferences
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Bundle
Expand All @@ -13,6 +12,7 @@ import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.FragmentTimerSettingBinding
import sopt.uni.util.MakeVibrator
import sopt.uni.util.binding.BindingFragment
Expand All @@ -25,9 +25,6 @@ class TimerSettingFragment :
private val viewModel by activityViewModels<TimerViewModel>()
private var mediaPlayer: MediaPlayer? = null
private lateinit var audioManager: AudioManager
private val sharedPreferences: SharedPreferences by lazy {
requireContext().getSharedPreferences(NAME, Context.MODE_PRIVATE)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down Expand Up @@ -127,9 +124,9 @@ class TimerSettingFragment :
}

private fun initsharedPrefSetting(total: Int) {
sharedPreferences.edit().putFloat(TOTALTIMEKEY, total.toFloat()).apply()
sharedPreferences.edit().putBoolean(ACTIVEKEY, true).apply()
sharedPreferences.edit().putBoolean(PAUSEKEY, false).apply()
SparkleStorage.totalTime = total.toFloat()
SparkleStorage.isActive = true
SparkleStorage.isPause = false
}

private fun initTimerSetting() {
Expand All @@ -151,9 +148,6 @@ class TimerSettingFragment :
}

companion object {
private const val NAME = "timer_prefs"
private const val PAUSEKEY = "isTimerPause"
private const val ACTIVEKEY = "isTimerActive"
private const val TOTALTIMEKEY = "totalTime"
private const val ZERO = 0
private const val MAXNUM = 59
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sopt.uni.presentation.timer
import android.os.Bundle
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.ActivityTimerBinding
import sopt.uni.util.binding.BindingActivity
import sopt.uni.util.extension.setOnSingleClickListener
Expand All @@ -19,14 +20,12 @@ class TimerStartActivity : BindingActivity<ActivityTimerBinding>(R.layout.activi
}

private fun setTimerFragment() {
val sharedPreferences =
applicationContext.getSharedPreferences(NAME, MODE_PRIVATE)
val isActive = sharedPreferences.getBoolean(ACTIVEKEY, false)
val isActive = SparkleStorage.isActive
val fragmentTransaction = supportFragmentManager.beginTransaction()

if (isActive) {
val fragmentTimerActive =
TimerActiveFragment(sharedPreferences.getFloat(TOTALTIMEKEY, ZEROFLOAT))
TimerActiveFragment(SparkleStorage.totalTime)
fragmentTransaction.replace(R.id.fcv_timer, fragmentTimerActive)
} else {
val fragmentTimerSetting = TimerSettingFragment()
Expand All @@ -51,10 +50,6 @@ class TimerStartActivity : BindingActivity<ActivityTimerBinding>(R.layout.activi
}

companion object {
private const val NAME = "timer_prefs"
private const val ACTIVEKEY = "isTimerActive"
private const val TOTALTIMEKEY = "totalTime"
private const val ZEROFLOAT = 0F
private const val TIMER_DIALOG_TAG = "LeaveTimerDialogFragment"
}
}
19 changes: 6 additions & 13 deletions app/src/main/java/sopt/uni/presentation/timer/TimerWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ package sopt.uni.presentation.timer
import android.content.Context
import androidx.work.Worker
import androidx.work.WorkerParameters
import sopt.uni.data.datasource.local.SparkleStorage

class TimerWorker(context: Context, params: WorkerParameters) :
Worker(context, params) {

private var totalSeconds: Long = ZEROLONG

override fun doWork(): Result {
val sharedPreferences =
applicationContext.getSharedPreferences(NAME, Context.MODE_PRIVATE)
val inputData = inputData
totalSeconds = inputData.getLong(TOTALTIMEKEY, ZEROLONG)
val startTimer = sharedPreferences.getBoolean(ACTIVEKEY, false)
val startTimer = SparkleStorage.isActive

while (totalSeconds >= ZERO && startTimer) {
val cancel = sharedPreferences.getBoolean(ACTIVEKEY, true)
val pause = sharedPreferences.getBoolean(PAUSEKEY, false)
val cancel = SparkleStorage.isActive
val pause = SparkleStorage.isPause

if (!cancel) {
break
Expand All @@ -27,23 +26,17 @@ class TimerWorker(context: Context, params: WorkerParameters) :
continue
}
totalSeconds--
sharedPreferences.edit().putLong(REMAINTIMEKEY, totalSeconds).apply()
SparkleStorage.remainTime = totalSeconds
Thread.sleep(ONESECONDS)
}

sharedPreferences.edit().putBoolean(ACTIVEKEY, false).apply()
sharedPreferences.edit().remove(REMAINTIMEKEY).apply()
sharedPreferences.edit().remove(TOTALTIMEKEY).apply()
SparkleStorage.timerClear()

return Result.success()
}

companion object {
private const val NAME = "timer_prefs"
private const val PAUSEKEY = "isTimerPause"
private const val ACTIVEKEY = "isTimerActive"
private const val TOTALTIMEKEY = "totalTime"
private const val REMAINTIMEKEY = "remainingSeconds"
private const val ONESECONDS = 1000L
private const val ZEROLONG = 0L
private const val ZERO = 0
Expand Down
Loading