๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋นˆ ๊ตฌ๋ฉ ์ฑ„์šฐ๊ธฐ

[Android][Coroutine][SharedPreferences] ํƒ€์ด๋จธ ๊ตฌํ˜„

์ธ์ฆ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์†ก ์š”์ฒญํ•˜๋Š” ์„œ๋ฒ„ api๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  2๋ถ„ ๋™์•ˆ ์ธ์ฆ๋ฒˆํ˜ธ ์žฌ๋ฐœ๊ธ‰์„ ๋ง‰๋Š” ํƒ€์ด๋จธ์˜ ๊ตฌํ˜„์„ ํ•ด์•ผ ํ–ˆ๋‹ค.

 

ํƒ€์ด๋จธ ๊ตฌํ˜„์€ ์ฒ˜์Œ์ด๋ผ์„œ ๊ณ ๋ฏผ์„ ๋งŽ์ด ํ–ˆ๋‹ค. CountDownTimer๋ฅผ ์“ธ๊นŒ, ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ• ๊นŒ...

์„ ๋ฐฐ๋‹˜๊ป˜ ์—ฌ์ญค๋ดค๋”๋‹ˆ CountDownTimer์™€ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์•ฑ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‚ ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ์—๋Š” ์žฌ๊ณ  ์žˆ๋˜ ์‹œ๊ฐ„๋„ ๋‚ ๋ผ๊ฐ„๋‹ค๊ณ  ์•Œ๋ ค์ฃผ์…จ๋‹ค. SharedPreferences๋ฅผ ์จ์„œ ํ™”๋ฉด์— ์ง„์ž…ํ•˜๊ณ  ๋‚˜๊ฐˆ ๋•Œ๋งˆ๋‹ค ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ์„ ์“ฐ๊ณ , ์ฝ”๋ฃจํ‹ด์„ ์“ฐ๋ฉด ์‰ฝ๋‹ค๊ณ  ์•Œ๋ ค์ฃผ์…จ๋‹ค. ๋„ˆ๋ฌด ๋‹น์—ฐํ•œ ๊ฑธ ๋ชฐ๋ž๋ƒ๋Š” ์„ ๋ฐฐ๋‹˜์˜ ํ‘œ์ •์„ ๋ณด๊ณ ๐Ÿ˜ณ ๋ฐ˜์„ฑํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ž ์™œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‚ ๋ผ๊ฐ€๋Š” ๊ฒฝ์šฐ๋Š” ์ƒ๊ฐ์„ ๋ชปํ•œ๊ฑฐ์•ผ

 

๊ทธ ๋’ค๋กœ ์ž‘์—…์€ ์‰ฌ์› ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ๋ฐœ์ž๋“ค์˜ ์†์„ ๊ฝค๋‚˜ ํƒ„ ์ด ์•ฑ์˜ ์ฝ”๋“œ๋Š” SharedPrefernces๋ฅผ ์‰ฝ๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ์ฝ”๋“œ ๋ชจ์ž„์ด ์žˆ์–ด์„œ ๊ฐ€์ ธ๋‹ค๊ฐ€ ์“ฐ๋ฉด ๋๋‹ค.

 

ํƒ€์ด๋จธ ๋‹ด๋‹น ๊ฐ์ฒด

//SharedPrefereneces๋กœ ์‹œ๊ฐ„ ์ •๋ณด์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด
object CodeTimer {

    const val duration : Long = 2* 60 * 1000
    
    // ์ €์žฅ๋œ ์‹œ๊ฐ„์„ 0์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
    fun reset() {
        //SharedPreferences๋ฅผ ํ†ตํ•ด 0์œผ๋กœ ์‹œ๊ฐ„์„ ์ €์žฅ
        ...
    }

    // ํƒ€์ด๋จธ ์‹œ์ž‘ ์‹œ๊ฐ„์„ ์ €์žฅ
	fun setRequestTime(time:Long) {
        //SharedPreferences๋ฅผ ํ†ตํ•ด ์‹œ์ž‘ ์‹œ๊ฐ„์„ ์ €์žฅ
        ...
    }
    
    // ๋‚จ์€ ์‹œ๊ฐ„์„ ๋ฐ˜ํ™˜
    fun getRemainTime() : Long {
    	val currentTime = System.currentTimeMillies()
        val requestTime = //SharedPreferences๋กœ ํƒ€์ด๋จธ ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ฐ€์ ธ์˜ด
        val remainTime = duration - (currentTime - requestTime)
        return when(remainTime > 0L) {
            true -> remainTime
            false -> {
                reset()
                0L
            }
        }
    }

}

 

 

๋ทฐ๋ชจ๋ธ์—์„œ ์‚ฌ์šฉ

class TimerViewModel : ViewModel() {

    private val _timeText = MutableLiveData<String>()
    val timeText : LiveData<String> = _timeText
    
    private var timeJob : Job? = null
    
    ...
    
    fun countDonw() {
        timberJob = viewModelScope.launch {
            var remainTime = CodeTimer.getRemainTime()
            var millisUntilFinished = if(remainTime == 0L) {
                CodeTimer.setRequestTime(System.currentTimeMillis())
                CodeTimer.duration
            } else remainTime
            
            while(millisUntilFinished > 0) {
                val second = milliesUntilFinished / 1000 % 60
                val minutes = millisUnitlFinished / (1000 * 60) / 60
                _timeText.value = getTimeString(minutes, second)
                delay(1000L)
                millisUntilFinished -= 1000L
            }
            _timeText.value = getTimeString(0, 0)
            setTimeout()
        }
    }

    fun stopCountDown() {
        timerJob?.cancel()
    }
    
    ...
}