diff --git a/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt b/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt index 698f4d8..fb9a0e5 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt @@ -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().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().apply { - this.addAll(it.filter(li)) - } - ) - it.notifyDataSetChanged() - } - } - } -} - @SuppressLint("NotifyDataSetChanged") - private fun queryWeather() { - val re = WorkersDb.getRealm().query() - 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().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() + .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().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().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().apply { +// this.addAll(it.filter(changes.list)) +// } +// ) +// it.notifyDataSetChanged() // } // } // } // } -// } -// weatherJob.start() + } + + var weatherJob : Job? = null + var result : RealmResults? = null + // lateinit var weatherJob : Job +// @SuppressLint("NotifyDataSetChanged") + private fun queryWeather() { + result = WorkersDb.getRealm().query().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 diff --git a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt index c1188d1..ecbc02b 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt @@ -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() } } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt b/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt index 3baca42..ffa8dfc 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt @@ -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().count().find()}") BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query().first().find()?.forecast?.forecastdayRealm?.size}") } }