From a7dfa35e10228c1a482dd5ce0ea8aba4297391dc Mon Sep 17 00:00:00 2001 From: JUNGGWAN KIM Date: Fri, 11 Oct 2024 17:15:29 +0900 Subject: [PATCH] weather manager created --- .../rasel/lunar/launcher/home/LauncherHome.kt | 1 - .../launcher/home/adapters/WeatherAdapter.kt | 17 ++- .../lunar/launcher/model/WeatherForcast.kt | 130 +++++++++--------- .../launcher/model/WeatherInfoManager.kt | 126 +++++++++++++++++ .../launcher/workers/OpenWeatherGetter.kt | 22 ++- .../main/res/layout/item_rec_hourly_dress.xml | 10 +- .../res/layout/recommended_hourly_dress.xml | 4 +- 7 files changed, 217 insertions(+), 93 deletions(-) create mode 100644 app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt 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 058f5ba..06948d9 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/LauncherHome.kt @@ -421,7 +421,6 @@ internal class LauncherHome : Fragment() { commandHandler.removeCallbacks(notiUpdate) commandHandler.postDelayed(notiUpdate, UPDATE_DELAY) } - else -> { // types other than UpdatedResults are not changes -- ignore them } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt index 07f73c5..9099255 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/home/adapters/WeatherAdapter.kt @@ -21,6 +21,7 @@ import rasel.lunar.launcher.model.Day import rasel.lunar.launcher.model.Hour import rasel.lunar.launcher.model.Location import rasel.lunar.launcher.model.WeatherForcast +import rasel.lunar.launcher.model.WeatherInfoManager import rasel.lunar.launcher.utils.BLog import rasel.lunar.launcher.workers.WorkersDb import java.time.Instant @@ -69,18 +70,20 @@ class WeatherAdapter( // today.time = Date(data?.time_epoch?.toLong()?.times(1000L) ?: 0L) // val dayOfItem = today.get(Calendar.DAY_OF_YEAR) data?.let { - total.loc = WorkersDb.getRealm().query().also { - BLog.LOGE("re >>> ${it.description()}") // 쿼리 로그 - }.find().first() - - holder.viewItem.setHour(it) +// total.loc = WorkersDb.getRealm().query().also { +// BLog.LOGE("re >>> ${it.description()}") // 쿼리 로그 +// }.find().first() + WeatherInfoManager.getShowingInfo(it).apply { + total.setInfo(this) + holder.viewItem.setInfo(this) + } BLog.LOGE("reeeeeeeeeee >>> ${holder.viewItem.hour.text}") holder.viewItem.amOrPm.visibility = - if (arrayListOf(12, 0).contains(data.toZonedDateTime(it.time_epoch).hour) || position == 0) { + if (arrayListOf(12, 0).contains(WeatherInfoManager.toZonedDateTime(it.time_epoch).hour) || position == 0) { View.VISIBLE } else View.INVISIBLE holder.viewItem.hour.apply { - if (data.toZonedDateTime(it.time_epoch).hour == 0) { + if (WeatherInfoManager.toZonedDateTime(it.time_epoch).hour == 0) { this@apply.setTextColor(Color.BLACK) this@apply.isSelected = true } else { 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 c7bc58a..d4eb20b 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherForcast.kt @@ -20,10 +20,10 @@ class WeatherForcast: RealmObject { var forecast: Forecast? = null var lastUpdateTime : Long = 0L - fun readyForSaving() { - lastUpdateTime = System.currentTimeMillis() - forecast?.fill() - } +// fun readyForSaving() { +// lastUpdateTime = System.currentTimeMillis() +// forecast?.fill() +// } } ////////////////////////////////// @@ -37,7 +37,7 @@ class Location: RealmObject { var localtime_epoch = 0 var localtime: String? = null - fun getTextLocation(): String = "$name / $country" +// fun getTextLocation(): String = "$name / $country" } class Current: RealmObject { @@ -77,11 +77,11 @@ class Forecast: RealmObject { var forecastday: ArrayList = arrayListOf() var forecastdayRealm: RealmList = realmListOf() - fun fill() { - if (forecastdayRealm.addAll(forecastday)) { - forecastdayRealm.forEach { f -> f.fill() } - } - } +// fun fill() { +// if (forecastdayRealm.addAll(forecastday)) { +// forecastdayRealm.forEach { f -> f.fill() } +// } +// } } ////////////////////////////////// @@ -102,9 +102,9 @@ class Forecastday: RealmObject { var hour: ArrayList = arrayListOf() var hourRealm: RealmList = realmListOf() - fun fill() { - hourRealm.addAll(hour) - } +// fun fill() { +// hourRealm.addAll(hour) +// } } ////////////////////////////////// @@ -183,56 +183,56 @@ class Hour: RealmObject { var gust_kph = 0.0 var uv = 0.0 - fun toZonedDateTime(timeEpoch: Int) = Instant.ofEpochSecond(timeEpoch.toLong()) - .atZone(ZoneId.systemDefault()) - - fun getAmOrPm(): String = toZonedDateTime(time_epoch).hour.run { - if (this < 12) "오전" else "오후" - } - - fun getTextHour(): String = toZonedDateTime(time_epoch) - .run { - if (this.hour == 0) { - when (this.dayOfYear - Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) { - 1 -> "내일" - 2 -> "모레" - 3 -> "글피" - else -> "${this.dayOfMonth}일" - } - } else { - "${this.hour}시" - } - } - - fun getDress(): String = - if (temp_c >= 23) { - "반팔" - } else if ((23 > temp_c) && (temp_c >= 20)) { - "긴팔" - } else if ((20 > temp_c) && (temp_c >= 17)) { - "니트" - } else if ((17 > temp_c) && (temp_c >= 12)) { - "얇은겉옷" - } else if ((12 > temp_c) && (temp_c >= 6)) { - "두꺼운겉옷" - } else { - "패딩" - } - - fun getImgDress() = - if (temp_c >= 23) { - R.drawable.dress_short_sleeves - } else if ((23 > temp_c) && (temp_c >= 20)) { - "긴팔" - } else if ((20 > temp_c) && (temp_c >= 17)) { - "니트" - } else if ((17 > temp_c) && (temp_c >= 12)) { - "얇은겉옷" - } else if ((12 > temp_c) && (temp_c >= 6)) { - "두꺼운겉옷" - } else { - "패딩" - } - - fun getTemp(): String = "${temp_c}도" +// fun toZonedDateTime(timeEpoch: Int) = Instant.ofEpochSecond(timeEpoch.toLong()) +// .atZone(ZoneId.systemDefault()) +// +// fun getAmOrPm(): String = toZonedDateTime(time_epoch).hour.run { +// if (this < 12) "오전" else "오후" +// } +// +// fun getTextHour(): String = toZonedDateTime(time_epoch) +// .run { +// if (this.hour == 0) { +// when (this.dayOfYear - Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) { +// 1 -> "내일" +// 2 -> "모레" +// 3 -> "글피" +// else -> "${this.dayOfMonth}일" +// } +// } else { +// "${this.hour}시" +// } +// } +// +// fun getDress(): String = +// if (temp_c >= 23) { +// "반팔" +// } else if ((23 > temp_c) && (temp_c >= 20)) { +// "긴팔" +// } else if ((20 > temp_c) && (temp_c >= 17)) { +// "니트" +// } else if ((17 > temp_c) && (temp_c >= 12)) { +// "얇은겉옷" +// } else if ((12 > temp_c) && (temp_c >= 6)) { +// "두꺼운겉옷" +// } else { +// "패딩" +// } +// +// fun getImgDress() = +// if (temp_c >= 23) { +// R.drawable.dress_short_sleeves +// } else if ((23 > temp_c) && (temp_c >= 20)) { +// "긴팔" +// } else if ((20 > temp_c) && (temp_c >= 17)) { +// "니트" +// } else if ((17 > temp_c) && (temp_c >= 12)) { +// "얇은겉옷" +// } else if ((12 > temp_c) && (temp_c >= 6)) { +// "두꺼운겉옷" +// } else { +// "패딩" +// } +// +// fun getTemp(): String = "${temp_c}도" } \ No newline at end of file diff --git a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt new file mode 100644 index 0000000..9741fa2 --- /dev/null +++ b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfoManager.kt @@ -0,0 +1,126 @@ +package rasel.lunar.launcher.model + +import io.realm.kotlin.ext.query +import io.realm.kotlin.types.RealmList +import rasel.lunar.launcher.R +import rasel.lunar.launcher.utils.BLog +import rasel.lunar.launcher.workers.OpenWeatherGetter +import rasel.lunar.launcher.workers.WorkersDb +import java.time.Instant +import java.time.ZoneId +import java.time.ZonedDateTime +import java.util.Calendar + +class ShowingWeatherInfo() { + var textLocation: String? = null + var amOrPm: String? = null + var textHour: String? = null + var dress: String? = null + var imgDress: Int? = null + var temp: String? = null + + fun setLocation(loc: Location?): ShowingWeatherInfo { + textLocation = "${loc?.name ?: "도시"} / ${loc?.country ?: "나라"}" + return this + } + fun setHour(hour: Hour): ShowingWeatherInfo { + setAmOrPm(hour) + setTextHour(hour) + setDress(hour) + setTemp(hour) + return this + } + private fun setAmOrPm(hour: Hour) = WeatherInfoManager.toZonedDateTime(hour.time_epoch).hour.run { + amOrPm = if (this < 12) "오전" else "오후" + } + private fun setTextHour(hour: Hour) { + textHour = WeatherInfoManager.toZonedDateTime(hour.time_epoch).run { + if (this.hour == 0) { + when (this.dayOfYear - Calendar.getInstance().get(Calendar.DAY_OF_YEAR)) { + 1 -> "내일" + 2 -> "모레" + 3 -> "글피" + else -> "${this.dayOfMonth}일" + } + } else { + "${this.hour}시" + } + } + } + private fun setDress(hour: Hour) { + if (hour.temp_c >= 23) { + dress = "반팔" + imgDress = R.drawable.dress_short_sleeves + } else if ((23 > hour.temp_c) && (hour.temp_c >= 20)) { + dress = "긴팔" + imgDress = R.drawable.dress_long_sleeves + } else if ((20 > hour.temp_c) && (hour.temp_c >= 17)) { + dress = "니트" + imgDress = R.drawable.dress_knitwear + } else if ((17 > hour.temp_c) && (hour.temp_c >= 12)) { + dress = "얇은겉옷" + imgDress = R.drawable.dress_flimsy_outer + } else if ((12 > hour.temp_c) && (hour.temp_c >= 6)) { + dress = "두꺼운겉옷" + imgDress = R.drawable.dress_heavy_outer + } else { + dress = "패딩" + imgDress = R.drawable.dress_padded_coat + } + } + private fun setTemp(hour: Hour) {temp = "${hour.temp_c}도"} +} + +object WeatherInfoManager { + var info: WeatherForcast? = null +// var showingInfo: ShowingWeatherInfo? = null + + +// fun setInfo(data: WeatherForcast) { +// this.info = data +// BLog.LOGE("Now WeatherForcast updated. info : ${this.info}") +// } + + fun readyForSaving(lat: Double, lon: Double) { + this.info?.lastUpdateTime = System.currentTimeMillis() + this.fill() + setLatitudeAndLongitude(lat, lon) + } + + private fun setLatitudeAndLongitude(lat: Double, lon: Double) { + info?.forecast?.forecastdayRealm?.forEach { + it.hourRealm.forEach {h -> + h.lat = lat + h.lon = lon + } + } + } + + private fun fill() { + getForecast()?.let { + if (it.forecastdayRealm.addAll(it.forecastday)) { + it.forecastdayRealm.forEach { f -> f.hourRealm.addAll(f.hour) } + } + } + } + + fun toZonedDateTime(timeEpoch: Int): ZonedDateTime = Instant.ofEpochSecond(timeEpoch.toLong()) + .atZone(ZoneId.systemDefault()) + + fun getShowingInfo(hour: Hour): ShowingWeatherInfo = ShowingWeatherInfo().setLocation(getLocation()).setHour(hour) + + + private fun getLocation(): Location? { + return if (info?.location == null) { + WorkersDb.getRealm().query().also { + BLog.LOGE("re >>> ${it.description()}") // 쿼리 로그 + }.find().first() + } else { + info?.location + } + } + + fun getForecast(): Forecast? = info?.forecast + fun getAllForecastDay(): RealmList? = getForecast()?.forecastdayRealm + fun getAllHour(day: Forecastday): RealmList = day.hourRealm +} \ No newline at end of file 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 ffa8dfc..2ea250c 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt @@ -12,6 +12,7 @@ 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.model.WeatherInfoManager import rasel.lunar.launcher.utils.BLog import retrofit2.Call import retrofit2.Retrofit @@ -64,24 +65,19 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base q = "$latitude,$longitude", days = (System.currentTimeMillis() % 5L).toInt().toString() )?.execute()?.let { response -> - BLog.LOGE("into getWeather afterc excute") + BLog.LOGE("into getWeather after execute") BLog.LOGE("weatherApi forecast response >>> $response") response.body()?.let { weatherInfo -> - BLog.LOGE("into getWeather on body ") - weatherInfo.readyForSaving() - weatherInfo.forecast?.forecastdayRealm?.forEach { - it.hourRealm.forEach {h -> - h.lat = lat ?: 0.0 - h.lon = lon ?: 0.0 - } - } + WeatherInfoManager.info = weatherInfo + WeatherInfoManager.readyForSaving(lat ?: 0.0, lon ?: 0.0) // Realm에 저장 WorkersDb.getRealm().writeBlocking { - var result = copyToRealm(weatherInfo, UpdatePolicy.ALL) - BLog.LOGE("saved weatherForcast >>> ${result}") + copyToRealm(weatherInfo, UpdatePolicy.ALL).also { + BLog.LOGE("saved weatherForcast >>> $it") + } } - BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query().count().find()}") - BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query().first().find()?.forecast?.forecastdayRealm?.size}") +// BLog.LOGE("saved weatherForcast forecastdayRealm.size >>> ${WorkersDb.getRealm().query().first().find()?.forecast?.forecastdayRealm?.size}") +// BLog.LOGE("saved weatherForcast hour.count >>> ${WorkersDb.getRealm().query().count().find()}") } } } diff --git a/app/src/main/res/layout/item_rec_hourly_dress.xml b/app/src/main/res/layout/item_rec_hourly_dress.xml index e60bd27..9b897be 100644 --- a/app/src/main/res/layout/item_rec_hourly_dress.xml +++ b/app/src/main/res/layout/item_rec_hourly_dress.xml @@ -1,7 +1,7 @@ - + - +