如何将模型参数传递给Kotlin中的MutableLiveData?

我正在开发新闻,并且已经用Koin实现了Kotlin协程,我已经成功创建了模块类,但是无法将正确的参数传递给ViewModel类,因此当我从服务器成功获取数据时,它应该显示来自服务器的数据没有数据时显示错误消息

在我的模块类下面

const val BASE_URL  = "https://newsapi.org/"

val appModules = module {
    // The Retrofit service using our custom HTTP client instance as a singleton
    single {
        createWebService<SportNewsInterface>(
            okHttpClient = createHttpClient(),factory = RxJava2CallAdapterFactory.create(),baseUrl = BASE_URL
        )
    }
    // Tells Koin how to create an instance of CatRepository
    factory<NewsRepository> { (NewsRepositoryImpl( sportNewsInterface= get())) }
    // Specific viewModel pattern to tell Koin how to build MainViewModel
    viewModel { MainViewModel(sportNewsInterface = get())  }
}

/* Returns a custom OkHttpClient instance with interceptor. Used for building Retrofit service */
fun createHttpClient(): OkHttpClient {
    val client = OkHttpClient.Builder()
    client.readTimeout(5 * 60,TimeUnit.SECONDS)
    return client.addInterceptor {
        val original = it.request()
        val requestBuilder = original.newBuilder()
        requestBuilder.header("Content-Type","application/json")
        val request = requestBuilder.method(original.method(),original.body()).build()
        return@addInterceptor it.proceed(request)
    }.build()
}

/* function to build our Retrofit service */
inline fun <reified T> createWebService(
    okHttpClient: OkHttpClient,factory: CallAdapter.Factory,baseUrl: String
): T {
    val retrofit = Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(GsonConverterFactory.create(GsonBuilder().setLenient().create()))
        .addCallAdapterFactory(CoroutineCallAdapterFactory())
        .addCallAdapterFactory(factory)
        .client(okHttpClient)
        .build()
    return retrofit.create(T::class.java)
}

在MainViewModel.kt下面,我在这里称Kotlin协程

class MainViewModel(
    private val sportNewsInterface: SportNewsInterface

) : ViewModel(),Coroutinescope {
    // Coroutine's background job
    private val job = Job()
    // Define default thread for Coroutine as Main and add job
    override val coroutineContext: CoroutineContext = Dispatchers.Main + job

    private val showLoading = MutableLiveData<Boolean>()
    private val sportList = MutableLiveData <List<SportNewsResponse>>()
    val showError = SingleLiveEvent<String>()

    fun loadNews() {
        // Show progressBar during the operation on the MAIN (default) thread
        showLoading.value = true
        // launch the Coroutine
        launch {
            // Switching from MAIN to IO thread for API operation
            // Update our data list with the new one from API
            val result = withContext(Dispatchers.IO) { sportNewsInterface.getNews() }
            // Hide progressBar once the operation is done on the MAIN (default) thread
            showLoading.value = false
            when (result) {


                is UseCaseResult.Success<List<SportNewsResponse> -> sportList.value = result.data
                is UseCaseResult.Error -> showError.value = result.exception.message
                 }
                }
            }



    override fun onCleared() {
        super.onCleared()
        // Clear our job when the linked activity is destroyed to avoid memory leaks
        job.cancel()
    }
}

在UseCaseResult.kt下面

sealed class UseCaseResult<out T : Any> {
    class Success<out T : Any>(val data: T) : UseCaseResult<<List<SportNewsResponse>>>()
    class Error(val exception: Throwable) : UseCaseResult<Nothing>()
}

在SportInterface下面

interface SportNewsInterface {

    @GET("v2/top-headlines?country=us&apiKey=da331087e3f3462bb534b3b0917cbee9")
     suspend fun  getNews(): List<SportNewsResponse>

    @GET("/v2/top-headlines?sources=espn&apiKey=da331087e3f3462bb534b3b0917cbee9")
    fun getEspn(): Deferred<List<SportNewsResponse>>

    @GET("/v2/top-headlines?sources=football-italia&apiKey=da331087e3f3462bb534b3b0917cbee9")
    fun getFootballItalia(): Deferred<List<SportNewsResponse>>

    @GET("/v2/top-headlines?sources=bbc-sport&apiKey=da331087e3f3462bb534b3b0917cbee9")
    fun getBBCSport(): Deferred<List<SportNewsResponse>>


        }

在Model类下面

data class SportNewsResponse(
    val articles: List<Article>,val status: String,val totalResults: Int
)
cathylia 回答:如何将模型参数传递给Kotlin中的MutableLiveData?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3156211.html

大家都在问