Skip to content

Commit

Permalink
#23 use androidx.databinding.Observable
Browse files Browse the repository at this point in the history
  • Loading branch information
raychenon committed Nov 24, 2020
1 parent 4c535f5 commit 0f13518
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.betterapps.graysky.data.binding

import io.betterapps.graysky.data.coroutines.Status
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt

data class ObservableWeather(
var progressStatus: Status,
var errorMsg: String?
val progressStatus: ObservableInt,
var errorMsg: ObservableField<String?>
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -64,7 +66,7 @@ class WeatherForecastFragment : Fragment() {
// lazy inject
val weatherViewModel: WeatherViewModel by viewModel()

private val observableWeather = ObservableWeather(Status.LOADING,null)
private val observableWeather = ObservableWeather(ObservableInt(View.VISIBLE), ObservableField(""))

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -79,12 +81,13 @@ class WeatherForecastFragment : Fragment() {
distanceFromUserLocation = arguments?.getDouble(ARG_DISTANCE)!!
position = arguments!!.getInt(ARG_POSITION_LIST, 0)

val binding: ForecastWeatherFragmentBinding = DataBindingUtil.inflate(inflater,
R.layout.forecast_weather_fragment, container, false)
val binding: ForecastWeatherFragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.forecast_weather_fragment, container, false
)
binding.obsWeather = observableWeather
return binding.root


// return inflater.inflate(R.layout.forecast_weather_fragment, container, false)
}

Expand Down Expand Up @@ -146,7 +149,9 @@ class WeatherForecastFragment : Fragment() {
resource: Resource<WeatherByLocationResponse>
) {
Timber.d("processWeatherResults , resource.status = ${resource.status}")
observableWeather.progressStatus = resource.status
observableWeather.progressStatus.set(if (resource.status == Status.LOADING) View.VISIBLE else View.GONE)
observableWeather.errorMsg.set(resource.message)

when (resource.status) {
Status.LOADING -> {
// forecast_weather_progressbar.visibility = View.VISIBLE
Expand All @@ -160,7 +165,7 @@ class WeatherForecastFragment : Fragment() {
Status.ERROR -> {
// forecast_weather_progressbar.visibility = View.INVISIBLE
// forecast_weather_error_textview.visibility = View.VISIBLE
observableWeather.errorMsg = resource.message
// observableWeather.errorMsg = resource.message
// forecast_weather_error_textview.text = resource.message
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/forecast_weather_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
android:id="@+id/forecast_weather_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{obsWeather.progressStatus == Status.LOADING ? View.VISIBLE : View.GONE}"
android:visibility="@{obsWeather.progressStatus}"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down

0 comments on commit 0f13518

Please sign in to comment.