LiveDataCallAdapter - Live Data Call Adapter Factory

Overview

LiveDataCallAdapterFactory

based on retrofit, the LiveData returned by the restfull api, can be used to manually re request data via net (配合retrofit使用,让网络请求返回的livedata可以手动重新发起网络请求)

use like that:

0

root build.gradle: repositories { ... maven { url "https://jitpack.io" } } app build.gradle: implementation 'com.github.jj532655203:LiveDataCallAdapter:1.0.0-beta.2'

1

companion object {
    private const val BASE_URL = "https://baseUrl/"
    fun create(): TestApi {
        val logger = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
            Log.d("API", it + Thread.currentThread().name)
        })
        logger.level = HttpLoggingInterceptor.Level.BASIC

        val client = OkHttpClient.Builder()
            .addInterceptor(logger)
            .build()
        return Retrofit.Builder()
            .baseUrl(HttpUrl.parse(BASE_URL)!!)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(LiveDataCallAdapterFactory())
            .build()
            .create(TestApi::class.java)
    }
}

2

class MainRepository {

fun getTest(): RetrofitLiveData<ApiResponse<TestApi.MyResponse>> {
    return TestApi.create().getTest()
}

companion object {
    @Volatile
    private var instance: MainRepository? = null

    fun getInstance() = instance ?: synchronized(this) {
        instance ?: MainRepository().also { instance = it }
    }

}

}

3

class MainViewModel internal constructor(mainRepository: MainRepository): ViewModel() { val testLiveData: RetrofitLiveData<ApiResponse<TestApi.MyResponse>> = mainRepository.getTest() }

4

class MainActivity : AppCompatActivity() {

private var mTestRefresh: IApiRefresh? = null

private val viewModel: MainViewModel by viewModels {
    InjectorUtils.provideMainViewModelFactory()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val tv: TextView = findViewById(R.id.tv)

    viewModel.testLiveData.observe(this) { result ->
        Log.d("MainActivity", "$result")
        tv.setText(result.toString())
    }

    mTestRefresh = viewModel.testLiveData
    tv.setOnClickListener {
        refreshData()
    }
}

private fun refreshData() {
    mTestRefresh?.refresh()
}

}

You might also like...
BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing).

BindsAdapter BindsAdapter is an Android library to help you create and maintain Adapter class easier via ksp( Kotlin Symbol Processing). Installation

Demo Spting REST Service on Kotlin. Works with PostgreSQL via Spring Data. Data initialization provided by liquibase

Spring Boot REST API with Kotlin Spring Boot REST API service. Spring Data with PostgreSQL. Data initialization with Liquibase. Swagger UI Reference D

Detailing about the data provided (Data Visualization Application)

Detailing about the data provided (Data Visualization Application): • In the application, the data provided in the CSV is used for the Scatter plot cr

Algorithms and data structures in Kotlin.
Algorithms and data structures in Kotlin.

Here you can find the most common algorithms and data structures written in Kotlin. The goal of this project is to create the most eloquent implementa

Small kotlin library for persisting _single instances_ of kotlin data classes
Small kotlin library for persisting _single instances_ of kotlin data classes

PerSista Small library for persisting single instances of kotlin data classes. NB: PerSista uses typeOf() internally which is marked as @ExperimentalS

A clean-aesthetically pleasing Measuring Application, which uses relevant sensors-converts raw sensor data into human readable formatted outputs-and displays accurate measurements.
A clean-aesthetically pleasing Measuring Application, which uses relevant sensors-converts raw sensor data into human readable formatted outputs-and displays accurate measurements.

Measure App A clean-aesthetically pleasing Measuring Application, which uses relevant sensors-converts raw sensor data into human readable formatted o

Multi-thread ZX0 data compressor in Kotlin

ZX0-Kotlin ZX0-Kotlin is a multi-thread implementation of the ZX0 data compressor in Kotlin. Requirements To run this compressor, you must have instal

This repository is a simple humidity and temperature dashboard to present data from sensors on your phone
This repository is a simple humidity and temperature dashboard to present data from sensors on your phone

ChilliBook This repository is a simple humidity and temperature dashboard to present data from sensors on your phone. It uses Bluetooth LE and an ESP3

An AutoValue extension that generates binary and source compatible equivalent Kotlin data classes of AutoValue models.
An AutoValue extension that generates binary and source compatible equivalent Kotlin data classes of AutoValue models.

AutoValue Kotlin auto-value-kotlin (AVK) is an AutoValue extension that generates binary-and-source-compatible, equivalent Kotlin data classes. This i

Releases(1.0.0-beta.2)
Owner
Jay Wang
Coding...
Jay Wang
Fixture factory in Kotlin

KFactory Create best-in-class factories for your synthetic data in Kotlin. ⭐ Test fixtures ⭐ DB seeding ⭐ Feature demos ⭐ Pre-production environments

Blueground 51 Dec 20, 2022
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin

AbstractFactoryDesignPatternWithKotlin Abstract Factory Design Pattern With Kotl

Harun Kör 2 Jun 16, 2022
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin

AbstractFactoryDesignPatternWithKotlin Abstract Factory Design Pattern With Kotl

Harun Kör 1 Jan 2, 2022
Block unknown callers with this call screening service.

Silence Block unknown callers with this call screening service. By default numbers not in your contact list are blocked. Optionally allow: Numbers you

lucky 162 Jan 5, 2023
📞 Remote call sample android app using RemoteMonster

?? Remote call sample android app using RemoteMonster

Robin 1 Mar 17, 2022
Automatically generates UI demos which allow users to call any function with any parameters

Automatically generates UI demos which allow users to call any function (including composable ones) with any parameters. Useful for building demo screens in playground apps of various design systems.

Anton Popov 3 Jul 28, 2022
A Basic Call Recorder for rooted Android devices

Basic Call Recorder BCR is a simple Android call recording app for rooted devices or devices running custom firmware. Once enabled, it stays out of th

Andrew Gunnerson 420 Jan 7, 2023
Live currency app in Uzbek sum

currencyApp Pro currency rate App Currency Converter providing real time exchange rates for over 180+ currencies and countries.  You can setup persona

Ro'ziboyev Ismoil 3 Dec 29, 2021
Live-coding a web server with Ktor

ktor-sample Live-coding a web server with Ktor Ktor is a Kotlin framework dedicated to building asynchronous servers and clients in connected systems.

Renaud Mathieu 1 May 10, 2022
Amazon IVS Live to VOD (DVR) Android demo

A demo Android app showing how to implement a Live to VOD (DVR) experience using Amazon IVS and the auto-record-to-s3 feature using Amazon S3.

AWS Samples 4 Jan 3, 2023