Compare commits
2 Commits
e872fe4e8a
...
3561137c6b
| Author | SHA1 | Date | |
|---|---|---|---|
| 3561137c6b | |||
| 6e34a6cad6 |
@ -113,6 +113,7 @@ import rasel.lunar.launcher.workers.ContactInfoGetter
|
||||
import rasel.lunar.launcher.workers.DCGetter
|
||||
import rasel.lunar.launcher.workers.DotaxGetter
|
||||
import rasel.lunar.launcher.workers.FmKoreaGetter
|
||||
import rasel.lunar.launcher.workers.LocationGetter
|
||||
import rasel.lunar.launcher.workers.NewsFeedsGetter
|
||||
import rasel.lunar.launcher.workers.OpenWeatherGetter
|
||||
import rasel.lunar.launcher.workers.RecentCallGetter
|
||||
@ -296,6 +297,17 @@ internal class LauncherActivity : AppCompatActivity() {
|
||||
.build())
|
||||
|
||||
}, delay, TimeUnit.SECONDS)
|
||||
|
||||
val weatherDelay = 3L
|
||||
Executors.newSingleThreadScheduledExecutor().schedule({
|
||||
//de574a260b1f474d99955729241909
|
||||
mWorkManager?.cancelAllWorkByTag(LocationGetter.TAG)
|
||||
mWorkManager?.enqueueUniquePeriodicWork(
|
||||
LocationGetter.TAG, ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
|
||||
PeriodicWorkRequestBuilder<OpenWeatherGetter>(midTimePeriod, TimeUnit.MINUTES)
|
||||
.addTag(LocationGetter.TAG)
|
||||
.build())
|
||||
}, weatherDelay, TimeUnit.SECONDS)
|
||||
Executors.newSingleThreadScheduledExecutor().schedule({
|
||||
//de574a260b1f474d99955729241909
|
||||
mWorkManager?.cancelAllWorkByTag(OpenWeatherGetter.TAG)
|
||||
@ -304,8 +316,7 @@ internal class LauncherActivity : AppCompatActivity() {
|
||||
PeriodicWorkRequestBuilder<OpenWeatherGetter>(midTimePeriod, TimeUnit.MINUTES)
|
||||
.addTag(OpenWeatherGetter.TAG)
|
||||
.build())
|
||||
}, 3, TimeUnit.SECONDS)
|
||||
|
||||
}, weatherDelay + 3, TimeUnit.SECONDS)
|
||||
}
|
||||
|
||||
fun workmanager() : WorkManager? {
|
||||
|
||||
@ -0,0 +1,65 @@
|
||||
package rasel.lunar.launcher.home.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import rasel.lunar.launcher.R
|
||||
import rasel.lunar.launcher.home.adapters.WeatherAdapter.ViewHolder
|
||||
import rasel.lunar.launcher.model.Hour
|
||||
import rasel.lunar.launcher.model.WeatherForcast
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
|
||||
class WeatherAdapter(private val dataSet: Array<String>): RecyclerView.Adapter<ViewHolder>(){
|
||||
var isShowAmOrPm: Boolean = true
|
||||
|
||||
class ViewHolder(view: View): RecyclerView.ViewHolder(view) {
|
||||
val viewItem: View
|
||||
|
||||
init {
|
||||
viewItem = view.findViewById(R.id.weather_item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val view = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.recommended_hourly_dress, parent, false)
|
||||
return ViewHolder(view)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val data = dataSet[position] as? Hour
|
||||
data?.let {
|
||||
if (it.time_epoch >= (System.currentTimeMillis() / 1000)) {
|
||||
holder.viewItem.findViewById<TextView>(R.id.amOrPm)
|
||||
.run {
|
||||
this.text = Instant.ofEpochSecond(it.time_epoch.toLong())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.hour
|
||||
.run {
|
||||
if (this < 12) "오전" else "오후"
|
||||
}
|
||||
}
|
||||
holder.viewItem.findViewById<TextView>(R.id.hour)
|
||||
.run {
|
||||
val hour = Instant.ofEpochSecond(it.time_epoch.toLong())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.hour
|
||||
.toString()
|
||||
this.text = "${hour}시"
|
||||
}
|
||||
holder.viewItem.findViewById<ImageView>(R.id.imgDress)
|
||||
.run { this.setImageResource(R.drawable.ico_time) }
|
||||
holder.viewItem.findViewById<TextView>(R.id.temperature)
|
||||
.run {
|
||||
this.text = "${it.temp_c}도"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = 3
|
||||
}
|
||||
@ -1,109 +0,0 @@
|
||||
package rasel.lunar.launcher.model
|
||||
|
||||
import android.app.appsearch.StorageInfo
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import io.realm.kotlin.ext.realmListOf
|
||||
import io.realm.kotlin.types.RealmList
|
||||
import io.realm.kotlin.types.RealmObject
|
||||
import io.realm.kotlin.types.annotations.Ignore
|
||||
|
||||
//class WeatherInfo : RealmObject {
|
||||
// constructor()
|
||||
//// constructor(coord: Loc?, weather: Array<Weather>?, main: Main?) {
|
||||
//// this.coord = coord
|
||||
//// weather?.let {
|
||||
//// this.weather?.addAll(it)
|
||||
//// }
|
||||
////
|
||||
//// this.main = main
|
||||
//// }
|
||||
//
|
||||
// @SerializedName("coord")
|
||||
// var coord: Loc? = null
|
||||
//
|
||||
// @Ignore
|
||||
// @SerializedName("weather")
|
||||
// var weather: ArrayList<Weather>? = arrayListOf()
|
||||
//
|
||||
// var weathers: RealmList<Weather> = realmListOf()
|
||||
//
|
||||
// @SerializedName("base")
|
||||
// var base: String? = null
|
||||
//
|
||||
// @SerializedName("main")
|
||||
// var main: Main? = null
|
||||
//fun filled() {
|
||||
// weather?.let {
|
||||
// weathers.addAll(it)
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
// @SerializedName("visibility")
|
||||
// var visibility: String? = null
|
||||
//
|
||||
// @SerializedName("wind")
|
||||
// var wind: Wind? = null
|
||||
//
|
||||
// @SerializedName("clouds")
|
||||
// var clouds: Clouds? = null
|
||||
//
|
||||
// @SerializedName("dt")
|
||||
// var dt: String? = null
|
||||
//
|
||||
// @SerializedName("sys")
|
||||
// var sys: Sys? = null
|
||||
//
|
||||
// @SerializedName("timezone")
|
||||
// var timezone: String? = null
|
||||
//
|
||||
// @SerializedName("id")
|
||||
// var id: String? = null
|
||||
//
|
||||
// @SerializedName("name")
|
||||
// var name: String? = null
|
||||
//
|
||||
// @SerializedName("cod")
|
||||
// var cod: String? = null
|
||||
// }
|
||||
|
||||
class Loc : RealmObject {
|
||||
var lon: String? = null
|
||||
var lat: String? = null
|
||||
}
|
||||
|
||||
class Weather : RealmObject {
|
||||
var id: String? = null
|
||||
var main: String? = null
|
||||
var description: String? = null
|
||||
var icon: String? = null
|
||||
}
|
||||
|
||||
class Main : RealmObject {
|
||||
var temp: String? = null
|
||||
var feels_like: String? = null
|
||||
var temp_min: String? = null
|
||||
var temp_max: String? = null
|
||||
var pressure: String? = null
|
||||
var humidity: String? = null
|
||||
var sea_level: String? = null
|
||||
var grnd_level: String? = null
|
||||
}
|
||||
|
||||
class Wind: RealmObject {
|
||||
var speed: String? = null
|
||||
var deg: String? = null
|
||||
var gust: String? = null
|
||||
}
|
||||
|
||||
class Clouds: RealmObject {
|
||||
var all: String? = null
|
||||
}
|
||||
|
||||
class Sys: RealmObject {
|
||||
var type: String? = null
|
||||
var id: String? = null
|
||||
var country: String? = null
|
||||
var sunrise: String? = null
|
||||
var sunset: String? = null
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package rasel.lunar.launcher.workers
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.location.Location
|
||||
import androidx.work.WorkerParameters
|
||||
import com.google.android.gms.location.LocationServices
|
||||
import com.google.android.gms.location.Priority
|
||||
import com.google.android.gms.tasks.CancellationTokenSource
|
||||
import rasel.lunar.launcher.utils.BLog
|
||||
|
||||
class LocationGetter(context: Context, workerParams: WorkerParameters) : BaseGetter(context, workerParams) {
|
||||
companion object {
|
||||
val TAG = "LocationGetter"
|
||||
var longitude: Double? = null
|
||||
var latitude: Double? = null
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
override fun realWork(): Result {
|
||||
BLog.LOGE("${OpenWeatherGetter.TAG} realWork()")
|
||||
|
||||
LocationServices.getFusedLocationProviderClient(this.applicationContext)
|
||||
.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, CancellationTokenSource().token)
|
||||
.addOnSuccessListener{ success: Location? ->
|
||||
success?.let {
|
||||
BLog.LOGE("Location >>> $it")
|
||||
BLog.LOGE("Location >>> (latitude)${it.longitude}/(longitude)${it.latitude}")
|
||||
longitude = it.longitude
|
||||
latitude = it.latitude
|
||||
}
|
||||
}.addOnFailureListener{
|
||||
BLog.LOGE("Location error >>> $it")
|
||||
}
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
}
|
||||
@ -24,9 +24,6 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
|
||||
val TAG = "OpenWeatherGetter"
|
||||
}
|
||||
//////////////////////////////////////////
|
||||
// openWeatherMap
|
||||
val URI_OPEN_WEATHER_MAP = "https://api.openweathermap.org"
|
||||
val KEY_OPEN_WEATHER_MAP = "87cd0810b7e4b4debd31a6ef98b98154"
|
||||
// weatherapi
|
||||
val VER_WEATHERAPI = "v1"
|
||||
val URI_WEATHERAPI = "https://api.weatherapi.com"
|
||||
@ -36,73 +33,46 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
|
||||
var lon: Double? = null // 경도
|
||||
var lat: Double? = null // 위도
|
||||
//////////////////////////////////////////
|
||||
@SuppressLint("MissingPermission")
|
||||
|
||||
override fun realWork(): Result {
|
||||
BLog.LOGE("${TAG} realWork() ")
|
||||
// setLocation()
|
||||
|
||||
fun location(loc: Location?) {
|
||||
val service = Retrofit.Builder()
|
||||
.baseUrl(URI_WEATHERAPI)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.run { this.create<RestrofitService>() }
|
||||
service.getForecast(
|
||||
ver = VER_WEATHERAPI,
|
||||
key = KEY_WEATHERAPI,
|
||||
q = "${loc?.latitude ?: lat},${loc?.longitude ?: lon}",
|
||||
days = DAYS.toString()
|
||||
)?.execute()?.let { response ->
|
||||
response.body()?.let { weatherInfo ->
|
||||
BLog.LOGE("Location error >>> $weatherInfo")
|
||||
WorkersDb.getRealm().writeBlocking {
|
||||
weatherInfo.readyForSaving()
|
||||
copyToRealm(weatherInfo, UpdatePolicy.ALL)
|
||||
}
|
||||
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<WeatherForcast>().first().find()}")
|
||||
}
|
||||
}
|
||||
BLog.LOGE("${TAG} realWork()")
|
||||
// 위치 값 가져오기
|
||||
lat = LocationGetter.latitude
|
||||
lon = LocationGetter.longitude
|
||||
if (lat != null && lon != null) {
|
||||
getWeather(lat!!, lon!!)
|
||||
} else {
|
||||
BLog.LOGE("lat or lon is null")
|
||||
}
|
||||
|
||||
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this.applicationContext)
|
||||
fusedLocationProviderClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY,CancellationTokenSource().token)
|
||||
.addOnSuccessListener{ success: Location? ->
|
||||
success?.let {
|
||||
BLog.LOGE("Location >>> $it")
|
||||
BLog.LOGE("Location >>> (latitude)${it.longitude}/(longitude)${it.latitude}")
|
||||
lon = it.longitude
|
||||
lat = it.latitude
|
||||
|
||||
//{https://home.openweathermap.org/api 에서 정보를 조회 하자}
|
||||
location(null)
|
||||
}
|
||||
}.addOnFailureListener{
|
||||
BLog.LOGE("Location error >>> $it")
|
||||
}
|
||||
// }
|
||||
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
fun setLocation() {
|
||||
|
||||
}
|
||||
|
||||
fun getWeather(loc: Location?) {
|
||||
|
||||
fun getWeather(latitude: Double, longitude: Double) {
|
||||
Retrofit.Builder()
|
||||
.baseUrl(URI_WEATHERAPI)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create<RestrofitService>()
|
||||
.getForecast( // weatherApi
|
||||
ver = VER_WEATHERAPI,
|
||||
key = KEY_WEATHERAPI,
|
||||
q = "$latitude,$longitude",
|
||||
days = DAYS.toString()
|
||||
)?.execute()?.let { response ->
|
||||
BLog.LOGE("weatherApi forecast response >>> $response")
|
||||
response.body()?.let { weatherInfo ->
|
||||
// Realm에 저장
|
||||
WorkersDb.getRealm().writeBlocking {
|
||||
weatherInfo.readyForSaving()
|
||||
copyToRealm(weatherInfo, UpdatePolicy.ALL)
|
||||
}
|
||||
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<WeatherForcast>().first().find()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface RestrofitService {
|
||||
// open_weather_map
|
||||
// @GET("/data/2.5/weather")
|
||||
// fun getPosts(
|
||||
// @Query("lat") lat: Double,
|
||||
// @Query("lon") lon: Double,
|
||||
// @Query("appid") key: String
|
||||
// ): Call<WeatherInfo?>?
|
||||
|
||||
// weather_api
|
||||
@GET("/{ver}/forecast.json")
|
||||
fun getForecast(
|
||||
|
||||
@ -4,12 +4,14 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:orientation="vertical"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center">
|
||||
android:gravity="center"
|
||||
android:id="@+id/weather_item">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="오후"
|
||||
android:id="@+id/amOrPm"
|
||||
android:gravity="center"
|
||||
android:fontFamily="sans-serif-medium"
|
||||
android:textSize="14sp" />
|
||||
@ -17,12 +19,13 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="16시"
|
||||
android:id="@+id/hour"
|
||||
android:fontFamily="sans-serif-medium"
|
||||
android:gravity="center"
|
||||
android:textSize="14sp"
|
||||
android:textAlignment="center"/>
|
||||
<ImageView
|
||||
android:id="@+id/imageView2"
|
||||
android:id="@+id/imgDress"
|
||||
android:layout_width="40dp"
|
||||
android:layout_height="40dp"
|
||||
android:contentDescription="시계 아이콘"
|
||||
@ -31,6 +34,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="27도"
|
||||
android:id="@+id/temperature"
|
||||
android:fontFamily="sans-serif-medium"
|
||||
android:textSize="14sp"
|
||||
android:gravity="center"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user