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

Commit

Permalink
Fix phone validator convention,closed #36
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaT2 committed Jul 30, 2019
1 parent 6b1322b commit 4263c1c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.netalic.peacock.data.exception

class InvalidPhoneNumberException:BaseException()
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class RegistrationFragment : BaseFragment() {

override fun initUiListeners() {
mCountryFlagImageView.setOnClickListener { countryPicker() }
mContinueButton.setOnClickListener { validatePhoneNumber() }
mContinueButton.setOnClickListener {
disableContinueButton()
claim()
}
mPhoneInputEditText.setOnKeyListener { _, keyCode, keyEvent ->
if (keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.action
== KeyEvent.ACTION_DOWN
Expand All @@ -61,11 +64,6 @@ class RegistrationFragment : BaseFragment() {
}
}

private fun validatePhoneNumber() {
val phoneNumber: String = mPhoneInputEditText.text.toString()
mPhoneInputViewModel.phoneValidator(phoneNumber)
}

override fun initUiComponents() {
initToolbar()
phoneInputEditTextWatcher()
Expand All @@ -82,7 +80,6 @@ class RegistrationFragment : BaseFragment() {
}

private fun initObserver() {
//TODO-Tina Do phone validation like our sample structure.
mPhoneInputViewModel.getClaimLiveData().observe(this, Observer {

//ToDo-tina: get all status for
Expand All @@ -91,17 +88,6 @@ class RegistrationFragment : BaseFragment() {
}
Snackbar.make(mViewRoot, it.status.toString(), Snackbar.LENGTH_LONG).show()
})

mPhoneInputViewModel.getPhoneValidatorLiveData().observe(this, Observer {

when (it.data) {
ResponseStatus.PHONE_VALID -> claim()
ResponseStatus.PHONE_INVALID -> Snackbar.make(
mViewRoot, it.status.toString()
, Snackbar.LENGTH_LONG
).show()
}
})
}

private fun claim() {
Expand All @@ -112,8 +98,7 @@ class RegistrationFragment : BaseFragment() {
}

private fun countryPicker() {
val countryPicker = CountryPicker.Builder(requireContext()).setCountrySelectionListener{
country ->
val countryPicker = CountryPicker.Builder(requireContext()).setCountrySelectionListener { country ->

mCountryCode = country.code
mDialCode = country.dialCode
Expand All @@ -132,7 +117,6 @@ class RegistrationFragment : BaseFragment() {
}



private fun changeCountryImage(flagName: String) {
val iconResId = resources.getIdentifier(flagName, "drawable", activity?.packageName)
mCountryFlagImageView.setImageResource(iconResId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package de.netalic.peacock.ui.registration

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import de.netalic.peacock.data.exception.BadRequestException
import de.netalic.peacock.data.exception.InvalidPhoneNumberException
import de.netalic.peacock.data.exception.InvalidUdidOrPhoneException
import de.netalic.peacock.data.exception.ServerException
import de.netalic.peacock.data.model.MyResponse
Expand All @@ -14,51 +14,34 @@ import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

enum class ResponseStatus {
PHONE_VALID,
PHONE_INVALID
}

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

private val mClaimResponseLiveData = MutableLiveData<MyResponse<UserModel>>()
private val mPhoneValidatorLiveData = MutableLiveData<MyResponse<ResponseStatus>>()

fun getClaimLiveData(): LiveData<MyResponse<UserModel>> {
return mClaimResponseLiveData
}

fun getPhoneValidatorLiveData(): LiveData<MyResponse<ResponseStatus>> {
return mPhoneValidatorLiveData
}

fun claim(phone: String, udid: String) {

val disposable = userRepository.claim(phone, udid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { mClaimResponseLiveData.value = MyResponse.loading() }
.subscribe({
when (it.code()) {
200 -> mClaimResponseLiveData.value = MyResponse.success(it.body()!!)
400 -> mClaimResponseLiveData.value = MyResponse.failed(BadRequestException())
500 -> mClaimResponseLiveData.value = MyResponse.failed(ServerException())
710 -> mClaimResponseLiveData.value =
MyResponse.failed(InvalidUdidOrPhoneException())
}
}, { throwable ->
mClaimResponseLiveData.value = MyResponse.failed(throwable)
})
mCompositeDisposable.add(disposable)
}

fun phoneValidator(phoneNumber: String) {

if (ValidatorUtils.phoneValidator(phoneNumber))
mPhoneValidatorLiveData.value = MyResponse.success(ResponseStatus.PHONE_VALID)
else
mPhoneValidatorLiveData.value = MyResponse.success(ResponseStatus.PHONE_INVALID)

if (ValidatorUtils.phoneValidator(phone)) {
val disposable = userRepository.claim(phone, udid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe { mClaimResponseLiveData.value = MyResponse.loading() }
.subscribe({
when (it.code()) {
200 -> mClaimResponseLiveData.value = MyResponse.success(it.body()!!)
400 -> mClaimResponseLiveData.value = MyResponse.failed(BadRequestException())
500 -> mClaimResponseLiveData.value = MyResponse.failed(ServerException())
710 -> mClaimResponseLiveData.value =
MyResponse.failed(InvalidUdidOrPhoneException())
}
}, { throwable ->
mClaimResponseLiveData.value = MyResponse.failed(throwable)
})
mCompositeDisposable.add(disposable)
} else
mClaimResponseLiveData.value = MyResponse.failed(InvalidPhoneNumberException())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ValidatorUtils {

companion object {
//ToDo-tina min and max size of phone number
private val mPhonePatternMarcher = "[0-9 ]{5,15}".toRegex()
private val mPhonePatternMarcher = "[+0-9 ]{5,15}".toRegex()

fun phoneValidator(phoneNumber: String): Boolean {
return mPhonePatternMarcher.matches(phoneNumber)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/all_buttonselector.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/registration_buttonovalshapewhite" android:state_enabled="false"/>
<item android:drawable="@drawable/registration_buttonovalshapepurple" android:state_enabled="true"/>
</selector>
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package de.netalic.peacock.ui.registration
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import de.netalic.peacock.base.BaseTest
import de.netalic.peacock.data.exception.BadRequestException
import de.netalic.peacock.data.exception.InvalidPhoneNumberException
import de.netalic.peacock.data.exception.InvalidUdidOrPhoneException
import de.netalic.peacock.data.exception.ServerException
import de.netalic.peacock.data.model.Status
import de.netalic.peacock.data.model.UserModel
import de.netalic.peacock.data.repository.UserRepository
import de.netalic.peacock.util.LiveDataTestUtil
import de.netalic.peacock.util.ValidatorUtils
import io.reactivex.Single
import io.reactivex.subjects.PublishSubject
import okhttp3.MediaType
Expand All @@ -25,6 +27,7 @@ class PhoneInputViewModelTest : BaseTest() {

companion object {
val sUser = UserModel(mPhone = "989359323175", mUdid = "123456")
val wrongUserPhone = UserModel(mPhone = "a359323175", mUdid = "123456")
}

@get:Rule
Expand Down Expand Up @@ -149,24 +152,18 @@ class PhoneInputViewModelTest : BaseTest() {
Assert.assertNull(LiveDataTestUtil.getValue(mRegistrationViewModel.getClaimLiveData()).data)
}

@Test
fun phoneValidator_validPhoneNumber() {
val phone = "123 456"
mRegistrationViewModel.phoneValidator(phone)
Assert.assertEquals(
LiveDataTestUtil.getValue(mRegistrationViewModel.getPhoneValidatorLiveData()).data,
ResponseStatus.PHONE_VALID
)
}

@Test
fun phoneValidator_invalidPhoneNumber() {
val phone = "12a456"
mRegistrationViewModel.phoneValidator(phone)

mRegistrationViewModel.claim(wrongUserPhone.mPhone, wrongUserPhone.mUdid)
Assert.assertEquals(LiveDataTestUtil.getValue(mRegistrationViewModel.getClaimLiveData()).status, Status.FAILED)
Assert.assertEquals(
LiveDataTestUtil.getValue(mRegistrationViewModel.getPhoneValidatorLiveData()).data,
ResponseStatus.PHONE_INVALID
LiveDataTestUtil.getValue(mRegistrationViewModel.getClaimLiveData()).throwable!!::class.java,
InvalidPhoneNumberException::class.java
)
Assert.assertNull(LiveDataTestUtil.getValue(mRegistrationViewModel.getClaimLiveData()).data)
}
//
//

}

0 comments on commit 4263c1c

Please sign in to comment.