-
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 d7e6379
Showing
23 changed files
with
310 additions
and
79 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
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
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.