-
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
77952bd
commit 68febac
Showing
22 changed files
with
299 additions
and
77 deletions.
There are no files selected for viewing
11 changes: 8 additions & 3 deletions
11
app/src/main/java/com/sun/weather/data/model/FavouriteLocation.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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package com.sun.weather.data.model | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "favourite_locations") | ||
data class FavouriteLocation( | ||
val id: Long? = null, | ||
val cityName: String, | ||
val countryName: String, | ||
@PrimaryKey(autoGenerate = true) val id: Long? = null, | ||
@ColumnInfo(name = "city_name") val cityName: String, | ||
@ColumnInfo(name = "country_name") val countryName: String, | ||
) |
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
23 changes: 4 additions & 19 deletions
23
app/src/main/java/com/sun/weather/data/repository/WeatherRepository.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
6 changes: 4 additions & 2 deletions
6
app/src/main/java/com/sun/weather/data/repository/source/local/AppDatabase.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
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/sun/weather/data/repository/source/local/dao/FavouriteDao.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,22 @@ | ||
package com.sun.weather.data.repository.source.local.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import com.sun.weather.data.model.FavouriteLocation | ||
|
||
@Dao | ||
interface FavouriteDao { | ||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertFavourite(favourite: FavouriteLocation) | ||
|
||
@Query("SELECT * FROM favourite_locations") | ||
suspend fun getAllFavourite(): List<FavouriteLocation> | ||
|
||
@Query("DELETE FROM favourite_locations WHERE id = :favouriteId") | ||
suspend fun removeFavouriteItem(favouriteId: Long) | ||
|
||
@Query("SELECT COUNT(*) FROM favourite_locations WHERE city_name = :cityName") | ||
suspend fun countCityByName(cityName: String): Int | ||
} |
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
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/sun/weather/ui/favourite/FavouriteAdapter.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,51 @@ | ||
package com.sun.weather.ui.favourite | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.sun.weather.data.model.FavouriteLocation | ||
import com.sun.weather.databinding.ItemFavouriteBinding | ||
import com.sun.weather.utils.listener.OnItemClickListener | ||
|
||
class FavouriteAdapter(private var listener: OnItemClickListener) : RecyclerView.Adapter<FavouriteAdapter.ViewHolder>() { | ||
private val mListWeathers by lazy { mutableListOf<FavouriteLocation>() } | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int, | ||
): ViewHolder { | ||
val inflater = LayoutInflater.from(parent.context) | ||
return ViewHolder(ItemFavouriteBinding.inflate(inflater, parent, false)) | ||
} | ||
|
||
override fun getItemCount(): Int { | ||
return mListWeathers.size | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: ViewHolder, | ||
position: Int, | ||
) { | ||
holder.bindData(mListWeathers[position]) | ||
holder.buttonFavourite.setOnClickListener { | ||
listener.onItemClickListener(holder.itemView, position) | ||
} | ||
} | ||
|
||
fun updateData(listData: List<FavouriteLocation>) { | ||
mListWeathers.clear() | ||
if (listData != null) { | ||
mListWeathers.addAll(listData) | ||
} | ||
notifyDataSetChanged() | ||
} | ||
|
||
inner class ViewHolder(private val binding: ItemFavouriteBinding) : RecyclerView.ViewHolder(binding.root) { | ||
val buttonFavourite = binding.btnHeart | ||
|
||
fun bindData(newItem: FavouriteLocation) { | ||
binding.tvCityName.text = newItem.cityName | ||
binding.tvCountryName.text = newItem.countryName | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/sun/weather/ui/favourite/FavouriteFragment.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,60 @@ | ||
package com.sun.weather.ui.favourite | ||
|
||
import android.util.Log | ||
import android.view.View | ||
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.FragmentFavouriteBinding | ||
import com.sun.weather.ui.SharedViewModel | ||
import com.sun.weather.utils.ext.goBackFragment | ||
import com.sun.weather.utils.listener.OnItemClickListener | ||
import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
import org.koin.androidx.viewmodel.ext.android.viewModel | ||
|
||
class FavouriteFragment() : BaseFragment<FragmentFavouriteBinding>(FragmentFavouriteBinding::inflate), OnItemClickListener { | ||
override val viewModel: FavouriteViewModel by viewModel() | ||
override lateinit var sharedViewModel: SharedViewModel | ||
private lateinit var favoriteAdapter: FavouriteAdapter | ||
|
||
override fun initView() { | ||
sharedViewModel = activityViewModel<SharedViewModel>().value | ||
favoriteAdapter = FavouriteAdapter(this) | ||
binding.rvFavorite.apply { | ||
layoutManager = LinearLayoutManager(requireContext()) | ||
adapter = favoriteAdapter | ||
} | ||
|
||
binding.toolBar.findViewById<ImageButton>(R.id.btn_back).setOnClickListener { | ||
goBackFragment() | ||
} | ||
} | ||
|
||
override fun initData() { | ||
viewModel.getAllFavouriteLocations() | ||
} | ||
|
||
override fun bindData() { | ||
viewModel.favouriteLocations.observe(viewLifecycleOwner) { favouriteList -> | ||
favouriteList?.let { | ||
favoriteAdapter.updateData(it) | ||
} | ||
} | ||
|
||
viewModel.error.observe(viewLifecycleOwner) { errorMessage -> | ||
Log.v("FavouriteFragment", "Error: $errorMessage") | ||
} | ||
} | ||
|
||
override fun onItemClickListener( | ||
view: View, | ||
position: Int, | ||
) { | ||
val favouriteLocation = viewModel.favouriteLocations.value?.get(position) | ||
favouriteLocation?.id?.let { viewModel.removeFavouriteItem(it) } | ||
} | ||
companion object { | ||
fun newInstance() = FavouriteFragment() | ||
} | ||
} |
Oops, something went wrong.