From 611832a050a45e5dd9ee535883ddde7aa374c24c Mon Sep 17 00:00:00 2001 From: KunMinX Date: Mon, 15 Jul 2024 14:09:51 +0800 Subject: [PATCH] update data flow --- .../purenote/data/repo/DataRepository.kt | 50 +++++++------------ .../purenote/domain/intent/NoteIntent.kt | 4 ++ .../purenote/domain/request/NoteRequester.kt | 50 +++++++++++++------ .../domain/request/WeatherRequester.kt | 9 ++-- 4 files changed, 62 insertions(+), 51 deletions(-) diff --git a/app/src/main/kotlin/com/kunminx/purenote/data/repo/DataRepository.kt b/app/src/main/kotlin/com/kunminx/purenote/data/repo/DataRepository.kt index 0481a9d..393c4c2 100644 --- a/app/src/main/kotlin/com/kunminx/purenote/data/repo/DataRepository.kt +++ b/app/src/main/kotlin/com/kunminx/purenote/data/repo/DataRepository.kt @@ -7,8 +7,7 @@ import com.kunminx.purenote.data.bean.Weather import com.kunminx.purenote.domain.intent.Api import java.util.concurrent.TimeUnit import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Retrofit @@ -42,41 +41,30 @@ object DataRepository { .build() } - fun getNotes() = flow { - emit(RepoResult.GetNote(dataBase.noteDao().notes())) - }.flowOn(Dispatchers.IO) + suspend fun getNotes() = withContext(Dispatchers.IO) { + dataBase.noteDao().notes() + } - suspend fun insertNote(note: Note) = flow { + suspend fun insertNote(note: Note) = withContext(Dispatchers.IO) { dataBase.noteDao().insertNote(note) - emit(true) - }.flowOn(Dispatchers.IO) + } - suspend fun updateNote(note: Note) = flow { + suspend fun updateNote(note: Note) = withContext(Dispatchers.IO) { dataBase.noteDao().updateNote(note) - emit(true) - }.flowOn(Dispatchers.IO) + } - suspend fun deleteNote(note: Note) = flow { + suspend fun deleteNote(note: Note) = withContext(Dispatchers.IO) { dataBase.noteDao().deleteNote(note) - emit(true) - }.flowOn(Dispatchers.IO) + } - suspend fun getWeatherInfo( - api: String, - cityCode: String - ) = flow { - val service = mRetrofit!!.create(WeatherService::class.java) - try { - val weather = service.getWeatherInfo(api, cityCode, Api.API_KEY) - emit(RepoResult.WeatherInfo(weather.lives?.get(0)!!)) - } catch (e: Exception) { - emit(RepoResult.Error(e.message.toString())) + suspend fun getWeatherInfo(api: String, cityCode: String): Pair = + withContext(Dispatchers.IO) { + val service = mRetrofit!!.create(WeatherService::class.java) + try { + val weather = service.getWeatherInfo(api, cityCode, Api.API_KEY) + Pair(weather.lives?.get(0)!!, "") + } catch (e: Exception) { + Pair(null, e.message.toString()) + } } - }.flowOn(Dispatchers.IO) -} - -sealed class RepoResult { - data class GetNote(val notes: List) : RepoResult() - data class WeatherInfo(val live: Weather.Live) : RepoResult() - data class Error(val msg: String) : RepoResult() } diff --git a/app/src/main/kotlin/com/kunminx/purenote/domain/intent/NoteIntent.kt b/app/src/main/kotlin/com/kunminx/purenote/domain/intent/NoteIntent.kt index 6729a0f..ae020ec 100644 --- a/app/src/main/kotlin/com/kunminx/purenote/domain/intent/NoteIntent.kt +++ b/app/src/main/kotlin/com/kunminx/purenote/domain/intent/NoteIntent.kt @@ -46,4 +46,8 @@ sealed class NoteIntent { data class InitItem( val param: Note? = null ) : NoteIntent() + + data class Error( + val msg: String? = null + ) : NoteIntent() } diff --git a/app/src/main/kotlin/com/kunminx/purenote/domain/request/NoteRequester.kt b/app/src/main/kotlin/com/kunminx/purenote/domain/request/NoteRequester.kt index 464e825..3d10208 100644 --- a/app/src/main/kotlin/com/kunminx/purenote/domain/request/NoteRequester.kt +++ b/app/src/main/kotlin/com/kunminx/purenote/domain/request/NoteRequester.kt @@ -25,29 +25,49 @@ class NoteRequester : MviDispatcherKTX() { * 消除 “mutable 样板代码 & mutable.emit 误用滥用 & repeatOnLifecycle + SharedFlow 错过时机” 等高频痛点。 */ override suspend fun onHandle(intent: NoteIntent) { - when (intent) { - is NoteIntent.InitItem -> sendResult(intent.copy()) + try { + when (intent) { + is NoteIntent.InitItem -> { + sendResult(intent.copy()) + } + + is NoteIntent.MarkItem -> { + DataRepository.updateNote(intent.param!!) + sendResult(intent.copy(isSuccess = true)) + } - is NoteIntent.MarkItem -> DataRepository.updateNote(intent.param!!) - .collect { sendResult(intent.copy(isSuccess = it)) } + is NoteIntent.UpdateItem -> { + DataRepository.updateNote(intent.param!!) + sendResult(intent.copy(isSuccess = true)) + } - is NoteIntent.UpdateItem -> DataRepository.updateNote(intent.param!!) - .collect { sendResult(intent.copy(isSuccess = it)) } + is NoteIntent.AddItem -> { + DataRepository.insertNote(intent.param!!) + sendResult(intent.copy(isSuccess = true)) + } - is NoteIntent.AddItem -> DataRepository.insertNote(intent.param!!) - .collect { sendResult(intent.copy(isSuccess = it)) } + is NoteIntent.RemoveItem -> { + DataRepository.deleteNote(intent.param!!) + sendResult(intent.copy(isSuccess = true)) + } - is NoteIntent.RemoveItem -> DataRepository.deleteNote(intent.param!!) - .collect { sendResult(intent.copy(isSuccess = it)) } + is NoteIntent.GetNoteList -> { + val notes = DataRepository.getNotes() + sendResult(intent.copy(notes)) + } - is NoteIntent.GetNoteList -> DataRepository.getNotes() - .collect { sendResult(intent.copy(it.notes)) } + is NoteIntent.ToppingItem -> { + DataRepository.updateNote(intent.param!!) + val notes = DataRepository.getNotes() + sendResult(NoteIntent.GetNoteList(notes)) + } - is NoteIntent.ToppingItem -> { - DataRepository.updateNote(intent.param!!).collect { - if (it) DataRepository.getNotes().collect { sendResult(NoteIntent.GetNoteList(it.notes)) } + is NoteIntent.Error -> { + sendResult(intent) } } + } catch (e: Exception) { + input(NoteIntent.Error(e.toString())) } } } diff --git a/app/src/main/kotlin/com/kunminx/purenote/domain/request/WeatherRequester.kt b/app/src/main/kotlin/com/kunminx/purenote/domain/request/WeatherRequester.kt index 412c01d..cbbaef9 100644 --- a/app/src/main/kotlin/com/kunminx/purenote/domain/request/WeatherRequester.kt +++ b/app/src/main/kotlin/com/kunminx/purenote/domain/request/WeatherRequester.kt @@ -2,7 +2,6 @@ package com.kunminx.purenote.domain.request import com.kunminx.architecture.domain.dispatch.MviDispatcherKTX import com.kunminx.purenote.data.repo.DataRepository -import com.kunminx.purenote.data.repo.RepoResult import com.kunminx.purenote.domain.intent.Api /** @@ -30,12 +29,12 @@ class WeatherRequester : MviDispatcherKTX() { is Api.Loading -> sendResult(intent) is Api.GetWeatherInfo -> { input(Api.Loading(true)) - DataRepository.getWeatherInfo(Api.GET_WEATHER_INFO, intent.param).collect { - if (it is RepoResult.WeatherInfo) sendResult(intent.copy(live = it.live)) - else if (it is RepoResult.Error) input(Api.Error(it.msg)) - } + val (result, msg) = DataRepository.getWeatherInfo(Api.GET_WEATHER_INFO, intent.param) + if (result != null) sendResult(intent.copy(live = result)) + else if (msg.isNotEmpty()) input(Api.Error(msg)) input(Api.Loading(false)) } + is Api.Error -> sendResult(intent) } }