Dagger Hilt, MVP Moxy, Retrofit, Kotlin coroutine, Sealed class

Related tags

Utility DemoProject
Overview

DemoProject

Dagger Hilt, MVP Moxy, Retrofit, Kotlin coroutine, Sealed class

//Presenterda ma'lumotlarni handler qilish

    override fun loadPost() {
    viewState.showRefresh()
    presenterScope.launch {
        try {
            repository.loadPost().onData {
                viewState.addPosts(it)
            }.onResource {
                viewState.showMessage(it)
            }.onMessage {
                viewState.showMessage(it)
            }.onFailure {
            }
        } catch (e: Exception) {
            e.printStackTrace()
            viewState.onFail()
        } finally {
            viewState.hideRefresh()
        }
    }
}

//Api module(Dagger Hilt)

@Module
@InstallIn(SingletonComponent::class)
class ApiModule {

@Provides
@Singleton
fun getPostApi(retrofit: Retrofit): PostApi = retrofit.create(PostApi::class.java)
}

//Retrofit module(Dagger Hilt)

@Module
@InstallIn(SingletonComponent::class)
class RetrofitModule {
@Provides
@Singleton
fun getRetrofit(): Retrofit = Retrofit.Builder()
    .baseUrl(BASE_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build()
 }

//Repository

class PostRepository @Inject constructor(private val api: PostApi) : IPostContract.Model {
override suspend fun loadPost(): ResultData
   
   
    
    
     
     > {
    val response = api.loadPost()

    when (response.code()) {
        200 -> {
            return ResultData.data(response.body()!!)
        }
        404 -> {
            return ResultData.resource(R.string.not_found)
        }
        in 500..600 -> {
            return ResultData.resource(R.string.internal_server_error)
        }
        else -> return if (response.body() == null) {
            ResultData.resource(R.string.server_error)
        } else {
            ResultData.message(response.body().toString())
        }
    }
}
}

    
    
   
   
You might also like...
Fuzzy string matching for Kotlin (JVM, native, JS, Web Assembly) - port of Fuzzy Wuzzy Python lib

FuzzyWuzzy-Kotlin Fuzzy string matching for Kotlin (JVM, iOS) - fork of the Java fork of of Fuzzy Wuzzy Python lib. For use in on JVM, Android, or Kot

🐫🐍🍢🅿 Multiplatform Kotlin library to convert strings between various case formats including Camel Case, Snake Case, Pascal Case and Kebab Case

KaseChange Multiplatform Kotlin library to convert strings between various case formats Supported Case Formats SCREAMING_SNAKE_CASE snake_case PascalC

Multiplaform kotlin library for calculating text differences. Based on java-diff-utils, supports JVM, JS and native targets.

kotlin-multiplatform-diff This is a port of java-diff-utils to kotlin with multiplatform support. All credit for the implementation goes to original a

recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.
recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Item Helper For Kotlin

Item Helper For Kotlin

kotlin mvvm+dataBinding+retrofit2+Arouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装
kotlin mvvm+dataBinding+retrofit2+Arouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装

kotlin-mvvm kotlin mvvm+dataBinding+retrofit2+ARouter等BaseActivity、BaseFragment、BaseDialogFragment基类封装 Android开发项目基本使用框架,封装了各类组件,在基类实现了沉浸式状态栏,可以自己更改颜色

 Bar Service Kotlin Client
Bar Service Kotlin Client

A simple starter service client written in Kotlin against generated models (protos)A simple starter service client written in Kotlin against generated models (protos)

Kotlin validation with a focus on readability
Kotlin validation with a focus on readability

kommodus Kotlin validation with a focus on readability import com.github.kommodus.constraints.* import com.github.kommodus.Validation data class Test

Owner
Dostonjon
Dostonjon
Small Android library to help you incorporate MVP, Passive View and Presentation Model patterns in your app

DroidMVP About DroidMVP is a small Android library to help you incorporate the MVP pattern along with Passive View and Presentation Model (yes, those

Andrzej Chmielewski 225 Nov 29, 2022
Kotlin matrix class which supports determinant, inverse matrix, matmul, etc.

Kotrix is a set of classes that helps people dealing with linear algebra in Kotlin.

Kanguk Lee 5 Dec 8, 2022
A logger with a small, extensible API which provides utility on top of Android's normal Log class.

This is a logger with a small, extensible API which provides utility on top of Android's normal Log class. I copy this class into all the little apps

Jake Wharton 9.8k Dec 30, 2022
Wrapper around the android Camera class that simplifies its usage

EasyCamera Wrapper around the android Camera class that simplifies its usage (read more about the process) Usage: // the surface where the preview wil

Bozhidar Bozhanov 641 Dec 29, 2022
BinGait is a tool to disassemble and view java class files, developed by BinClub.

BinGait Tool to diassemble java class files created by x4e. Usage To run BinGait, run java -jar target/bingait-shadow.jar and BinGait will launch. If

null 18 Jul 7, 2022
A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.

##fb-android-dagger A set of helper classes for using dagger with Android components such as Applications, Activities, Fragments, BroadcastReceivers,

Andy Dennie 283 Nov 11, 2022
A cup of library to Parse RSS for android. Also available as a ConverterFactory for Retrofit & Fuel

ParseRSS RSS Parser for android Simple, concise, and extensible RSS Parser in the entire coffee shop. It can capture these information from the RSS ar

Muhammad Rifqi Fatchurrahman 23 Dec 10, 2022
Remove the dependency of compiled kotlin on kotlin-stdlib

Dekotlinify This project aims to take compiled Kotlin Java bytecode (compiled by the standard Kotlin compiler) and remove all references to the Kotlin

Joseph Burton 10 Nov 29, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 31, 2022
A DSL to handle soft keyboard visibility change event written in Kotlin.

About A DSL to handle soft keyboard visibility change event written in Kotlin. How to use? Step 1. Add it in your root build.gradle at the end of repo

Vinícius Oliveira 17 Jan 7, 2023