Last active
May 11, 2021 11:27
-
-
Save Wottrich/96f13bf2bf97977bb378993afba70d5c to your computer and use it in GitHub Desktop.
InAppUpdates InAppUpdateStatusIterator abstract class pt1
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
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) | |
} | |
} | |
return resourceDefinitions.inAppUpdateStateWrapper() | |
} | |
private fun Resource<AppUpdateInfoDefinitions>.inAppUpdateStateWrapper(): Resource<InAppUpdateStatus> { | |
return mapNotNull { definitions -> | |
getSuccessInAppUpdateStatus(definitions) | |
} | |
} | |
abstract fun getSuccessInAppUpdateStatus(definitions: AppUpdateInfoDefinitions): InAppUpdateStatus | |
} | |
//mapNotNull é um extension de Resource, apenas para conhecimento pessoal: | |
inline fun <R> mapNotNull(mapBlock: (T) -> R?): Resource<R> { | |
return map { | |
if (it != null) { | |
mapBlock(it) | |
} else null | |
} | |
} | |
inline fun <R> map(mapBlock: (T?) -> R?): Resource<R> { | |
return when (this) { | |
is Error -> error(throwable) | |
is Loading -> loading() | |
is Success -> { | |
val r = mapBlock(data) | |
success(r) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment