-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lê Chí Dũng
committed
Sep 9, 2024
1 parent
423de25
commit 5fdcd3b
Showing
20 changed files
with
373 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,4 +102,4 @@ dependencies { | |
// WorkManager | ||
implementation 'androidx.work:work-runtime-ktx:2.9.0' | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 7 additions & 8 deletions
15
app/src/main/java/com/sun/weather/data/repository/source/local/WeatherLocalDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/sun/weather/ui/detail/DailyAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.sun.weather.ui.detail | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.bumptech.glide.Glide | ||
import com.sun.weather.data.model.WeatherBasic | ||
import com.sun.weather.databinding.ForecastDailyBinding | ||
import com.sun.weather.utils.Constant | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
class DailyAdapter(private var forecastList: List<WeatherBasic>) : RecyclerView.Adapter<DailyAdapter.ViewHolder>() { | ||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
return ViewHolder(ForecastDailyBinding.inflate(inflater, parent, false)) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return forecastList.size | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: ViewHolder, | ||
position: Int, | ||
) { | ||
holder.bindData(forecastList[position]) | ||
} | ||
|
||
inner class ViewHolder(private val binding: ForecastDailyBinding) : RecyclerView.ViewHolder(binding.root) { | ||
fun bindData(item: WeatherBasic) { | ||
val dayFormat = SimpleDateFormat(DAY_ONLY_PATTERN, Locale.getDefault()) | ||
val day = dayFormat.format(Date(item.dateTime!! * SECOND_TO_MILLIS)) | ||
binding.tvDay.text = day | ||
binding.tvStatus.text = item.weatherDescription | ||
binding.tvMaxTemp.text = String.format("%.1f", item.maxTemperature) + "°C" | ||
binding.tvMinTemp.text = String.format("%.1f", item.minTemperature) + "°C" | ||
val iconWeatherUrl = "${Constant.BASE_ICON_URL}${item.iconWeather}@2x.png" | ||
Glide.with(itemView.context.applicationContext) | ||
.load(iconWeatherUrl) | ||
.into(binding.imgStatus) | ||
} | ||
} | ||
|
||
fun updateData(list: List<WeatherBasic>) { | ||
forecastList = list | ||
notifyDataSetChanged() | ||
} | ||
companion object { | ||
const val DAY_ONLY_PATTERN = "dd-MM-yyyy" | ||
const val SECOND_TO_MILLIS = 1000 | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
app/src/main/java/com/sun/weather/ui/detail/DetailFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.sun.weather.ui.detail | ||
|
||
import android.icu.text.SimpleDateFormat | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.ImageButton | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.sun.weather.R | ||
import com.sun.weather.base.BaseFragment | ||
import com.sun.weather.databinding.FragmentDetailBinding | ||
import com.sun.weather.ui.SharedViewModel | ||
import com.sun.weather.utils.ext.goBackFragment | ||
import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
class DetailFragment : BaseFragment<FragmentDetailBinding>(FragmentDetailBinding::inflate) { | ||
override val viewModel: DetailViewModel by viewModel() | ||
override lateinit var sharedViewModel: SharedViewModel | ||
private lateinit var dailyAdapter: DailyAdapter | ||
private lateinit var hourlyAdapter: HourlyAdapter | ||
private var isNetworkAvailable: Boolean = true | ||
private var cityName: String? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
arguments?.let { | ||
cityName = it.getString(ARG_CITY_NAME) | ||
} | ||
} | ||
|
||
override fun initView() { | ||
sharedViewModel = activityViewModel<SharedViewModel>().value | ||
isNetworkAvailable = sharedViewModel.isNetworkAvailable.value ?: true | ||
binding.toolbar.findViewById<ImageButton>(R.id.btn_back).setOnClickListener { | ||
goBackFragment() | ||
} | ||
|
||
hourlyAdapter = HourlyAdapter(mutableListOf()) | ||
binding.recyclerViewHourly.apply { | ||
layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false) | ||
adapter = hourlyAdapter | ||
} | ||
|
||
dailyAdapter = DailyAdapter(mutableListOf()) | ||
binding.recylerViewDaily.apply { | ||
layoutManager = LinearLayoutManager(requireContext()) | ||
adapter = dailyAdapter | ||
} | ||
binding.tvToolbarTitle.text = getString(R.string.city_name_app_bar, getString(R.string.forecast_detail), cityName) | ||
val currentDate = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(Date()) | ||
binding.tvCurrentTime.text = currentDate | ||
} | ||
|
||
override fun initData() { | ||
cityName?.let { name -> | ||
Log.v(MY_TAG, "initData: cityName: $name, isNetworkAvailable: $isNetworkAvailable") | ||
viewModel.loadWeeklyForecast(name, isNetworkAvailable, "vi") | ||
viewModel.loadHourlyForecast(name, isNetworkAvailable, "vi") | ||
} | ||
} | ||
|
||
override fun bindData() { | ||
viewModel.weeklyForecast.observe(viewLifecycleOwner) { forecast -> | ||
dailyAdapter.updateData(forecast) | ||
} | ||
|
||
viewModel.hourlyForecast.observe(viewLifecycleOwner) { forecast -> | ||
hourlyAdapter.updateData(forecast) | ||
} | ||
|
||
viewModel.error.observe(viewLifecycleOwner) { errorMessage -> | ||
Log.d(MY_TAG, "onError: $errorMessage") | ||
} | ||
} | ||
|
||
companion object { | ||
private const val ARG_CITY_NAME = "city_name" | ||
const val MY_TAG = "DetailFragment" | ||
|
||
fun newInstance(cityName: String): DetailFragment { | ||
val fragment = DetailFragment() | ||
val args = | ||
Bundle().apply { | ||
putString(ARG_CITY_NAME, cityName) | ||
} | ||
fragment.arguments = args | ||
return fragment | ||
} | ||
} | ||
} |
Oops, something went wrong.