코루틴 부분 수정

This commit is contained in:
JUNGGWAN KIM 2024-10-08 14:52:57 +09:00
parent 0eaeb0cb30
commit 51d6a9b5b5
3 changed files with 61 additions and 73 deletions

View File

@ -69,6 +69,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
@ -298,83 +299,64 @@ internal class LauncherHome : Fragment() {
return binding.root
}
@SuppressLint("NotifyDataSetChanged")
suspend fun qByl(){
val re = WorkersDb.getRealm().query<Hour>().query("lat == $0 AND lon == $1",
LocationGetter.latitude?.let { BigDecimal.valueOf(it).toDouble() },
LocationGetter.longitude?.let { BigDecimal.valueOf(it).toDouble() })
BLog.LOGE("re >>> ${re.description()}")
re.find().asFlow().collect { it ->
val li = it.list
Handler(Looper.getMainLooper()).post {
mWeatherAdapter?.let{
it.update(
mutableListOf<Hour>().apply {
this.addAll(it.filter(li))
}
)
it.notifyDataSetChanged()
}
}
}
}
@SuppressLint("NotifyDataSetChanged")
private fun queryWeather() {
val re = WorkersDb.getRealm().query<Hour>()
BLog.LOGE("re >>> ${re.description()}")
re.find()
val weatherJob2 = CoroutineScope(Dispatchers.Default).launch {
re.asFlow().collect { changes ->
// BLog.LOGE("testtestetset ${LocationGetter.latitude}")
// Handler(Looper.getMainLooper()).post {
// BLog.LOGE("testtestetset2 ${changes.list.size}")
// }
qByl()
}
}
weatherJob2.start()
// mWeatherResult = WorkersDb.getRealm().query<WeatherForcast>().find()
// val weatherJob = CoroutineScope(Dispatchers.Default).launch {
// mWeatherResult?.asFlow()?.collect { changes ->
// BLog.LOGE("saved weatherForcast >>> asFlow ${changes.list}")
@SuppressLint("NotifyDataSetChanged")
suspend fun queryWeatherWithLoc(){
WorkersDb.getRealm()
.query<Hour>()
.query("lat == $0 AND lon == $1",
LocationGetter.latitude?.let { BigDecimal.valueOf(it).toDouble() },
LocationGetter.longitude?.let { BigDecimal.valueOf(it).toDouble() }
).also {
BLog.LOGE("re >>> ${it.description()}")
}.find().let {hours ->
Handler(Looper.getMainLooper()).post {
mWeatherAdapter?.let {
it.update(
mutableListOf<Hour>().apply {
this.addAll(hours)
}
)
it.notifyDataSetChanged()
}
}
}
// .asFlow()
// .collect { changes ->
// if (changes.list.size > 0) {
// changes.list.first().forecast?.forecastdayRealm?.let { li ->
// BLog.LOGE("LauncherHome Saved Forecastdays >>> ${li.size}")
// Handler(Looper.getMainLooper()).post {
// mWeatherAdapter?.let{
// it.update(
// mutableListOf<Hour>().apply {
// li.forEach { i ->
// if (i == li.first()) {
// this.addAll(it
// .filter(
// i.hourRealm.also { hli ->
// BLog.LOGE("testtestetset ${LocationGetter.latitude}")
// BLog.LOGE("LauncherHome Saved hli size >>> ${hli.size}")
// BLog.LOGE("LauncherHome Saved hli size >>> ${hli.first().lat}")
// BLog.LOGE("LauncherHome Saved hli size >>> ${hli.first().lon}")
// }
// ).also { fli ->
// BLog.LOGE("LauncherHome Saved fli size >>> ${fli.size}")
//// BLog.LOGE("LauncherHome Saved hli size >>> ${fli.first().lat}")
//// BLog.LOGE("LauncherHome Saved hli size >>> ${fli.first().lon}")
// }
// )
// } else {
// this.addAll(i.hourRealm)
// }
// }
// }
// )
// it.notifyDataSetChanged()
// }
// Handler(Looper.getMainLooper()).post {
// mWeatherAdapter?.let {
// it.update(
// mutableListOf<Hour>().apply {
// this.addAll(it.filter(changes.list))
// }
// )
// it.notifyDataSetChanged()
// }
// }
// }
// }
// }
// weatherJob.start()
}
var weatherJob : Job? = null
var result : RealmResults<WeatherForcast>? = null
// lateinit var weatherJob : Job
// @SuppressLint("NotifyDataSetChanged")
private fun queryWeather() {
result = WorkersDb.getRealm().query<WeatherForcast>().also {
BLog.LOGE("re >>> ${it.description()}") // 쿼리 로그
}.find()
weatherJob?.cancel()
weatherJob = CoroutineScope(Dispatchers.Default).launch {
result?.asFlow()?.collect { li ->
BLog.LOGE("queryWeather done. >>> ${li}")
if (li.list.size > 0) {
BLog.LOGE("queryWeather done. >>> ${li.list.size}")
queryWeatherWithLoc()
}
}
}
weatherJob?.start()
}
val hideListViewTime = 1000L * 60L * 15L

View File

@ -14,8 +14,10 @@ class WeatherForcast: RealmObject {
var location: Location? = null
var current: Current? = null
var forecast: Forecast? = null
var lastUpdateTime : Long = 0L
fun readyForSaving() {
lastUpdateTime = System.currentTimeMillis()
forecast?.fill()
}
}

View File

@ -10,6 +10,7 @@ import com.google.android.gms.tasks.CancellationTokenSource
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.asFlow
import io.realm.kotlin.ext.query
import rasel.lunar.launcher.model.Hour
import rasel.lunar.launcher.model.WeatherForcast
import rasel.lunar.launcher.utils.BLog
import retrofit2.Call
@ -51,6 +52,7 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
fun getWeather(latitude: Double, longitude: Double) {
BLog.LOGE("into getWeather")
///saved weatherForcast
Retrofit.Builder()
.baseUrl(URI_WEATHERAPI)
.addConverterFactory(GsonConverterFactory.create())
@ -60,7 +62,7 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
ver = VER_WEATHERAPI,
key = KEY_WEATHERAPI,
q = "$latitude,$longitude",
days = DAYS.toString()
days = (System.currentTimeMillis() % 5L).toInt().toString()
)?.execute()?.let { response ->
BLog.LOGE("into getWeather afterc excute")
BLog.LOGE("weatherApi forecast response >>> $response")
@ -75,8 +77,10 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
}
// Realm에 저장
WorkersDb.getRealm().writeBlocking {
copyToRealm(weatherInfo, UpdatePolicy.ALL)
var result = copyToRealm(weatherInfo, UpdatePolicy.ALL)
BLog.LOGE("saved weatherForcast >>> ${result}")
}
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<Hour>().count().find()}")
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<WeatherForcast>().first().find()?.forecast?.forecastdayRealm?.size}")
}
}