Skip to content

Instantly share code, notes, and snippets.

Avatar
🌞
🇧🇷 ➡️ 🇳🇱

Iago Mendes Fucolo ifucolo

🌞
🇧🇷 ➡️ 🇳🇱
View GitHub Profile
View request.kt
//Não esquecer de verificar se é o device utilza Android 5.0 ou mais
//se seu app tiver minSdk < 21
val request = reviewManager.requestReviewFlow()
request.addOnCompleteListener { result ->
if (request.isSuccessful) {
// We got the ReviewInfo object
val reviewInfo: ReviewInfo = result.result
val flow = reviewManager.launchReviewFlow(this, reviewInfo)
View ReviewManager.kt
val reviewManager: ReviewManager = ReviewManagerFactory.create(this)
View activity.kt
private static final String GOOGLE_PLAY = "https://play.google.com/store/apps/details?id=";
fun intentForGooglePlayStore(context: Context): Intent {
val uri: Uri = Uri.parse(UriHelper.GOOGLE_PLAY + context.packageName)
return Intent(Intent.ACTION_VIEW, uri)
}
//...
startActivity(IntentHelper.intentForGooglePlayStore(this))
View loadState.kt
var loadState: LoadState = LoadState.Done
set(value) {
when (field) {
value -> {
notifyItemChanged(0)
}
is LoadState.Loading -> {
itemsCount = 0
notifyItemRemoved(0)
}
View LoadStateAdapter.kt
class LoadStateAdapter : RecyclerView.Adapter<LoadStateAdapter.LoadStateViewHolder>() {
var loadState: LoadState = LoadState.Done
set(value) {
when (field) {
value -> {
notifyItemChanged(0)
}
is LoadState.Loading -> {
itemsCount = 0
View LoadState.kt
sealed class LoadState {
object Loading: LoadState()
object Done: LoadState()
}
View setupViewModel.kt
private fun setupViewModel() {
viewModel.getJobs()
viewModel.viewJobsStatesLiveData.observe(this, Observer { state ->
state.getContentIfNotHandled()?.let {
hideAll()
when(it) {
is AndroidJobListViewModel.ViewJobsStates.Show -> showContent(it.list)
AndroidJobListViewModel.ViewJobsStates.Empty -> showEmptyState()
View views.kt
private fun showError() {
binding.btnTryAgain.show()
}
private fun showEmptyState() {
binding.txtEmpty.show()
}
private fun showContent(list: List<AndroidJob>) = with(androidJobsAdapter) {
jobs = list
View hideAll.kt
private fun hideAll() {
listOf(binding.btnTryAgain, binding.txtEmpty, binding.recyclerView, binding.progressBar).map {
it.hide()
}
}
View getJobs4.kt
fun getJobs() {
viewModelScope.launch(Dispatchers.IO) {
jobsUseCase.getJobs().collect {
val resultJobs = when(it) {
is GetJobsUseCases.ResultJobs.Jobs -> ViewJobsStates.Show(it.list)
GetJobsUseCases.ResultJobs.NoJobs -> ViewJobsStates.Empty
GetJobsUseCases.ResultJobs.Error -> ViewJobsStates.Error
}
_viewJobsStatesLiveData.postValue(Event(resultJobs))