Skip to content

Commit

Permalink
Merge pull request #17 from VladShurakov/fix-issue-16
Browse files Browse the repository at this point in the history
Fix issue 16
  • Loading branch information
VladShurakov authored Nov 22, 2023
2 parents dfb50d7 + 3b3f057 commit e3bfea5
Show file tree
Hide file tree
Showing 36 changed files with 156 additions and 128 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ dependencies {
val hiltVersion = "2.48.1"
val retrofitVersion = "2.9.0"
val roomVersion = "2.6.0"
val navVersion = "2.7.5"

// Room
// implementation("androidx.room:room-runtime:$roomVersion")
implementation("androidx.room:room-runtime:$roomVersion")
ksp("androidx.room:room-compiler:$roomVersion")
implementation("androidx.room:room-ktx:$roomVersion")

Expand Down Expand Up @@ -81,6 +82,10 @@ dependencies {
// Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")

// Navigation
implementation("androidx.navigation:navigation-fragment-ktx:$navVersion")
implementation("androidx.navigation:navigation-ui-ktx:$navVersion")

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.10.0")
Expand Down
30 changes: 1 addition & 29 deletions app/src/main/java/com/example/weatherapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.fragment.app.Fragment
import com.example.weatherapp.databinding.ActivityMainBinding
import com.example.weatherapp.feature_city_search.present.fragment.CityFragment
import com.example.weatherapp.feature_settings.presenter.fragment.SettingsFragment
import com.example.weatherapp.feature_settings.presenter.viewmodel.SettingsViewModel
import com.example.weatherapp.feature_weather.presenter.fragment.WeatherFragment
import com.example.weatherapp.core.Screen
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -24,7 +19,7 @@ class MainActivity : AppCompatActivity() {
setContentView(binding?.root)

if (savedInstanceState == null) {
openScreen(Screen.Weather)
// TODO("savedInstanceState == null")
}

// Set theme from sharedpref
Expand All @@ -40,27 +35,4 @@ class MainActivity : AppCompatActivity() {
}
}
}

private fun openFragment(fragment: Fragment) {
supportFragmentManager
.beginTransaction()
.replace(R.id.placeholder, fragment)
.commit()
}

fun openScreen(screen: Screen) {
when (screen) {
Screen.Weather -> {
openFragment(WeatherFragment.newInstance())
}

Screen.SearchCity -> {
openFragment(CityFragment.newInstance())
}

Screen.Settings -> {
openFragment(SettingsFragment.newInstance())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ data class CityEntity(
@ColumnInfo("admin")
val admin: String,
@ColumnInfo("inFavorite")
val inFavorite: Boolean = false
var inFavorite: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.example.weatherapp.R
import com.example.weatherapp.WeatherApplication
import com.example.weatherapp.feature_city_search.domain.model.CityGeo
import com.example.weatherapp.feature_city_search.domain.repository.NetworkCityRepository
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

/*
* Get Cities from network by city name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import com.example.weatherapp.databinding.CardCityBinding
import com.example.weatherapp.feature_city_search.domain.model.CityEntity
import com.example.weatherapp.feature_city_search.domain.model.CityResult
import com.example.weatherapp.feature_city_search.present.adapter.model.CityAdapterModel
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

class CitiesAdapter(
private val onCityListener: OnCityListener,
) : RecyclerView.Adapter<CitiesAdapter.CityHolder>() {
private var cityEntities = listOf<CityEntity>()
private var cityEntities = mutableListOf<CityEntity>()

inner class CityHolder(view: View) : RecyclerView.ViewHolder(view) {
private val binding = CardCityBinding.bind(view)
Expand All @@ -25,17 +25,23 @@ class CitiesAdapter(
tvCityInfo.text = cityAdapterModel.cityInfo
when (cityAdapterModel.cityEntity.inFavorite){
true -> {
imFavorite.setImageResource(R.drawable.ic_star_filled)
imFavorite.setImageResource(R.drawable.ic_favorite_filled)
}
false -> {
imFavorite.setImageResource(R.drawable.ic_star_outfilled)
imFavorite.setImageResource(R.drawable.ic_favorite_bordered)
}
}

itemView.setOnClickListener {
onCityListener.onCityClick(cityAdapterModel.cityResult)
}

imFavorite.setOnClickListener {
onCityListener.onFavoriteClick(cityAdapterModel.cityEntity)
onCityListener.toggleFavorite(cityAdapterModel.cityEntity)
cityEntities = cityEntities.apply {
this[adapterPosition].inFavorite = !this[adapterPosition].inFavorite
}
notifyItemChanged(adapterPosition)
}
}
}
Expand Down Expand Up @@ -89,13 +95,13 @@ class CitiesAdapter(

@SuppressLint("NotifyDataSetChanged")
fun setCityEntities(cityEntities: List<CityEntity>) {
this.cityEntities = cityEntities
this.cityEntities = cityEntities.toMutableList()
notifyDataSetChanged()
}

interface OnCityListener {
fun onCityClick(cityResult: NetworkResult<CityResult>)

fun onFavoriteClick(cityEntity: CityEntity)
fun toggleFavorite(cityEntity: CityEntity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.weatherapp.feature_city_search.present.adapter.model
import com.example.weatherapp.feature_city_search.domain.model.CityEntity
import com.example.weatherapp.feature_city_search.domain.model.CityResult
import com.example.weatherapp.feature_city_search.present.adapter.CitiesAdapter
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

/*
* Model for City Adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import android.view.ViewGroup
import android.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.weatherapp.MainActivity
import com.example.weatherapp.R
import com.example.weatherapp.databinding.FragmentCitySearchBinding
import com.example.weatherapp.feature_city_search.domain.model.CityEntity
Expand All @@ -17,8 +17,7 @@ import com.example.weatherapp.feature_city_search.present.adapter.CitiesAdapter
import com.example.weatherapp.feature_city_search.present.viewmodel.model.CitySearchUIState
import com.example.weatherapp.feature_city_search.present.viewmodel.CitySearchViewModel
import com.example.weatherapp.feature_weather.presenter.viewmodel.WeatherViewModel
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.core.Screen
import com.example.weatherapp.util.NetworkResult

class CityFragment : Fragment(), CitiesAdapter.OnCityListener {
private var binding: FragmentCitySearchBinding? = null
Expand Down Expand Up @@ -83,10 +82,11 @@ class CityFragment : Fragment(), CitiesAdapter.OnCityListener {
false
)
adapter = citiesAdapter
itemAnimator = null
}

ibBack.setOnClickListener {
(activity as MainActivity?)?.openScreen(Screen.Weather)
findNavController().navigate(R.id.navigateToWeatherFragment)
}

// SearchView
Expand All @@ -111,20 +111,13 @@ class CityFragment : Fragment(), CitiesAdapter.OnCityListener {
binding = null
}

companion object {
@JvmStatic
fun newInstance() = CityFragment()
}

override fun onCityClick(cityResult: NetworkResult<CityResult>) {
weatherViewModel.getWeather(cityResult)
(activity as MainActivity).openScreen(Screen.Weather)
findNavController().navigate(R.id.navigateToWeatherFragment)

}

override fun onFavoriteClick(cityEntity: CityEntity) {
override fun toggleFavorite(cityEntity: CityEntity) {
citySearchViewModel.toggleFavorite(cityEntity)
// Get cities with new data
val cityName = citySearchViewModel.citySearchState.value?.currentCityName ?: ""
citySearchViewModel.getCities(cityName, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.example.weatherapp.feature_city_search.domain.model.CityEntity
import com.example.weatherapp.feature_city_search.domain.use_case.CitySearchUseCases
import com.example.weatherapp.feature_city_search.present.viewmodel.model.CitySearchState
import com.example.weatherapp.feature_city_search.present.viewmodel.model.CitySearchUIState
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.example.weatherapp.R
import com.example.weatherapp.databinding.FragmentSettingsBinding
import com.example.weatherapp.feature_settings.domain.model.weather_unit.PrecipitationUnit
import com.example.weatherapp.feature_settings.domain.model.SettingsBundle
import com.example.weatherapp.feature_settings.domain.model.weather_unit.TempUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.WindSpeedUnit
import com.example.weatherapp.MainActivity
import com.example.weatherapp.core.Screen
import com.example.weatherapp.feature_settings.presenter.viewmodel.SettingsViewModel

class SettingsFragment : Fragment() {
Expand All @@ -36,7 +35,7 @@ class SettingsFragment : Fragment() {
binding?.apply {
// Back button
ibBack.setOnClickListener {
(activity as MainActivity?)?.openScreen(Screen.Weather)
findNavController().navigate(R.id.navigateToWeatherFragment)
}

// Init Temp Unit
Expand Down Expand Up @@ -165,8 +164,4 @@ class SettingsFragment : Fragment() {
super.onDestroyView()
binding = null
}

companion object {
fun newInstance() = SettingsFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.example.weatherapp.feature_weather.domain.model.CurrentWeather
import com.example.weatherapp.feature_weather.domain.model.DailyWeather
import com.example.weatherapp.feature_weather.domain.model.HourlyWeather
import com.example.weatherapp.feature_weather.domain.repository.WeatherRepository
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult
import javax.inject.Inject

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.example.weatherapp.feature_weather.domain.model.HourlyWeather
import com.example.weatherapp.feature_settings.domain.model.weather_unit.PrecipitationUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.TempUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.WindSpeedUnit
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

interface WeatherRepository {
suspend fun getCurrentWeather(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.example.weatherapp.feature_settings.domain.model.weather_unit.Precipi
import com.example.weatherapp.feature_settings.domain.model.weather_unit.TempUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.WindSpeedUnit
import com.example.weatherapp.feature_weather.domain.repository.WeatherRepository
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

class GetCurrentWeather(
private val repository: WeatherRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.example.weatherapp.feature_settings.domain.model.weather_unit.Precipi
import com.example.weatherapp.feature_settings.domain.model.weather_unit.TempUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.WindSpeedUnit
import com.example.weatherapp.feature_weather.domain.repository.WeatherRepository
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

class GetDailyWeather (
private val repository: WeatherRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.example.weatherapp.feature_settings.domain.model.weather_unit.Precipi
import com.example.weatherapp.feature_settings.domain.model.weather_unit.TempUnit
import com.example.weatherapp.feature_settings.domain.model.weather_unit.WindSpeedUnit
import com.example.weatherapp.feature_weather.domain.repository.WeatherRepository
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult

class GetHourlyWeather(
private val repository: WeatherRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.example.weatherapp.R
import com.example.weatherapp.feature_weather.domain.model.DailyWeather
import com.example.weatherapp.databinding.CardDailyWeatherBinding
import com.example.weatherapp.core.WeatherType
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.WeatherType
import com.example.weatherapp.util.NetworkResult
import com.example.weatherapp.feature_weather.presenter.adapter.model.DailyWeatherAdapterModel
import java.lang.Exception

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import androidx.recyclerview.widget.RecyclerView
import com.example.weatherapp.R
import com.example.weatherapp.feature_weather.domain.model.HourlyWeather
import com.example.weatherapp.databinding.CardHourlyWeatherBinding
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.core.WeatherType
import com.example.weatherapp.util.NetworkResult
import com.example.weatherapp.util.WeatherType
import com.example.weatherapp.feature_weather.presenter.adapter.model.HourlyWeatherAdapterModel
import java.lang.Exception

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.weatherapp.MainActivity
import com.example.weatherapp.R
import com.example.weatherapp.databinding.FragmentWeatherBinding
import com.example.weatherapp.feature_weather.presenter.adapter.DailyWeatherAdapter
import com.example.weatherapp.feature_weather.presenter.adapter.HourlyWeatherAdapter
import com.example.weatherapp.feature_weather.presenter.viewmodel.WeatherUiState
import com.example.weatherapp.feature_weather.presenter.viewmodel.modle.WeatherUiState
import com.example.weatherapp.feature_weather.presenter.viewmodel.WeatherViewModel
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.core.Screen
import com.example.weatherapp.core.WeatherType
import com.example.weatherapp.util.NetworkResult
import com.example.weatherapp.util.WeatherType

class WeatherFragment : Fragment() {
private var binding: FragmentWeatherBinding? = null
Expand All @@ -29,18 +28,20 @@ class WeatherFragment : Fragment() {
): View? {
// Binding
binding = FragmentWeatherBinding.inflate(inflater, container, false)
binding?.apply {
ibSettings.setOnClickListener {
findNavController().navigate(R.id.navigateToSettingsFragment)
}
ibAdd.setOnClickListener {
findNavController().navigate(R.id.navigateToCitySearchFragment)
}
}
return binding?.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.apply {
ibSettings.setOnClickListener {
(activity as MainActivity?)?.openScreen(Screen.Settings)
}
ibAdd.setOnClickListener {
(activity as MainActivity?)?.openScreen(Screen.SearchCity)
}

weatherViewModel.weatherState.observe(viewLifecycleOwner) { weatherState ->
// Adapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import androidx.lifecycle.viewModelScope
import com.example.weatherapp.feature_city_search.domain.model.CityResult
import com.example.weatherapp.feature_settings.domain.use_case.SettingsUseCases
import com.example.weatherapp.feature_weather.domain.use_case.WeatherUseCases
import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult
import com.example.weatherapp.feature_weather.presenter.viewmodel.modle.WeatherState
import com.example.weatherapp.feature_weather.presenter.viewmodel.modle.WeatherUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.weatherapp.feature_weather.presenter.viewmodel
package com.example.weatherapp.feature_weather.presenter.viewmodel.modle

import com.example.weatherapp.core.NetworkResult
import com.example.weatherapp.util.NetworkResult
import com.example.weatherapp.feature_city_search.domain.model.CityResult
import com.example.weatherapp.feature_weather.domain.model.CurrentWeather
import com.example.weatherapp.feature_weather.domain.model.DailyWeather
Expand Down
Loading

0 comments on commit e3bfea5

Please sign in to comment.