Skip to content

Commit

Permalink
update data flow
Browse files Browse the repository at this point in the history
  • Loading branch information
KunMinX committed Jul 15, 2024
1 parent 9d7702a commit 611832a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Weather.Live?, String> =
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<Note>) : RepoResult()
data class WeatherInfo(val live: Weather.Live) : RepoResult()
data class Error(val msg: String) : RepoResult()
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ sealed class NoteIntent {
data class InitItem(
val param: Note? = null
) : NoteIntent()

data class Error(
val msg: String? = null
) : NoteIntent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,49 @@ class NoteRequester : MviDispatcherKTX<NoteIntent>() {
* 消除 “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()))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -30,12 +29,12 @@ class WeatherRequester : MviDispatcherKTX<Api>() {
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)
}
}
Expand Down

0 comments on commit 611832a

Please sign in to comment.