Skip to content

Commit

Permalink
Merge pull request #17 from m1awicki/ml/countries_reports_follow_sett…
Browse files Browse the repository at this point in the history
…ings

Countries reports follow feature
  • Loading branch information
MDikkii authored Mar 20, 2020
2 parents 67f3a6f + cbf072f commit 43e1069
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mobilecoronatracker.data.persistence

interface CountriesFollowRepo {
fun addFollowedCountry(country: String)
fun removeFollowedCountry(country: String)
fun getFollowedCountries(): List<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mobilecoronatracker.data.persistence.impl

import android.content.Context
import android.content.SharedPreferences
import com.mobilecoronatracker.data.persistence.CountriesFollowRepo

class SharedPreferencesCountriesFollowRepo(context: Context) : CountriesFollowRepo {
private val sharedPrefs: SharedPreferences

init {
sharedPrefs = context.getSharedPreferences(SHARED_PREFS_FILE_NAME, SHARED_PREFS_MODE)
}

override fun addFollowedCountry(country: String) {
val followed = sharedPrefs.getStringSet(FOLLOWED_COUNTRIES, emptySet()) ?: emptySet()
if (followed.contains(country)) {
return
}
val newSet = HashSet(followed)
newSet.add(country)
sharedPrefs.edit().putStringSet(FOLLOWED_COUNTRIES, newSet).apply()
}

override fun removeFollowedCountry(country: String) {
val followed = sharedPrefs.getStringSet(FOLLOWED_COUNTRIES, emptySet()) ?: emptySet()
if (followed.contains(country)) {
val newSet = HashSet(followed)
newSet.remove(country)
sharedPrefs.edit().putStringSet(FOLLOWED_COUNTRIES, newSet).apply()
}
}

override fun getFollowedCountries(): List<String> {
val followed = sharedPrefs.getStringSet(FOLLOWED_COUNTRIES, emptySet()) ?: emptySet()
return followed.toList()
}

companion object {
const val FOLLOWED_COUNTRIES = "corona.tracker.prefs.followed"
const val SHARED_PREFS_FILE_NAME = "corona.tracker.prefs"
const val SHARED_PREFS_MODE = Context.MODE_PRIVATE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ package com.mobilecoronatracker.model

import com.mobilecoronatracker.model.pojo.CovidCountryEntry

class CountryReportModel(
override var country: String,
override var cases: Int,
override var todayCases: Int,
override var deaths: Int,
override var todayDeaths: Int,
override var recovered: Int,
override var critical: Int,
override var active: Int
data class CountryReportModel(
override val country: String,
override val cases: Int,
override val todayCases: Int,
override val deaths: Int,
override val todayDeaths: Int,
override val recovered: Int,
override val critical: Int,
override val active: Int,
override var followed: Boolean
) : CountryReportModelable {
constructor(data: CovidCountryEntry) :
this(data.country, data.cases, data.todayCases, data.deaths, data.todayDeaths,
data.recovered, data.critical, data.active)
}
this(
data.country,
data.cases,
data.todayCases,
data.deaths,
data.todayDeaths,
data.recovered,
data.critical,
data.active,
followed = false
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.mobilecoronatracker.model

interface CountryReportModelable {
var country: String
var cases: Int
var todayCases: Int
var deaths: Int
var todayDeaths: Int
var recovered: Int
var critical: Int
var active: Int
}
val country: String
val cases: Int
val todayCases: Int
val deaths: Int
val todayDeaths: Int
val recovered: Int
val critical: Int
val active: Int
var followed: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import androidx.recyclerview.widget.RecyclerView
import com.mobilecoronatracker.databinding.ItemCountryReportBinding
import com.mobilecoronatracker.model.CountryReportModelable

class CountriesListAdapter : RecyclerView.Adapter<CountriesListAdapter.ViewHolder>() {

class CountriesListAdapter :
RecyclerView.Adapter<CountriesListAdapter.ViewHolder>() {
var reports = emptyList<CountryReportModelable>()
var listener: CountryFollowListener? = null

inner class ViewHolder(private val binding: ItemCountryReportBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: CountryReportModelable) {
binding.item = item
binding.listener = listener
binding.executePendingBindings()
}
}
Expand All @@ -25,5 +29,6 @@ class CountriesListAdapter : RecyclerView.Adapter<CountriesListAdapter.ViewHolde

override fun getItemCount(): Int = reports.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(reports[position])
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) =
holder.bind(reports[position])
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package com.mobilecoronatracker.ui.countriesreports
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.mobilecoronatracker.data.persistence.CountriesFollowRepo
import com.mobilecoronatracker.data.source.CovidCountriesDataObserver
import com.mobilecoronatracker.data.source.CovidDataSource
import com.mobilecoronatracker.data.source.impl.CovidRestDataReader
import com.mobilecoronatracker.model.CountryReportModelable
import com.mobilecoronatracker.ui.cumulatedreport.CumulatedReportViewModel
import java.util.Locale

class CountriesListViewModel : ViewModel(), CountriesListViewModelable, CovidCountriesDataObserver {
class CountriesListViewModel(private val countriesFollowRepo: CountriesFollowRepo) :
ViewModel(), CountriesListViewModelable, CovidCountriesDataObserver, CountryFollowListener {
private var currentList: List<CountryReportModelable> = emptyList()
private var currentFilterText: String = ""
override val countryReports = MutableLiveData<List<CountryReportModelable>>()
Expand Down Expand Up @@ -42,15 +43,46 @@ class CountriesListViewModel : ViewModel(), CountriesListViewModelable, CovidCou
dataSource.requestData()
}

override fun onCountryFollowed(countryName: String) {
countriesFollowRepo.addFollowedCountry(countryName)
postFilteredList()
}

override fun onCountryUnfollowed(countryName: String) {
countriesFollowRepo.removeFollowedCountry(countryName)
postFilteredList()
}

private fun postFilteredList() {
countryReports.postValue(
currentList.filter {
it.country
.toLowerCase(Locale.getDefault())
.contains(
currentFilterText.toLowerCase(Locale.getDefault())
)
})
val filtered = getFilteredList()
countryReports.postValue(getFollowStatusList(filtered))
isRefreshing.postValue(false)
}
}

private fun getFilteredList(): List<CountryReportModelable> {
return currentList.filter {
it.country
.toLowerCase(Locale.getDefault())
.contains(
currentFilterText.toLowerCase(Locale.getDefault())
)
}
}

private fun getFollowStatusList(filtered: List<CountryReportModelable>): List<CountryReportModelable> {
val followedCountries = countriesFollowRepo.getFollowedCountries()
val followed = filtered.filter {
followedCountries.contains(it.country)
}.map {
it.followed = true
it
}
val notFollowed = filtered.filterNot {
followedCountries.contains(it.country)
}.map {
it.followed = false;
it
}
return followed + notFollowed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ interface CountriesListViewModelable {

val countryReports: LiveData<List<CountryReportModelable>>
val isRefreshing: LiveData<Boolean>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mobilecoronatracker.ui.countriesreports

interface CountryFollowListener {
fun onCountryFollowed(countryName: String)
fun onCountryUnfollowed(countryName: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.mobilecoronatracker.R
import com.mobilecoronatracker.data.persistence.impl.SharedPreferencesCountriesFollowRepo
import com.mobilecoronatracker.databinding.FragmentCountriesReportsBinding
import com.mobilecoronatracker.ui.utils.hideKeyboard
import kotlinx.android.synthetic.main.fragment_countries_reports.*

class FragmentCountriesReports : Fragment() {
private val viewModel = CountriesListViewModel()
private val adapter = CountriesListAdapter()
private val viewModel by lazy {
CountriesListViewModel(SharedPreferencesCountriesFollowRepo(requireContext()))
}
private val adapter by lazy {
CountriesListAdapter()
}

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -29,6 +34,7 @@ class FragmentCountriesReports : Fragment() {
val binding = DataBindingUtil.inflate<FragmentCountriesReportsBinding>(
inflater, R.layout.fragment_countries_reports, container, false
)
adapter.listener = viewModel
binding.adapter = adapter
binding.viewModel = viewModel
binding.lifecycleOwner = viewLifecycleOwner
Expand All @@ -41,6 +47,11 @@ class FragmentCountriesReports : Fragment() {
bindObservers()
}

override fun onDestroyView() {
super.onDestroyView()
adapter.listener = null
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.countries_reports_menu, menu)
super.onCreateOptionsMenu(menu, inflater)
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_flag.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M14.4,6L14,4H5v17h2v-7h5.6l0.4,2h7V6z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/rounded_bottom_rect_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" />
<solid android:color="@android:color/white" />
</shape>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_countries_reports.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:refreshing="@{viewModel.isRefreshing}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/countries_reports_search_view">
app:layout_constraintTop_toBottomOf="@+id/countries_reports_search_view"
app:refreshing="@{viewModel.isRefreshing}">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/countries_reports"
Expand Down
Loading

0 comments on commit 43e1069

Please sign in to comment.