Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Set timer with RX java , closed #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Miladsalimiiiii committed Jul 30, 2019
1 parent cb528c5 commit 76b9fe7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CodeVerificationFragment : BaseFragment() {

when (it.data) {

"RESEND" -> {
getString(R.string.codeVerification_resendCode) -> {

onFinish()
}
Expand Down Expand Up @@ -190,10 +190,8 @@ class CodeVerificationFragment : BaseFragment() {
}
}


private fun setTimer() {


}

private fun disableButton() {
Expand All @@ -204,6 +202,6 @@ class CodeVerificationFragment : BaseFragment() {
private fun enableButton() {

mButton.isEnabled = true
mButton.setTextColor(ContextCompat.getColor(requireContext(),R.color.colorPrimaryLight))
mButton.setTextColor(ContextCompat.getColor(requireContext(), R.color.colorPrimaryLight))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.netalic.peacock.ui.registeration.codeverification

import android.os.CountDownTimer
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import de.netalic.peacock.data.exception.ActivationCodeIsNotValid
Expand All @@ -11,21 +10,21 @@ import de.netalic.peacock.data.model.MyResponse
import de.netalic.peacock.data.model.User
import de.netalic.peacock.data.repository.UserRepository
import de.netalic.peacock.ui.base.BaseViewModel
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
import java.util.concurrent.TimeUnit


class CodeVerificationViewModel(private val userRepository: UserRepository) : BaseViewModel() {


companion object {

var sTimer = 30000
const val sResend="RESEND"
const val sResend = "RESEND"
}

private lateinit var mCountDownTimer: CountDownTimer
private val mBindResponseLiveData = MutableLiveData<MyResponse<ResponseBody>>()
private val mTimerLiveData = MutableLiveData<MyResponse<String>>()

Expand All @@ -39,26 +38,30 @@ class CodeVerificationViewModel(private val userRepository: UserRepository) : Ba
return mBindResponseLiveData
}

fun setTimer() {


mCountDownTimer = object : CountDownTimer(sTimer.toLong(), 1000) {
override fun onTick(millisUntilFinished: Long) {
fun setTimer(time: Long = 30) {

val minuteTimer = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)
val secondTimer = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)
)
mTimerLiveData.value = MyResponse.success(String.format("%02d:%02d ", minuteTimer, secondTimer))
}

override fun onFinish() {
val timerDisposable = Observable.interval(1, TimeUnit.SECONDS)
.take(time)
.map { 30 - it }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{

mTimerLiveData.value = MyResponse.success(sResend)
val minuteTimer = TimeUnit.SECONDS.toMinutes(it)
val secondTimer = TimeUnit.SECONDS.toSeconds(it) -
TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(it))
mTimerLiveData.value = MyResponse.success(String.format("%02d:%02d ", minuteTimer, secondTimer))
},
{

}
},
{
mTimerLiveData.value = MyResponse.success(sResend)
}
)

}.start()
mCompositeDisposable.add(timerDisposable)
}

fun bind(user: User) {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/layout/fragment_codeverification.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/textView_codeVerification_codeVerification"
android:layout_marginBottom="@dimen/margin_all_20dp"

/>
android:layout_marginBottom="@dimen/margin_all_20dp"/>

<TextView
android:id="@+id/textView_codeVerification_codeVerification"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,13 @@ class CodeVerificationViewModelTest : BaseTest() {
)
}

@Test
fun setTimer() {

mCodeVerificationViewModel.setTimer(3)

Assert.assertEquals(LiveDataTestUtil.getValue(mCodeVerificationViewModel.getTimerLiveData()), 3)

}

}

0 comments on commit 76b9fe7

Please sign in to comment.