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 / SwipeToDeleteItem.kt
Created May 9, 2022 19:55
SwipeToDeleteItem -> Compose 1.2.0-alpha8
SwipeToDeleteItem(
onDeleteItem = { onDeleteTask(task) },
onBackground = { state -> BackgroundSwipeToDeleteItem(state) },
onDismissContent = {
TaskItem(
task = task,
showDeleteItem = showDeleteItem,
onCheckChange = onCheckChange,
onDeleteTask = onDeleteTask
)
@Wottrich
Wottrich / Event.kt
Created March 4, 2022 16:26
Used as a wrapper for data that is exposed via a LiveData that represents an event.
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
open class Event<out T>(private val content: T) {
@Suppress("MemberVisibilityCanBePrivate")
var hasBeenHandled = false
private set // Allow external read but not write
/**
@Wottrich
Wottrich / SingleLiveEvent.kt
Created January 7, 2022 20:39
SingleLiveEvent example
import androidx.annotation.MainThread
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean
class SingleLiveEvent<T> : MutableLiveData<T> {
constructor() : super()
constructor(initialValue: T) : super(initialValue)
@Wottrich
Wottrich / LauncherActivity.kt
Last active May 11, 2021 11:33
InAppUpdates LauncherActivity pt1
class LauncherActivity : AppCompactActivity() {
private val inAppUpdateViewModel by viewModel<InAppUpdateViewModel>()
private val appUpdateInfoProvider by inject<AppUpdateInfoWrapper>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Essa feature não funciona em DEBUG,
//então precisamos fazer isso para conseguir utilizar o ambiente de dev
if (BuildConfig.DEBUG) {
@Wottrich
Wottrich / PlayCoreModuleKoin.kt
Last active May 12, 2021 02:41
InAppUpdates PlayCoreModule kotin implementation pt1
val playCoreModule = module {
single { AppUpdateInfoWrapper(androidContext()) }
single<AppUpdateInfoDefinitions> { AppUpdateInfoDefinitionsImpl() }
factory<InAppUpdateStatusIterator> { InAppUpdateStatusIteratorImpl(get(), get()) }
viewModel { InAppUpdateViewModel(get()) }
}
@Wottrich
Wottrich / InAppUpdateViewModel.kt
Last active May 11, 2021 11:29
InAppUpdates InAppUpdateViewModel pt1
class InAppUpdateViewModel(
private val inAppUpdateStatusIterator: InAppUpdateStatusIterator
) : ViewModel() {
private val _inAppUpdateAction = MediatorLiveData<Resource<InAppUpdateStatus>>()
fun getAppUpdateActionLiveData(): LiveData<Resource<InAppUpdateStatus>> {
if (_inAppUpdateAction.value == null) {
checkAvailableVersion()
}
@Wottrich
Wottrich / AppUpdateInfoWrapper2.kt
Created May 11, 2021 11:04
InAppUpdates AppUpdateInfoWrapper pt2
class AppUpdateInfoWrapper(context: Context) {
private val appUpdateManager: AppUpdateManager = AppUpdateManagerFactory.create(context)
private var appUpdateInfo: AppUpdateInfo? = null
suspend fun getAppUpdateInfo(): Resource<AppUpdatePertinentInfo> {
//...
}
@Throws(IllegalStateException::class)
@Wottrich
Wottrich / InAppUpdateStatusIteratorImpl.kt
Last active May 11, 2021 11:27
InAppUpdate InAppUpdateStatusIteratorImpl pt1
class InAppUpdateStatusIteratorImpl(
appUpdateInfoWrapper: AppUpdateInfoWrapper,
definitions: AppUpdateInfoDefinitions
) : InAppUpdateStatusIterator(appUpdateInfoWrapper, definitions) {
override fun getSuccessInAppUpdateStatus(definitions: AppUpdateInfoDefinitions): InAppUpdateStatus {
return when {
//Utilizamos shouldNotifyUpdateVersion para saber se precisamos notificar uma atualização
definitions.shouldNotifyUpdateVersion() -> getAvailableVersionUpdate(definitions)
else -> InAppUpdateStatus.NoNeedUpdate
@Wottrich
Wottrich / InAppUpdateStatusIterator.kt
Last active May 11, 2021 11:27
InAppUpdates InAppUpdateStatusIterator abstract class pt1
abstract class InAppUpdateStatusIterator(
private val appUpdateInfoWrapper: AppUpdateInfoWrapper,
private val definitions: AppUpdateInfoDefinitions
) {
suspend fun getInAppUpdateStatus(): Resource<InAppUpdateStatus> {
val resourceDefinitions = appUpdateInfoWrapper.getAppUpdateInfo().mapNotNull { appUpdatePertinentInfo ->
definitions.apply {
setupAppUpdatePertinentInfo(appUpdatePertinentInfo)
}
@Wottrich
Wottrich / AppUpdateInfoDefinitionsImpl.kt
Created May 11, 2021 11:00
InAppUpdates AppUpdateInfoDefinitionsImpl pt1
class AppUpdateInfoDefinitionsImpl : AppUpdateInfoDefinitions() {
override fun shouldNotifyUpdateVersion() =
(hasUpdateAvailable() && !isNoNotifyUpdateVersion())
private fun hasUpdateAvailable() = /*UpdateAvailability.UPDATE_AVAILABLE*/
appUpdatePertinentInfo?.updateAvailability() == appUpdatePertinentInfo?.updateAvailableValue
private fun isNoNotifyUpdateVersion() =
appUpdatePertinentInfo?.updatePriority() == NO_NOTIFY