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
//BaseActivity | |
inline fun <reified T : ViewModel> initViewModelProvider() : T { | |
return ViewModelProviders.of(this)[T::class.java] | |
} | |
//Activity | |
private val viewModel : YourViewModel by lazy { | |
initViewModelProvider<YourViewModel>() | |
} |
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
//View Model | |
var name = MutableLiveData<String>() | |
//BaseActivity - Code to be easier | |
infix fun <T> MutableLiveData<T>.observer(completion: (T?) -> Unit) { | |
this.observe(this@BaseActivity, Observer(completion)) | |
} | |
//Activity | |
viewModel.name observer this::onNameChange |
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
ext { | |
defaultLinks = [ | |
kotlin : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version", | |
appCompat : 'androidx.appcompat:appcompat:1.0.2', | |
core : 'androidx.core:core-ktx:1.0.2', | |
constraintLayout : 'androidx.constraintlayout:constraintlayout:1.1.3' | |
] | |
testLinks = [ |
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
//Interface API | |
interface UserAPI { | |
suspend fun fetchUserFromService(): User | |
} | |
//Service | |
class UserService( | |
private val api: UserAPI = Retrofit.create<User>()//implementação de exemplo | |
) { |
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 AppUpdateInfoWrapper(context: Context) { | |
private val appUpdateManager: AppUpdateManager = AppUpdateManagerFactory.create(context) | |
private var appUpdateInfo: AppUpdateInfo? = null | |
suspend fun getAppUpdateInfo(): Resource<AppUpdatePertinentInfo> { | |
return suspendCancellableCoroutine { continuation -> | |
//Pegando o AppUpdateInfoTask | |
val appUpdateInfoTask = appUpdateManager.appUpdateInfo |
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 AppUpdatePertinentInfo(private val appUpdateInfo: AppUpdateInfo) { | |
val updateAvailableValue: Int | |
get() = UpdateAvailability.UPDATE_AVAILABLE | |
val installStatusDownloadedValue: Int | |
get() = InstallStatus.DOWNLOADED | |
val developerTriggeredUpdateInProgressValue: Int | |
get() = UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS |
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 appUpdateInfoWrapper = AppUpdateInfoWrapper(context) | |
val appUpdateInfoResource = appUpdateInfoWrapper.getAppUpdateInfo() | |
when(appUpdateInfoResource) { | |
Success -> //TODO | |
Error -> //TODO | |
Loading -> Unit//Não temos loading nesse caso mas poderia ser utilizado também | |
} |
OlderNewer