Skip to content

Instantly share code, notes, and snippets.

View Wottrich's full-sized avatar
🏃
0% Talent; 100% Hard work

Lucas Cruz Wottrich Wottrich

🏃
0% Talent; 100% Hard work
View GitHub Profile
@Wottrich
Wottrich / InitViewModelProvider.kt
Last active May 29, 2020 02:47
Init ViewModelProviders with inline function
//BaseActivity
inline fun <reified T : ViewModel> initViewModelProvider() : T {
return ViewModelProviders.of(this)[T::class.java]
}
//Activity
private val viewModel : YourViewModel by lazy {
initViewModelProvider<YourViewModel>()
}
@Wottrich
Wottrich / InfixLiveDataObserver.kt
Last active May 29, 2020 02:46
Custom LiveData example with infix function
//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
@Wottrich
Wottrich / build.gradle
Created February 28, 2020 20:11
[Android] Organize your gradle dependencies
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 = [
// build.gradle(app level)
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: "androidx.navigation.safeargs"
android {
compileOptions {
sourceCompatibility rootProject.ext.java_version
targetCompatibility rootProject.ext.java_version
}
// build.gradle(app level)
android {
compileOptions {
sourceCompatibility rootProject.ext.java_version
targetCompatibility rootProject.ext.java_version
}
}
dependencies {
//...
//Implementation
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
SessionManager.initialize(this)
}
}
//Use
//Interface API
interface UserAPI {
suspend fun fetchUserFromService(): User
}
//Service
class UserService(
private val api: UserAPI = Retrofit.create<User>()//implementação de exemplo
) {
@Wottrich
Wottrich / AppUpdateInfoWrapper.kt
Last active May 11, 2021 11:20
InAppUpdates AppUpdateInfoWrapper pt1
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
@Wottrich
Wottrich / AppUpdatePertinentInfo.kt
Created May 11, 2021 10:55
InAppUpdate AppUpdatePertinentInfo pt1
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
@Wottrich
Wottrich / AppUpdateInfoWrapperImplExemple.kt
Created May 11, 2021 10:57
InAppUpdates AppUpdateInfoWrapper Implementation example pt1
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
}