728x90
commit message : Request weather api as the return value of getLocation()
commit message : Change API from openweather to WeatherAPI
commit message : The icon url was loaded as an image using Glide
경로 : app/build.gradle
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.3.9"
// 글라이드 이미지 라이브러리
def glide_version = "4.12.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"
경로 : app/src/main/java/com/example/lifehelper/PresentationLayer/Fragment/WeatherFragment.kt
@SuppressLint("MissingPermission")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
callWeatherApi()
viewModel.apply {
loadingLiveData.observe(requireActivity(), Observer {
binding?.progressBar?.visibility =
if(it) View.VISIBLE else View.GONE
})
weatherLiveData.observe(requireActivity(), Observer {
Glide.with(requireContext())
.load(it.icon) // 출력시킬 사진
.placeholder(R.mipmap.ic_launcher) // 작동되지 않을 시 기본값 설정
.into(binding!!.imageView)
binding?.textView?.text = it.country.toString()
})
}
}
private fun callWeatherApi() {
CoroutineScope(Dispatchers.Main).launch {
viewModel.callWeatherApi(viewModel.getLocation())
}
}
경로 : app/src/main/java/com/example/lifehelper/PresentationLayer/ViewModel/WeatherViewModel.kt
var loadingLiveData = MutableLiveData<Boolean>()
@SuppressLint("MissingPermission")
suspend fun getLocation(): Location? {
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"getLocation() called"
)
loadingLiveData.value = true
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
val aa = fusedLocationClient.lastLocation
.addOnSuccessListener(object : OnSuccessListener<Location> {
override fun onSuccess(it: Location?) {
if (it != null) {
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"getLocation() location : ${it.latitude}, ${it.longitude}"
)
} else {
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"Null"
)
}
}
})
return aa.await()
}
fun callWeatherApi(location: Location?) {
val latitude = location?.latitude
val longitude = location?.longitude
val q = latitude.toString() + "," + longitude.toString()
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"callWeatherApi() called"
)
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"callWeatherApi() location : ${latitude}, ${longitude}"
)
RetrofitManager.instance.searchWeather("$q") { responseState, responseBody ->
when (responseState) {
RESPONSE_STATE.OK -> {
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"API 호출 성공"
)
parsingWeatherJsonData(responseBody)
loadingLiveData.value = false // Progress Bar 값 false
}
RESPONSE_STATE.FAIL -> {
Log.d(
Constants.TAG, "${this::class.simpleName} " +
"API 호출 실패"
)
loadingLiveData.value = false // Progress Bar 값 false
}
}
}
}
경로 : app/src/main/java/com/example/lifehelper/DomainLayer/model/WeatherData.kt
data class WeatherData(
var country: String?, // 나라
var last_updated: String?, // 마지막 업데이트 정보
var icon: String?,
var text: String?, // 날씨 정보
var temp_c: Double?, // 온도 섭씨
var wind_kph: Double?, // 시간당 최대 풍속
var humidity: Double?, // 습도
)