diff --git a/app/src/main/java/sopt/uni/UniApplication.kt b/app/src/main/java/sopt/uni/UniApplication.kt index 2f8bea77..1c41270b 100644 --- a/app/src/main/java/sopt/uni/UniApplication.kt +++ b/app/src/main/java/sopt/uni/UniApplication.kt @@ -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) diff --git a/app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt b/app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt index e6f60517..7e1865eb 100644 --- a/app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt +++ b/app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt @@ -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) @@ -28,6 +29,11 @@ object SparkleStorage { EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, ) } + + prefTimer = context.getSharedPreferences( + NAME, + Context.MODE_PRIVATE + ) } var accessToken: String? @@ -47,9 +53,29 @@ 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" @@ -57,3 +83,8 @@ 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" diff --git a/app/src/main/java/sopt/uni/presentation/timer/TimerActiveFragment.kt b/app/src/main/java/sopt/uni/presentation/timer/TimerActiveFragment.kt index d1544392..99b280ae 100644 --- a/app/src/main/java/sopt/uni/presentation/timer/TimerActiveFragment.kt +++ b/app/src/main/java/sopt/uni/presentation/timer/TimerActiveFragment.kt @@ -1,7 +1,5 @@ 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 @@ -9,6 +7,7 @@ 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 @@ -20,9 +19,6 @@ class TimerActiveFragment(total: Float) : private val viewModel by activityViewModels() 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( @@ -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 @@ -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 = @@ -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 @@ -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() } @@ -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 -> @@ -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 } } diff --git a/app/src/main/java/sopt/uni/presentation/timer/TimerSettingFragment.kt b/app/src/main/java/sopt/uni/presentation/timer/TimerSettingFragment.kt index 638b720e..14f21d20 100644 --- a/app/src/main/java/sopt/uni/presentation/timer/TimerSettingFragment.kt +++ b/app/src/main/java/sopt/uni/presentation/timer/TimerSettingFragment.kt @@ -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 @@ -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 @@ -25,9 +25,6 @@ class TimerSettingFragment : private val viewModel by activityViewModels() 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) @@ -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() { @@ -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 diff --git a/app/src/main/java/sopt/uni/presentation/timer/TimerStartActivity.kt b/app/src/main/java/sopt/uni/presentation/timer/TimerStartActivity.kt index 88ebb0d7..256345a3 100644 --- a/app/src/main/java/sopt/uni/presentation/timer/TimerStartActivity.kt +++ b/app/src/main/java/sopt/uni/presentation/timer/TimerStartActivity.kt @@ -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 @@ -19,14 +20,12 @@ class TimerStartActivity : BindingActivity(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() @@ -51,10 +50,6 @@ class TimerStartActivity : BindingActivity(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" } } diff --git a/app/src/main/java/sopt/uni/presentation/timer/TimerWorker.kt b/app/src/main/java/sopt/uni/presentation/timer/TimerWorker.kt index e16aa668..a6839cd9 100644 --- a/app/src/main/java/sopt/uni/presentation/timer/TimerWorker.kt +++ b/app/src/main/java/sopt/uni/presentation/timer/TimerWorker.kt @@ -3,6 +3,7 @@ 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) { @@ -10,15 +11,13 @@ class TimerWorker(context: Context, params: WorkerParameters) : 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 @@ -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