Skip to content

Commit

Permalink
Major update
Browse files Browse the repository at this point in the history
New
- Fully function weather icon

Fix
- Fixed ImageView for status in fragment_geo_search.xml

Change
- Code clean-up
  • Loading branch information
Reynaldev committed Sep 4, 2023
1 parent fae5454 commit 4243bb6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 28 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dependencies {
// Moshi
implementation 'com.squareup.moshi:moshi-kotlin:1.13.0'

// Coil
implementation "io.coil-kt:coil:1.1.1"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/reyndev/simpliweather/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.reyndev.simpliweather
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.net.toUri
import androidx.databinding.BindingAdapter
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.reyndev.simpliweather.adapter.GeoAdapter
import com.reyndev.simpliweather.data.GeoModel
import com.reyndev.simpliweather.data.WeatherApiStatus
Expand Down Expand Up @@ -36,4 +38,17 @@ fun apiStatus(imageView: ImageView, status: WeatherApiStatus) {
imageView.setImageResource(R.drawable.ic_error)
}
}
}

@BindingAdapter("imageUrl")
fun imageUrl(imageView: ImageView, icon: String?) {
icon?.let {
val imgUrl = "https://openweathermap.org/img/wn/${icon}@2x.png"
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build()

imageView.load(imgUri) {
placeholder(R.drawable.loading_animation)
error(R.drawable.default_weather)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class GeoAdapter(private val clickListener: GeoListener)
: RecyclerView.ViewHolder(binding.root) {

fun bind(clickListener: GeoListener, geo: GeoModel) {
if (!geo.state.isNullOrBlank())
binding.geoName.text = "${geo.name}, ${geo.state}"
else
binding.geoName.text = geo.name

binding.geo = geo
binding.clickListener = clickListener
binding.executePendingBindings()
Expand All @@ -41,7 +46,7 @@ class GeoAdapter(private val clickListener: GeoListener)
}

override fun areContentsTheSame(oldItem: GeoModel, newItem: GeoModel): Boolean {
return (oldItem.lat == newItem.lat) && (oldItem.lon == newItem.lon)
return oldItem.state == newItem.state
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.squareup.moshi.Json
data class GeoModel(
val name: String,
val lat: String,
val lon: String
val lon: String,
val state: String?
)

data class WeatherModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class WeatherViewModel : ViewModel() {
val newGeo = GeoModel(
"Special Capital Region of Jakarta",
(-6.1753942).toString(),
(106.827183).toString()
(106.827183).toString(),
null
)

_location.value = newGeo
Expand All @@ -43,7 +44,7 @@ class WeatherViewModel : ViewModel() {
_status.value = WeatherApiStatus.LOADING

try {
_geo.value = WeatherApiService.geoService.getGeo(
_geo.value = WeatherApiService.weatherService.getGeo(
name,
5,
appId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,29 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

private const val GEO_URL = "https://api.openweathermap.org/geo/1.0/"
private const val WEATHER_URL = "https://api.openweathermap.org/data/3.0/"
private const val BASE_URL = "https://api.openweathermap.org/"

val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()

val retroGeo = Retrofit.Builder()
val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(GEO_URL)
.baseUrl(BASE_URL)
.build()

val retroWeather = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(WEATHER_URL)
.build()

interface GeoApi {
@GET("direct")
interface WeatherApi {
@GET("geo/1.0/direct")
suspend fun getGeo(
@Query("q") name: String,
@Query("limit") limit: Int,
@Query("appid") id: String
): List<GeoModel>
}

interface WeatherApi {
@GET("onecall")
@GET("data/3.0/onecall")
suspend fun getWeather(
@Query("lat") lat: Double,
@Query("lon") lon: Double,
Expand All @@ -47,11 +40,7 @@ interface WeatherApi {
}

object WeatherApiService {
val geoService: GeoApi by lazy {
retroGeo.create(GeoApi::class.java)
}

val weatherService: WeatherApi by lazy {
retroWeather.create(WeatherApi::class.java)
retrofit.create(WeatherApi::class.java)
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_geo_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@

<ImageView
android:id="@+id/status_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="128dp"
android:layout_height="128dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/borderline"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
android:id="@+id/weather_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginTop="8dp"
android:src="@drawable/default_weather"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/city_search" />
app:layout_constraintTop_toBottomOf="@id/city_search"
app:imageUrl="@{viewModel.weather.current.weather[0].icon}"/>

<LinearLayout
android:id="@+id/temp_label"
Expand Down Expand Up @@ -93,7 +95,7 @@
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/geo_item_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
android:id="@+id/geo_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{geo.name}"
tools:text="Jakarta" />

</LinearLayout>
Expand Down

0 comments on commit 4243bb6

Please sign in to comment.