This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
public class SpacesItemDecoration extends RecyclerView.ItemDecoration { | |
private final int leftSpace; | |
private final int topSpace; | |
private final int rightSpace; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Data(val mail: String, val phone: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE)!! | |
val VALID_PHONE_NUMBER_ADDRESS_REGEX = Pattern.compile("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}", Pattern.CASE_INSENSITIVE)!! | |
fun validMail(input: String): Boolean { | |
val matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(input) | |
return matcher.find() | |
} | |
fun validNumber(input: String): Boolean { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:background="@color/primary_light" | |
tools:context="com.danielecampogiani.androidfunctionalvalidation.MainActivity"> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun String.optionMail(): Option<String> = | |
when { | |
validMail(this) -> this.some() | |
else -> Option.None | |
} | |
fun String.optionPhoneNumber(): Option<String> = | |
when { | |
validNumber(this) -> this.some() | |
else -> Option.None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OptionViewModel : ViewModel() { | |
private val mutableValidation: MutableLiveData<Option<Data>> = MutableLiveData() | |
val validation: LiveData<Option<Data>> | |
get() = mutableValidation | |
fun validate(mail: String, phoneNumber: String) { | |
val validationResult : Option<Data> = validateData(mail, phoneNumber) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OptionActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_input) | |
val viewModel = ViewModelProviders.of(this)[OptionViewModel::class.java] | |
signup_button.setOnClickListener { | |
viewModel.validate(email.text.toString(), number.text.toString()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun handleNone() { | |
val message = getString(R.string.option_wrong) | |
showMessage(message) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun handleSome(data: Data) { | |
val message = "Mail: ${data.mail} Phone: ${data.phone}" | |
showMessage(message) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun String.eitherMail(): Either<String, String> = | |
when { | |
validMail(this) -> this.right() | |
else -> "Invalid email".left() | |
} | |
fun String.eitherPhoneNumber(): Either<String, String> = | |
when { | |
validNumber(this) -> this.right() | |
else -> "Invalid phone number".left() |
OlderNewer