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
implementation 'com.android.support:recyclerview-v7:28.0.0' | |
implementation 'com.android.support:design:28.0.0' | |
def room_version = "1.1.1" | |
implementation "android.arch.persistence.room:runtime:$room_version" | |
annotationProcessor "android.arch.persistence.room:compiler:$room_version" |
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
@Target(AnnotationTarget.FUNCTION) | |
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME) | |
@MapKey | |
annotation class ViewModelKey(val value: KClass<out ViewModel>) | |
@Module | |
abstract class ViewModelModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey(MainViewModel::class) |
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 ViewModelFactory<T : ViewModel> @Inject constructor(private val viewModelProvider: Provider<T>) : | |
ViewModelProvider.Factory { | |
@Suppress("UNCHECKED_CAST") | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return viewModelProvider.get() as T | |
} | |
} | |
// usage in the client code |
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 MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val button: Button = findViewById(R.id.button) | |
button.setOnClickListener { | |
startActivity(Intent(this, DetailsActivity::class.java)) | |
} |
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 MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
phoneNumberEditText.addTextChangedListener(object : TextWatcher { | |
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) { | |
} |
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
dependencies { | |
implementation 'io.michaelrocks:libphonenumber-android:8.10.16' | |
} |
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 MainActivity : AppCompatActivity() { | |
private lateinit var phoneNumberUtil: PhoneNumberUtil | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
phoneNumberUtil = PhoneNumberUtil.createInstance(this) | |
} | |
} |
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 getCountryIsoCode(number: String): String? { | |
val validatedNumber = if (number.startsWith("+")) number else "+$number" | |
val phoneNumber = try { | |
phoneNumberUtil.parse(validatedNumber, null) | |
} catch (e: NumberParseException) { | |
Log.e(TAG, "error during parsing a number") | |
null | |
} | |
if(phoneNumber == null) return null |
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
override fun afterTextChanged(editable: Editable?) { | |
val number = editable.toString() | |
val countryIsoCode = getCountryIsoCode(number) | |
countryTextView.text = countryIsoCode ?: "Can't detect a country" | |
} |
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
package com.sharyfire.countryphonedetector | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.text.Editable | |
import android.text.TextWatcher | |
import android.util.Log | |
import io.michaelrocks.libphonenumber.android.NumberParseException | |
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil | |
import io.michaelrocks.libphonenumber.android.Phonenumber.PhoneNumber |
OlderNewer