-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from dcampogiani/feature/either
Either implementation
- Loading branch information
Showing
7 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
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
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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/danielecampogiani/androidfunctionalvalidation/either/EitherActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.danielecampogiani.androidfunctionalvalidation.either | ||
|
||
import android.arch.lifecycle.Observer | ||
import android.arch.lifecycle.ViewModelProviders | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import android.widget.Toast | ||
import com.danielecampogiani.androidfunctionalvalidation.Data | ||
import com.danielecampogiani.androidfunctionalvalidation.R | ||
import kotlinx.android.synthetic.main.activity_input.* | ||
|
||
class EitherActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_input) | ||
|
||
val viewModel = ViewModelProviders.of(this)[EitherViewModel::class.java] | ||
|
||
signup_button.setOnClickListener { | ||
viewModel.validate(email.text.toString(), number.text.toString()) | ||
} | ||
|
||
viewModel.validation.observe(this, Observer { | ||
it?.fold(this::handleLeft, this::handleRight) | ||
}) | ||
} | ||
|
||
private fun handleLeft(error: String) { | ||
showMessage(error) | ||
} | ||
|
||
private fun handleRight(data: Data) { | ||
val message = "Mail: ${data.mail} Phone: ${data.phone}" | ||
showMessage(message) | ||
} | ||
|
||
private fun showMessage(message: String) { | ||
Toast.makeText(this, message, Toast.LENGTH_SHORT).show() | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...src/main/java/com/danielecampogiani/androidfunctionalvalidation/either/EitherViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.danielecampogiani.androidfunctionalvalidation.either | ||
|
||
import android.arch.lifecycle.LiveData | ||
import android.arch.lifecycle.MutableLiveData | ||
import android.arch.lifecycle.ViewModel | ||
import com.danielecampogiani.androidfunctionalvalidation.Data | ||
import kategory.Either | ||
|
||
class EitherViewModel : ViewModel() { | ||
|
||
private val mutableValidation: MutableLiveData<Either<String, Data>> = MutableLiveData() | ||
|
||
val validation: LiveData<Either<String, Data>> | ||
get() = mutableValidation | ||
|
||
|
||
fun validate(mail: String, phoneNumber: String) { | ||
mutableValidation.value = validateData(mail, phoneNumber) | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/danielecampogiani/androidfunctionalvalidation/either/functions.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.danielecampogiani.androidfunctionalvalidation.either | ||
|
||
import com.danielecampogiani.androidfunctionalvalidation.Data | ||
import com.danielecampogiani.androidfunctionalvalidation.validMail | ||
import com.danielecampogiani.androidfunctionalvalidation.validNumber | ||
import kategory.* | ||
|
||
fun validateData(mail: String, phoneNumber: String): Either<String, Data> { | ||
|
||
return Either.applicative<String>().map2(mail.optionMail(), phoneNumber.optionPhoneNumber()) { | ||
Data(it.a, it.b) | ||
}.ev() | ||
|
||
} | ||
|
||
private fun String.optionMail(): Either<String, String> = | ||
when { | ||
validMail(this) -> this.right() | ||
else -> "Invalid email".left() | ||
} | ||
|
||
private fun String.optionPhoneNumber(): Either<String, String> = | ||
when { | ||
validNumber(this) -> this.right() | ||
else -> "Invalid phone number".left() | ||
} |
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
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