From b931caf768b37e6f688b0e693f94ec0951c3e210 Mon Sep 17 00:00:00 2001 From: Sadeq Al-Mhana <64174395+devsadeq@users.noreply.github.com> Date: Fri, 19 May 2023 00:45:45 +0300 Subject: [PATCH 1/7] remove nested recycler --- .../marvel/ui/comics/ComicAdapter.kt | 12 -- .../marvel/ui/comics/ComicsCollection.kt | 10 -- .../marvel/ui/comics/ComicsScreenAdapter.kt | 29 ---- .../marvel/ui/comics/ComicsViewModel.kt | 140 ------------------ app/src/main/res/layout/list_comics.xml | 46 ------ 5 files changed, 237 deletions(-) delete mode 100644 app/src/main/java/com/red_velvet/marvel/ui/comics/ComicAdapter.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsCollection.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsScreenAdapter.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt delete mode 100644 app/src/main/res/layout/list_comics.xml diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicAdapter.kt deleted file mode 100644 index 3f2b5e62..00000000 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicAdapter.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.red_velvet.marvel.ui.comics - -import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Comic -import com.red_velvet.marvel.ui.base.BaseAdapter - -class ComicAdapter( - items: List, - listener: ComicsInteractionListener -) : BaseAdapter(items, listener) { - override val layoutId = R.layout.item_comic -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsCollection.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsCollection.kt deleted file mode 100644 index e8b45b6e..00000000 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsCollection.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.red_velvet.marvel.ui.comics - -import androidx.annotation.StringRes -import com.red_velvet.marvel.data.model.Comic -import com.red_velvet.marvel.ui.utils.State - -data class ComicsCollection( - @StringRes val titleId: Int, - val comics: State?> -) \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsScreenAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsScreenAdapter.kt deleted file mode 100644 index 80255b9e..00000000 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsScreenAdapter.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.red_velvet.marvel.ui.comics - -import com.red_velvet.marvel.R -import com.red_velvet.marvel.databinding.ListComicsBinding -import com.red_velvet.marvel.ui.base.BaseAdapter -import com.red_velvet.marvel.ui.base.BaseInteractionListener - -class ComicsScreenAdapter( - items: List, - private val listener: ComicsInteractionListener, -) : BaseAdapter(items, listener) { - override val layoutId = R.layout.list_comics - - override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { - super.onBindViewHolder(holder, position) - when (holder) { - is ItemViewHolder -> { - (holder.binding as ListComicsBinding).recyclerComics.adapter = ComicAdapter( - emptyList(), - listener - ) - } - } - } -} - -interface ComicsInteractionListener : BaseInteractionListener { - fun doOnComicClicked(comicId: Int) -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt deleted file mode 100644 index e03489b1..00000000 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt +++ /dev/null @@ -1,140 +0,0 @@ -package com.red_velvet.marvel.ui.comics - -import androidx.annotation.StringRes -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Comic -import com.red_velvet.marvel.data.remote.RetrofitClient -import com.red_velvet.marvel.data.repository.MarvelRepository -import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl -import com.red_velvet.marvel.ui.base.BaseViewModel -import com.red_velvet.marvel.ui.utils.SingleEvent -import com.red_velvet.marvel.ui.utils.State - -class ComicsViewModel : BaseViewModel(), ComicsInteractionListener { - - private val repository: MarvelRepository by lazy { - MarvelRepositoryImpl(RetrofitClient.apiService) - } - - private val _navigationToComicDetails: MutableLiveData> = MutableLiveData() - val navigationToComicDetails: LiveData> = _navigationToComicDetails - - @StringRes - private val thisWeekStringResource = R.string.this_week - - @StringRes - private val nextWeekStringResource = R.string.next_week - - @StringRes - private val lastWeekStringResource = R.string.last_week - - @StringRes - private val thisMonthStringResource = R.string.this_month - - private val _comicsCollections: MutableLiveData> = - MutableLiveData(emptyList()) - val comicCollections: LiveData> = _comicsCollections - - private val _thisWeekComics = MutableLiveData>>(State.Loading) - val thisWeekComicsLiveData: LiveData>> = _thisWeekComics - - private val _nextWeekComics = MutableLiveData>>(State.Loading) - - private val _lastWeekComics = MutableLiveData>>(State.Loading) - - private val _thisMonthComics = MutableLiveData>>(State.Loading) - - init { - getThisWeekComics() - getNextWeekComics() - getLastWeekComics() - getThisMonthComics() - } - - fun getThisWeekComics() { - bindStateUpdates( - repository.getAllComics(dateDescriptor = THIS_WEEK), - onError = ::onGetThisWeekComicsFailure, - onNext = ::onGetThisWeekComicsState - ) - } - - private fun onGetThisWeekComicsFailure(throwable: Throwable) { - _thisWeekComics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - - private fun onGetThisWeekComicsState(state: State>) { - _thisWeekComics.postValue(state) - if (state is State.Success) insertNewComicsCollection(thisWeekStringResource, state) - } - - fun getNextWeekComics() { - bindStateUpdates( - repository.getAllComics(dateDescriptor = NEXT_WEEK), - onError = ::onGetNextWeekComicsFailure, - onNext = ::onGetNextWeekComicsState - ) - } - - private fun onGetNextWeekComicsFailure(throwable: Throwable) { - _nextWeekComics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - - private fun onGetNextWeekComicsState(state: State>) { - _nextWeekComics.postValue(state) - if (state is State.Success) insertNewComicsCollection(nextWeekStringResource, state) - } - - fun getLastWeekComics() { - bindStateUpdates( - repository.getAllComics(dateDescriptor = LAST_WEEK), - onError = ::onGetLastWeekComicsFailure, - onNext = ::onGetLastWeekComicsState - ) - } - - private fun onGetLastWeekComicsFailure(throwable: Throwable) { - _lastWeekComics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - - private fun onGetLastWeekComicsState(state: State>) { - _lastWeekComics.postValue(state) - if (state is State.Success) insertNewComicsCollection(lastWeekStringResource, state) - } - - fun getThisMonthComics() { - bindStateUpdates( - repository.getAllComics(dateDescriptor = THIS_MONTH), - onError = ::onGetThisMonthComicsFailure, - onNext = ::onGetThisMonthComicsState - ) - } - - private fun onGetThisMonthComicsFailure(throwable: Throwable) { - _thisMonthComics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - - private fun onGetThisMonthComicsState(state: State>) { - _thisMonthComics.postValue(state) - if (state is State.Success) insertNewComicsCollection(thisMonthStringResource, state) - } - - private fun insertNewComicsCollection(titleId: Int, comics: State>) { - _comicsCollections.value = - _comicsCollections.value!!.plus(ComicsCollection(titleId, comics)) - } - - override fun doOnComicClicked(comicId: Int) { - _navigationToComicDetails.postValue(SingleEvent(comicId)) - } - - companion object { - private const val THIS_WEEK = "thisWeek" - private const val NEXT_WEEK = "nextWeek" - private const val LAST_WEEK = "lastWeek" - private const val THIS_MONTH = "thisMonth" - private const val UNKNOWN_ERROR = "Unknown error" - } -} \ No newline at end of file diff --git a/app/src/main/res/layout/list_comics.xml b/app/src/main/res/layout/list_comics.xml deleted file mode 100644 index 5bae4e33..00000000 --- a/app/src/main/res/layout/list_comics.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file From aa5ac3e68076e643f3979f0d0f7b5b725f6b2979 Mon Sep 17 00:00:00 2001 From: Sadeq Al-Mhana <64174395+devsadeq@users.noreply.github.com> Date: Fri, 19 May 2023 00:47:56 +0300 Subject: [PATCH 2/7] create home screen --- .../com/red_velvet/marvel/ui/MainActivity.kt | 2 +- .../marvel/ui/home/BaseInteractionListener.kt | 9 ++ .../marvel/ui/home/HomeViewModel.kt | 109 +++++++++++++ .../ui/home/adapter/CharactersAdapter.kt | 13 ++ .../marvel/ui/home/adapter/ComicsAdapter.kt | 13 ++ .../marvel/ui/home/adapter/EventsAdapter.kt | 13 ++ app/src/main/res/layout/characters_item.xml | 2 +- app/src/main/res/layout/fragment_comics.xml | 70 -------- app/src/main/res/layout/fragment_home.xml | 151 ++++++++++++++++++ .../main/res/layout/item_comic_character.xml | 2 +- .../main/res/layout/item_home_character.xml | 53 ++++++ .../{item_comic.xml => item_home_comic.xml} | 6 +- app/src/main/res/layout/item_home_event.xml | 53 ++++++ app/src/main/res/layout/item_series.xml | 2 +- app/src/main/res/layout/item_stories.xml | 2 +- app/src/main/res/menu/bottom_nav_menu.xml | 4 +- app/src/main/res/navigation/nav_graph.xml | 16 +- app/src/main/res/values/strings.xml | 1 + 18 files changed, 436 insertions(+), 85 deletions(-) create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/BaseInteractionListener.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt delete mode 100644 app/src/main/res/layout/fragment_comics.xml create mode 100644 app/src/main/res/layout/fragment_home.xml create mode 100644 app/src/main/res/layout/item_home_character.xml rename app/src/main/res/layout/{item_comic.xml => item_home_comic.xml} (90%) create mode 100644 app/src/main/res/layout/item_home_event.xml diff --git a/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt b/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt index 5c970e79..63a8f0cf 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt @@ -34,7 +34,7 @@ class MainActivity : BaseActivity() { private fun initNavigationDestinationListener() { navController.addOnDestinationChangedListener { _, destination, arguments -> when (destination.id) { - R.id.comicsFragment, + R.id.homeFragment, R.id.charactersFragment, R.id.seriesFragment, R.id.eventsFragment, diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/BaseInteractionListener.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/BaseInteractionListener.kt new file mode 100644 index 00000000..bfe0bf5a --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/BaseInteractionListener.kt @@ -0,0 +1,9 @@ +package com.red_velvet.marvel.ui.home + +import com.red_velvet.marvel.ui.base.BaseInteractionListener + +interface HomeInteractionListener : BaseInteractionListener { + fun doOnComicClicked(comicId: Int) + fun doOnEventClicked(eventId: Int) + fun doOnCharacterClicked(charId: Int) +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt new file mode 100644 index 00000000..a009af1b --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt @@ -0,0 +1,109 @@ +package com.red_velvet.marvel.ui.home + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.remote.RetrofitClient +import com.red_velvet.marvel.data.repository.MarvelRepository +import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl +import com.red_velvet.marvel.ui.base.BaseViewModel +import com.red_velvet.marvel.ui.utils.SingleEvent +import com.red_velvet.marvel.ui.utils.State + +class HomeViewModel : BaseViewModel(), HomeInteractionListener { + + private val repository: MarvelRepository by lazy { + MarvelRepositoryImpl(RetrofitClient.apiService) + } + + private val _navigationToComicDetails: MutableLiveData> = MutableLiveData() + val navigationToComicDetails: LiveData> = _navigationToComicDetails + + private val _navigationToEventDetails: MutableLiveData> = MutableLiveData() + val navigationToEventDetails: LiveData> = _navigationToEventDetails + + private val _navigationToCharacterDetails: MutableLiveData> = MutableLiveData() + val navigationToCharacterDetails: LiveData> = _navigationToCharacterDetails + + private val _comics = MutableLiveData>>(State.Loading) + val comicsLiveData: LiveData>> = _comics + + private val _events = MutableLiveData>>(State.Loading) + val eventLiveData: LiveData>> = _events + + private val _characters = MutableLiveData>>(State.Loading) + val characterLiveData: LiveData>> = _characters + + + init { + getComics() + getEvents() + getCharacters() + } + + fun getComics() { + bindStateUpdates( + repository.getAllComics(), + onError = ::onGetComicsFailure, + onNext = ::onGetComicsState + ) + } + + private fun onGetComicsFailure(throwable: Throwable) { + _comics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) + } + + private fun onGetComicsState(state: State>) { + _comics.postValue(state) + } + + fun getEvents() { + bindStateUpdates( + repository.getAllEvents(), + onError = ::onGetEventsFailure, + onNext = ::onGetEventsState + ) + } + + private fun onGetEventsFailure(throwable: Throwable) { + _events.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) + } + + private fun onGetEventsState(state: State>) { + _events.postValue(state) + } + + fun getCharacters() { + bindStateUpdates( + repository.getAllCharacters(), + onError = ::onGetCharactersFailure, + onNext = ::onGetCharactersState + ) + } + + private fun onGetCharactersFailure(throwable: Throwable) { + _characters.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) + } + + private fun onGetCharactersState(state: State>) { + _characters.postValue(state) + } + + override fun doOnComicClicked(comicId: Int) { + _navigationToComicDetails.postValue(SingleEvent(comicId)) + } + + override fun doOnEventClicked(eventId: Int) { + _navigationToEventDetails.postValue(SingleEvent(eventId)) + } + + override fun doOnCharacterClicked(charId: Int) { + _navigationToCharacterDetails.postValue(SingleEvent(charId)) + } + + companion object { + private const val UNKNOWN_ERROR = "Unknown error" + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt new file mode 100644 index 00000000..87d722b2 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt @@ -0,0 +1,13 @@ +package com.red_velvet.marvel.ui.home.adapter + +import com.red_velvet.marvel.R +import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.ui.base.BaseAdapter +import com.red_velvet.marvel.ui.home.HomeInteractionListener + +class CharactersAdapter( + items: List, + listener: HomeInteractionListener +) : BaseAdapter(items, listener) { + override val layoutId = R.layout.item_home_character +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt new file mode 100644 index 00000000..e420717d --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt @@ -0,0 +1,13 @@ +package com.red_velvet.marvel.ui.home.adapter + +import com.red_velvet.marvel.R +import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.ui.base.BaseAdapter +import com.red_velvet.marvel.ui.home.HomeInteractionListener + +class ComicsAdapter( + items: List, + listener: HomeInteractionListener +) : BaseAdapter(items, listener) { + override val layoutId = R.layout.item_home_comic +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt new file mode 100644 index 00000000..c19f884d --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt @@ -0,0 +1,13 @@ +package com.red_velvet.marvel.ui.home.adapter + +import com.red_velvet.marvel.R +import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.ui.base.BaseAdapter +import com.red_velvet.marvel.ui.home.HomeInteractionListener + +class EventsAdapter( + items: List, + listener: HomeInteractionListener +) : BaseAdapter(items, listener) { + override val layoutId = R.layout.item_home_event +} \ No newline at end of file diff --git a/app/src/main/res/layout/characters_item.xml b/app/src/main/res/layout/characters_item.xml index b3a98ecb..1f71195a 100644 --- a/app/src/main/res/layout/characters_item.xml +++ b/app/src/main/res/layout/characters_item.xml @@ -35,7 +35,7 @@ tools:src="@tools:sample/backgrounds/scenic" /> - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml new file mode 100644 index 00000000..79d2885e --- /dev/null +++ b/app/src/main/res/layout/fragment_home.xml @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_comic_character.xml b/app/src/main/res/layout/item_comic_character.xml index daae80e6..60dccc09 100644 --- a/app/src/main/res/layout/item_comic_character.xml +++ b/app/src/main/res/layout/item_comic_character.xml @@ -32,7 +32,7 @@ tools:src="@tools:sample/backgrounds/scenic" /> + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_comic.xml b/app/src/main/res/layout/item_home_comic.xml similarity index 90% rename from app/src/main/res/layout/item_comic.xml rename to app/src/main/res/layout/item_home_comic.xml index 6e796e2a..c6c50756 100644 --- a/app/src/main/res/layout/item_comic.xml +++ b/app/src/main/res/layout/item_home_comic.xml @@ -7,11 +7,11 @@ + type="com.red_velvet.marvel.data.model.Event" /> + type="com.red_velvet.marvel.ui.home.HomeInteractionListener" /> + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/item_series.xml b/app/src/main/res/layout/item_series.xml index 45e1aaf2..cb7a48d3 100644 --- a/app/src/main/res/layout/item_series.xml +++ b/app/src/main/res/layout/item_series.xml @@ -35,7 +35,7 @@ tools:src="@tools:sample/backgrounds/scenic" /> + android:title="@string/home" /> + app:startDestination="@id/homeFragment"> + app:popUpTo="@id/homeFragment" /> + + Stories Search Looks like Thanos snapped this search result away. \nTry searching for something else. + Home \ No newline at end of file From eac23876f5b37d18913a8c2872de359ce1dd83f2 Mon Sep 17 00:00:00 2001 From: Sadeq Al-Mhana <64174395+devsadeq@users.noreply.github.com> Date: Fri, 19 May 2023 00:48:43 +0300 Subject: [PATCH 3/7] handle home navigation --- .../marvel/ui/comics/ComicsFragment.kt | 42 ------------ .../red_velvet/marvel/ui/home/HomeFragment.kt | 67 +++++++++++++++++++ 2 files changed, 67 insertions(+), 42 deletions(-) delete mode 100644 app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt deleted file mode 100644 index 602211f2..00000000 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.red_velvet.marvel.ui.comics - -import android.os.Bundle -import android.view.View -import androidx.fragment.app.viewModels -import androidx.navigation.fragment.findNavController -import com.red_velvet.marvel.R -import com.red_velvet.marvel.databinding.FragmentComicsBinding -import com.red_velvet.marvel.ui.base.BaseFragment - -class ComicsFragment : BaseFragment() { - - override val layoutIdFragment = R.layout.fragment_comics - - override val viewModel: ComicsViewModel by viewModels() - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - val comicsAdapter = ComicsScreenAdapter(emptyList(), viewModel) - binding.recyclerComics.adapter = comicsAdapter - binding.textViewError.setOnClickListener { - viewModel.apply { - getThisWeekComics() - getNextWeekComics() - getLastWeekComics() - getThisMonthComics() - } - } - initNavigateToComicDetails() - } - - private fun initNavigateToComicDetails() { - viewModel.navigationToComicDetails.observe(viewLifecycleOwner) { event -> - event.getContentIfNotHandled()?.let { - val directions = - ComicsFragmentDirections.actionComicsFragmentToComicDetailsFragment(it) - findNavController().navigate(directions) - } - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt new file mode 100644 index 00000000..ef9a287e --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt @@ -0,0 +1,67 @@ +package com.red_velvet.marvel.ui.home + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.viewModels +import androidx.navigation.fragment.findNavController +import com.red_velvet.marvel.R +import com.red_velvet.marvel.databinding.FragmentHomeBinding +import com.red_velvet.marvel.ui.base.BaseFragment +import com.red_velvet.marvel.ui.home.adapter.CharactersAdapter +import com.red_velvet.marvel.ui.home.adapter.ComicsAdapter +import com.red_velvet.marvel.ui.home.adapter.EventsAdapter + +class HomeFragment : BaseFragment() { + + override val layoutIdFragment = R.layout.fragment_home + + override val viewModel: HomeViewModel by viewModels() + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val comicsAdapter = ComicsAdapter(emptyList(), viewModel) + val eventsAdapter = EventsAdapter(emptyList(), viewModel) + val charsAdapter = CharactersAdapter(emptyList(), viewModel) + + binding.apply { + recyclerComics.adapter = comicsAdapter + recyclerChars.adapter = charsAdapter + recyclerEvents.adapter = eventsAdapter + } + binding.textViewError.setOnClickListener { + viewModel.apply { + getCharacters() + getComics() + getEvents() + } + } + initNavigateToComicDetails() + } + + private fun initNavigateToComicDetails() { + viewModel.navigationToComicDetails.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled()?.let { + val directions = + HomeFragmentDirections.actionHomeFragmentToComicDetailsFragment(it) + findNavController().navigate(directions) + } + } + + viewModel.navigationToEventDetails.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled()?.let { + val directions = + HomeFragmentDirections.actionHomeFragmentToEventDetailsFragment(it) + findNavController().navigate(directions) + } + } + + viewModel.navigationToCharacterDetails.observe(viewLifecycleOwner) { event -> + event.getContentIfNotHandled()?.let { + val directions = + HomeFragmentDirections.actionHomeFragmentToCharacterFragment(it) + findNavController().navigate(directions) + } + } + } + +} \ No newline at end of file From 45ed2b45cec16f2d95468d7ae31f5eecf0133d16 Mon Sep 17 00:00:00 2001 From: Sadeq Al-Mhana <64174395+devsadeq@users.noreply.github.com> Date: Fri, 19 May 2023 01:58:23 +0300 Subject: [PATCH 4/7] fix comic item type issue --- app/src/main/res/layout/item_home_comic.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/layout/item_home_comic.xml b/app/src/main/res/layout/item_home_comic.xml index c6c50756..358934f8 100644 --- a/app/src/main/res/layout/item_home_comic.xml +++ b/app/src/main/res/layout/item_home_comic.xml @@ -7,7 +7,7 @@ + type="com.red_velvet.marvel.data.model.Comic" /> Date: Fri, 19 May 2023 17:41:52 +0300 Subject: [PATCH 5/7] init room db --- app/build.gradle.kts | 8 +++++ .../java/com/red_velvet/marvel/data/Comics.kt | 11 +++++++ .../marvel/data/database/ComicsDao.kt | 17 ++++++++++ .../marvel/data/database/ComicsDataBase.kt | 27 +++++++++++++++ .../marvel/data/repository/MovieRepository.kt | 15 +++++++++ .../red_velvet/marvel/ui/base/BaseActivity.kt | 2 ++ .../marvel/ui/comics/ComicsFragment.kt | 2 ++ .../marvel/ui/comics/ComicsViewModel.kt | 33 +++++++++++++++++++ 8 files changed, 115 insertions(+) create mode 100644 app/src/main/java/com/red_velvet/marvel/data/Comics.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/data/database/ComicsDao.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/data/database/ComicsDataBase.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7b1990b6..4f61bfc4 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -62,6 +62,7 @@ android { dependencies { val lifecycleVersion = "2.6.1" + val roomVersion = "2.5.1" implementation("androidx.core:core-ktx:1.9.0") implementation("androidx.appcompat:appcompat:1.6.1") @@ -111,4 +112,11 @@ dependencies { // Lottie Animation implementation("com.airbnb.android:lottie:6.0.0") + //rome db + implementation("androidx.room:room-runtime:$roomVersion") + annotationProcessor("androidx.room:room-compiler:$roomVersion") + kapt("androidx.room:room-compiler:$roomVersion") + + + } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/Comics.kt b/app/src/main/java/com/red_velvet/marvel/data/Comics.kt new file mode 100644 index 00000000..bc63a2df --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/data/Comics.kt @@ -0,0 +1,11 @@ +package com.red_velvet.marvel.data + +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity("COMICS_TABLE") +data class Comics( + @PrimaryKey val id :Int, + val title :String, + val img :String, +) diff --git a/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDao.kt b/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDao.kt new file mode 100644 index 00000000..5cb07495 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDao.kt @@ -0,0 +1,17 @@ +package com.red_velvet.marvel.data.database + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.Query +import com.red_velvet.marvel.data.Comics +import io.reactivex.rxjava3.core.Completable +import io.reactivex.rxjava3.core.Single + +@Dao +interface ComicsDao { + @Insert + fun insertComics(comics: Comics):Completable + + @Query("SELECT * FROM COMICS_TABLE") + fun getAllComics() :Single> +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDataBase.kt b/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDataBase.kt new file mode 100644 index 00000000..f755c68c --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/data/database/ComicsDataBase.kt @@ -0,0 +1,27 @@ +package com.red_velvet.marvel.data.database + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import com.red_velvet.marvel.data.Comics + +@Database(entities = [Comics::class] , version = 1) +abstract class ComicsDataBase :RoomDatabase() { + abstract fun comicsDao(): ComicsDao + + companion object { + @Volatile private var instance:ComicsDataBase? =null + fun getInstanceWithoutContext():ComicsDataBase { + return instance !! + } + fun getInstance(context:Context):ComicsDataBase { + return instance?: synchronized(this){buildDataBase( context).also { instance=it }} + } + const val DATABASE_NAME="marverl" + private fun buildDataBase(context:Context):ComicsDataBase{ + return Room.databaseBuilder(context,ComicsDataBase::class.java,DATABASE_NAME).build() + } + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt new file mode 100644 index 00000000..0e346413 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt @@ -0,0 +1,15 @@ +package com.red_velvet.marvel.data.repository + +import com.red_velvet.marvel.data.Comics +import com.red_velvet.marvel.data.database.ComicsDataBase +import io.reactivex.rxjava3.core.Completable + +class MovieRepository { + val dao = ComicsDataBase.getInstanceWithoutContext().comicsDao() + + fun insertData(comics:Comics): Completable { + return dao.insertComics(comics) + } + fun getComics()=dao.getAllComics() + +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/base/BaseActivity.kt b/app/src/main/java/com/red_velvet/marvel/ui/base/BaseActivity.kt index b8aec82d..94c4aa7f 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/base/BaseActivity.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/base/BaseActivity.kt @@ -5,6 +5,7 @@ import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding +import com.red_velvet.marvel.data.database.ComicsDataBase abstract class BaseActivity : AppCompatActivity() { @@ -14,6 +15,7 @@ abstract class BaseActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + ComicsDataBase.getInstance(applicationContext) binding = DataBindingUtil.setContentView(this, getLayoutResId()) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt index 602211f2..d9989aeb 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsFragment.kt @@ -5,6 +5,7 @@ import android.view.View import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController import com.red_velvet.marvel.R +import com.red_velvet.marvel.data.database.ComicsDataBase import com.red_velvet.marvel.databinding.FragmentComicsBinding import com.red_velvet.marvel.ui.base.BaseFragment @@ -14,6 +15,7 @@ class ComicsFragment : BaseFragment() { override val viewModel: ComicsViewModel by viewModels() + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val comicsAdapter = ComicsScreenAdapter(emptyList(), viewModel) diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt index e03489b1..888ce92f 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/comics/ComicsViewModel.kt @@ -4,16 +4,41 @@ import androidx.annotation.StringRes import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.red_velvet.marvel.R +import com.red_velvet.marvel.data.Comics +import com.red_velvet.marvel.data.database.ComicsDataBase import com.red_velvet.marvel.data.model.Comic import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl +import com.red_velvet.marvel.data.repository.MovieRepository import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.schedulers.Schedulers class ComicsViewModel : BaseViewModel(), ComicsInteractionListener { + val movieRepository= MovieRepository() + + fun addCashedComics(){ + movieRepository. + insertData(Comics(0,"aya","img")) + .subscribeOn(Schedulers.io()) + .subscribe() + } + fun getCashedComics(){ + + movieRepository. + getComics() + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + ::onGetComics, + ::onGetThisWeekComicsFailure, + + ) + } private val repository: MarvelRepository by lazy { MarvelRepositoryImpl(RetrofitClient.apiService) } @@ -41,6 +66,10 @@ class ComicsViewModel : BaseViewModel(), ComicsInteractionListener { val thisWeekComicsLiveData: LiveData>> = _thisWeekComics private val _nextWeekComics = MutableLiveData>>(State.Loading) + private val _comics = MutableLiveData>() + val comics : LiveData> = _comics + + private val _lastWeekComics = MutableLiveData>>(State.Loading) @@ -69,6 +98,10 @@ class ComicsViewModel : BaseViewModel(), ComicsInteractionListener { _thisWeekComics.postValue(state) if (state is State.Success) insertNewComicsCollection(thisWeekStringResource, state) } + private fun onGetComics(comics: List) { + _comics.postValue(comics) + + } fun getNextWeekComics() { bindStateUpdates( From 8de4834564d47fcd81abf3a3595be7e86dcd43d6 Mon Sep 17 00:00:00 2001 From: ay0o5h Date: Sat, 20 May 2023 02:15:55 +0300 Subject: [PATCH 6/7] inject --- app/build.gradle.kts | 5 +- .../data/entity/{Chars.kt => CharsEntity.kt} | 2 +- .../entity/{Comics.kt => ComicsEntity.kt} | 2 +- .../entity/{Events.kt => EventsEntity.kt} | 2 +- .../red_velvet/marvel/data/local/MovieDao.kt | 46 ++++++ .../marvel/data/local/MovieDataBase.kt | 45 +++--- .../marvel/data/local/dao/ComicsDao.kt | 17 -- .../marvel/data/local/dao/EventsDao.kt | 24 --- .../marvel/data/local/dao/SeriesDao.kt | 17 -- .../model/{Character.kt => CharacterDto.kt} | 2 +- .../data/model/{Comic.kt => ComicDto.kt} | 2 +- .../data/model/{Event.kt => EventDto.kt} | 2 +- .../marvel/data/remote/MarvelService.kt | 26 ++-- .../data/repository/MarvelRepository.kt | 45 ++++-- .../data/repository/MarvelRepositoryImpl.kt | 145 ++++++++++++++++-- .../marvel/data/repository/MovieRepository.kt | 14 -- .../marvel/di/MovieDataBaseModule.kt | 20 +++ .../red_velvet/marvel/di/RepositoryModule.kt | 34 ++++ .../marvel/domain/mappers/CharsMapper.kt | 15 ++ .../marvel/domain/mappers/ComicsMapper.kt | 13 ++ .../marvel/domain/mappers/EventMapper.kt | 15 ++ .../marvel/domain/mappers/Mapper.kt | 6 + .../red_velvet/marvel/domain/models/Chars.kt | 7 + .../red_velvet/marvel/domain/models/Comic.kt | 10 ++ .../red_velvet/marvel/domain/models/event.kt | 7 + .../com/red_velvet/marvel/ui/MainActivity.kt | 2 + .../red_velvet/marvel/ui/MarvelApplication.kt | 9 ++ .../marvel/ui/base/BaseViewModel.kt | 3 +- .../CharacterComicsAdapter.kt | 6 +- .../CharacterDetailsFragment.kt | 2 + .../CharacterDetailsViewModel.kt | 16 +- .../marvel/ui/characters/CharactersAdapter.kt | 6 +- .../ui/characters/CharactersFragment.kt | 2 + .../ui/characters/CharactersViewModel.kt | 16 +- .../ComicDetailsCharactersAdapter.kt | 6 +- .../ui/comicDetails/ComicDetailsFragment.kt | 2 + .../ui/comicDetails/ComicDetailsViewModel.kt | 26 ++-- .../ui/eventDetails/CharactersAdapter.kt | 6 +- .../ui/eventDetails/EventDetailViewModel.kt | 24 +-- .../ui/eventDetails/EventDetailsFragment.kt | 2 + .../marvel/ui/events/EventsAdapter.kt | 6 +- .../marvel/ui/events/EventsFragment.kt | 3 +- .../marvel/ui/events/EventsViewModel.kt | 41 ++++- .../red_velvet/marvel/ui/home/HomeFragment.kt | 2 + .../marvel/ui/home/HomeViewModel.kt | 87 ++++++----- .../ui/home/adapter/CharactersAdapter.kt | 6 +- .../marvel/ui/home/adapter/ComicsAdapter.kt | 6 +- .../marvel/ui/home/adapter/EventsAdapter.kt | 6 +- .../marvel/ui/series/SeriesFragment.kt | 2 + .../marvel/ui/series/SeriesViewModel.kt | 8 +- .../ui/seriesDetails/SeriesDetailsFragment.kt | 2 + .../seriesDetails/SeriesDetailsViewModel.kt | 7 +- .../marvel/ui/stories/StoriesFragment.kt | 2 + .../marvel/ui/stories/StoriesViewModel.kt | 7 +- .../ui/storyDetails/StoryDetailsFragment.kt | 3 +- .../ui/storyDetails/StoryDetailsViewModel.kt | 17 +- app/src/main/res/layout/characters_item.xml | 2 +- .../main/res/layout/item_character_comics.xml | 2 +- app/src/main/res/layout/item_charactors.xml | 2 +- .../main/res/layout/item_comic_character.xml | 2 +- app/src/main/res/layout/item_event.xml | 2 +- .../main/res/layout/item_home_character.xml | 2 +- app/src/main/res/layout/item_home_comic.xml | 2 +- app/src/main/res/layout/item_home_event.xml | 2 +- 64 files changed, 592 insertions(+), 280 deletions(-) rename app/src/main/java/com/red_velvet/marvel/data/entity/{Chars.kt => CharsEntity.kt} (89%) rename app/src/main/java/com/red_velvet/marvel/data/entity/{Comics.kt => ComicsEntity.kt} (89%) rename app/src/main/java/com/red_velvet/marvel/data/entity/{Events.kt => EventsEntity.kt} (89%) create mode 100644 app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/data/local/dao/ComicsDao.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/data/local/dao/EventsDao.kt delete mode 100644 app/src/main/java/com/red_velvet/marvel/data/local/dao/SeriesDao.kt rename app/src/main/java/com/red_velvet/marvel/data/model/{Character.kt => CharacterDto.kt} (97%) rename app/src/main/java/com/red_velvet/marvel/data/model/{Comic.kt => ComicDto.kt} (99%) rename app/src/main/java/com/red_velvet/marvel/data/model/{Event.kt => EventDto.kt} (98%) delete mode 100644 app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/di/MovieDataBaseModule.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/Mapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/models/Chars.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/models/Comic.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/models/event.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/ui/MarvelApplication.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4f61bfc4..6678f814 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -5,6 +5,7 @@ plugins { id("org.jetbrains.kotlin.android") id("kotlin-kapt") id("androidx.navigation.safeargs") + id("com.google.dagger.hilt.android") } android { @@ -117,6 +118,8 @@ dependencies { annotationProcessor("androidx.room:room-compiler:$roomVersion") kapt("androidx.room:room-compiler:$roomVersion") - + //dagger hilt + implementation("com.google.dagger:hilt-android:2.44") + kapt("com.google.dagger:hilt-android-compiler:2.44") } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/entity/Chars.kt b/app/src/main/java/com/red_velvet/marvel/data/entity/CharsEntity.kt similarity index 89% rename from app/src/main/java/com/red_velvet/marvel/data/entity/Chars.kt rename to app/src/main/java/com/red_velvet/marvel/data/entity/CharsEntity.kt index eafe53a0..0ceb73b8 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/entity/Chars.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/entity/CharsEntity.kt @@ -4,7 +4,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity("CHARS_TABLE") -data class Chars( +data class CharsEntity( @PrimaryKey val id :Int, val title :String, val img :String, diff --git a/app/src/main/java/com/red_velvet/marvel/data/entity/Comics.kt b/app/src/main/java/com/red_velvet/marvel/data/entity/ComicsEntity.kt similarity index 89% rename from app/src/main/java/com/red_velvet/marvel/data/entity/Comics.kt rename to app/src/main/java/com/red_velvet/marvel/data/entity/ComicsEntity.kt index ed6e972a..0d8a7ef0 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/entity/Comics.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/entity/ComicsEntity.kt @@ -4,7 +4,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity("COMICS_TABLE") -data class Comics( +data class ComicsEntity( @PrimaryKey val id :Int, val title :String, val img :String, diff --git a/app/src/main/java/com/red_velvet/marvel/data/entity/Events.kt b/app/src/main/java/com/red_velvet/marvel/data/entity/EventsEntity.kt similarity index 89% rename from app/src/main/java/com/red_velvet/marvel/data/entity/Events.kt rename to app/src/main/java/com/red_velvet/marvel/data/entity/EventsEntity.kt index bbd627a7..27353e19 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/entity/Events.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/entity/EventsEntity.kt @@ -4,7 +4,7 @@ import androidx.room.Entity import androidx.room.PrimaryKey @Entity("Events_TABLE") -data class Events( +data class EventsEntity( @PrimaryKey val id :Int, val title :String, val img :String, diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt new file mode 100644 index 00000000..dd0f61dc --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt @@ -0,0 +1,46 @@ +package com.red_velvet.marvel.data.local + +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.red_velvet.marvel.data.entity.CharsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity +import com.red_velvet.marvel.data.entity.EventsSearch +import com.red_velvet.marvel.data.entity.SeriesSearch +import io.reactivex.rxjava3.core.Completable +import io.reactivex.rxjava3.core.Observable +import io.reactivex.rxjava3.core.Single +import retrofit2.Response + +interface MovieDao { + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertChars(charsEntity: CharsEntity): Completable + + @Query("SELECT * FROM CHARS_TABLE") + fun getAllChars() : Observable> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertComics(comicsEntity: ComicsEntity):Completable + + @Query("SELECT * FROM COMICS_TABLE") + fun getAllComics() : Observable> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertEvents(eventsEntity: EventsEntity): Completable + + @Query("SELECT * FROM EVENTS_TABLE") + fun getAllEvents() : Observable> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertEventsSearch(eventsSearch: EventsSearch): Completable + + @Query("SELECT * FROM EVENTS_SEARCH_TABLE") + fun getAllEventsSearch() : Observable> + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertSeries(seriesSearch: SeriesSearch): Completable + + @Query("SELECT * FROM SERIES_SEARCH_TABLE") + fun getAllSeries() : Observable> +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/MovieDataBase.kt b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDataBase.kt index 1e82c9d8..42d3b27a 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/local/MovieDataBase.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDataBase.kt @@ -1,39 +1,40 @@ package com.red_velvet.marvel.data.local - import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase -import com.red_velvet.marvel.data.entity.Comics -import com.red_velvet.marvel.data.entity.Events +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity import com.red_velvet.marvel.data.entity.EventsSearch import com.red_velvet.marvel.data.entity.SeriesSearch -import com.red_velvet.marvel.data.local.dao.ComicsDao -import com.red_velvet.marvel.data.local.dao.EventsDao -import com.red_velvet.marvel.data.local.dao.SeriesDao +import javax.inject.Inject +import javax.inject.Singleton @Database(entities = [ SeriesSearch::class, - Events::class, - EventsSearch::class - , Comics::class] , version = 1) -abstract class MovieDataBase : RoomDatabase() { - abstract fun seriesDao(): SeriesDao - abstract fun eventsDao(): EventsDao - abstract fun comicsDao(): ComicsDao + EventsEntity::class, + EventsSearch::class, + Char::class + , ComicsEntity::class] , version = 1) +abstract class MovieDataBase : RoomDatabase() { + + abstract fun movieDao(): MovieDao + companion object { - @Volatile private var instance: MovieDataBase? =null - fun getInstanceWithoutContext(): MovieDataBase { - return instance !! - } + + @Volatile + private var instance: MovieDataBase? = null + + @Singleton + @Inject fun getInstance(context: Context): MovieDataBase { - return instance ?: synchronized(this){ buildDataBase( context).also { instance =it }} - } - const val DATABASE_NAME="marverl" - private fun buildDataBase(context: Context): MovieDataBase { - return Room.databaseBuilder(context, MovieDataBase::class.java, DATABASE_NAME).build() + return instance ?: synchronized(this) { + Room.databaseBuilder(context, MovieDataBase::class.java, DATABASE_NAME).build() + .also { instance = it } + } } + const val DATABASE_NAME = "marvel" } } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/dao/ComicsDao.kt b/app/src/main/java/com/red_velvet/marvel/data/local/dao/ComicsDao.kt deleted file mode 100644 index d9848bf0..00000000 --- a/app/src/main/java/com/red_velvet/marvel/data/local/dao/ComicsDao.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.red_velvet.marvel.data.local.dao - -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.Query -import com.red_velvet.marvel.data.entity.Comics -import io.reactivex.rxjava3.core.Completable -import io.reactivex.rxjava3.core.Single - -@Dao -interface ComicsDao { - @Insert - fun insertComics(comics: Comics):Completable - - @Query("SELECT * FROM COMICS_TABLE") - fun getAllComics() :Single> -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/dao/EventsDao.kt b/app/src/main/java/com/red_velvet/marvel/data/local/dao/EventsDao.kt deleted file mode 100644 index c24a1ac3..00000000 --- a/app/src/main/java/com/red_velvet/marvel/data/local/dao/EventsDao.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.red_velvet.marvel.data.local.dao - -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.Query -import com.red_velvet.marvel.data.entity.Events -import com.red_velvet.marvel.data.entity.EventsSearch -import io.reactivex.rxjava3.core.Completable -import io.reactivex.rxjava3.core.Single - -@Dao -interface EventsDao { - @Insert - fun insertEvents(events: Events): Completable - - @Query("SELECT * FROM EVENTS_TABLE") - fun getAllEvents() : Single> - - @Insert - fun insertEventsSearch(eventsSearch: EventsSearch): Completable - - @Query("SELECT * FROM EVENTS_SEARCH_TABLE") - fun getAllEventsSearch() : Single> -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/dao/SeriesDao.kt b/app/src/main/java/com/red_velvet/marvel/data/local/dao/SeriesDao.kt deleted file mode 100644 index 72234123..00000000 --- a/app/src/main/java/com/red_velvet/marvel/data/local/dao/SeriesDao.kt +++ /dev/null @@ -1,17 +0,0 @@ -package com.red_velvet.marvel.data.local.dao - -import androidx.room.Insert -import androidx.room.Query -import com.red_velvet.marvel.data.entity.Events -import com.red_velvet.marvel.data.entity.SeriesSearch -import io.reactivex.rxjava3.core.Completable -import io.reactivex.rxjava3.core.Single - -interface SeriesDao { - - @Insert - fun insertSeries(seriesSearch: SeriesSearch): Completable - - @Query("SELECT * FROM SERIES_SEARCH_TABLE") - fun getAllChars() : Single> -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/model/Character.kt b/app/src/main/java/com/red_velvet/marvel/data/model/CharacterDto.kt similarity index 97% rename from app/src/main/java/com/red_velvet/marvel/data/model/Character.kt rename to app/src/main/java/com/red_velvet/marvel/data/model/CharacterDto.kt index 5f521352..ac3c70a0 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/model/Character.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/model/CharacterDto.kt @@ -3,7 +3,7 @@ package com.red_velvet.marvel.data.model import com.google.gson.annotations.SerializedName import com.red_velvet.marvel.data.model.* -data class Character( +data class CharacterDto( @SerializedName("comics") val comics: ResourceCollection? = ResourceCollection(), @SerializedName("description") diff --git a/app/src/main/java/com/red_velvet/marvel/data/model/Comic.kt b/app/src/main/java/com/red_velvet/marvel/data/model/ComicDto.kt similarity index 99% rename from app/src/main/java/com/red_velvet/marvel/data/model/Comic.kt rename to app/src/main/java/com/red_velvet/marvel/data/model/ComicDto.kt index 64f78ac3..7062bfb9 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/model/Comic.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/model/ComicDto.kt @@ -3,7 +3,7 @@ package com.red_velvet.marvel.data.model import com.google.gson.annotations.SerializedName -data class Comic( +data class ComicDto( @SerializedName("characters") val characters: ResourceCollection? = ResourceCollection(), @SerializedName("collectedIssues") diff --git a/app/src/main/java/com/red_velvet/marvel/data/model/Event.kt b/app/src/main/java/com/red_velvet/marvel/data/model/EventDto.kt similarity index 98% rename from app/src/main/java/com/red_velvet/marvel/data/model/Event.kt rename to app/src/main/java/com/red_velvet/marvel/data/model/EventDto.kt index fb4b08a9..133eb380 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/model/Event.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/model/EventDto.kt @@ -3,7 +3,7 @@ package com.red_velvet.marvel.data.model import com.google.gson.annotations.SerializedName -data class Event( +data class EventDto( @SerializedName("id") val id: Int? = 0, @SerializedName("title") diff --git a/app/src/main/java/com/red_velvet/marvel/data/remote/MarvelService.kt b/app/src/main/java/com/red_velvet/marvel/data/remote/MarvelService.kt index b2d45876..3309252b 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/remote/MarvelService.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/remote/MarvelService.kt @@ -1,10 +1,10 @@ package com.red_velvet.marvel.data.remote import com.red_velvet.marvel.data.model.BaseResponse -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Creator -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.model.Story import io.reactivex.rxjava3.core.Single @@ -19,19 +19,19 @@ interface MarvelService { fun getAllComics( @Query("titleStartsWith") titleStartsWith: String? = null, @Query("dateDescriptor") dateDescriptor: String? = null, - ): Single>>> + ): Single>>> @GET("comics/{comicId}") fun getComicDetailById( @Path("comicId") comicId: Int - ): Single>>> + ): Single>>> @GET("characters/{characterId}/comics") fun getComicsByCharacterId( @Path("characterId") characterId: Int, @Query("titleStartsWith") titleStartsWith: String? = null, @Query("dateDescriptor") dateDescriptor: String? = null - ): Single>>> + ): Single>>> @GET("comics/{comicId}/creators") fun getCreatorByComicId( @@ -52,17 +52,17 @@ interface MarvelService { @GET("events") fun getAllEvents( @Query("nameStartsWith") nameStartsWith: String? = null - ): Single>>> + ): Single>>> @GET("comics/{comicId}/characters") fun getCharactersByComicId( @Path("comicId") comicId: Int? = null, - ): Single>>> + ): Single>>> @GET("events/{eventId}/characters") fun getCharactersByEventId( @Path("eventId") eventId: Int, - ): Single>>> + ): Single>>> @GET("events/{eventId}/creators") fun getCreatorsByEventId( @@ -85,17 +85,17 @@ interface MarvelService { @GET("stories/{storyId}/comics") fun getComicsByStoryId( @Path("storyId") storyId: Int - ): Single>>> + ): Single>>> @GET("characters") fun getAllCharacters( @Query("nameStartsWith") nameStartsWith: String? = null - ): Single>>> + ): Single>>> @GET("characters/{characterId}") fun getCharacterById( @Path("characterId") characterId: Int - ): Single>>> + ): Single>>> @GET("characters/{characterId}/series") fun getSeriesByCharacterId( @@ -110,5 +110,5 @@ interface MarvelService { @GET("events/{eventId}") fun getEventById( @Path("eventId") eventId: Int - ): Single>>> + ): Single>>> } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt index 247f0d0b..a45420e4 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt @@ -1,10 +1,15 @@ package com.red_velvet.marvel.data.repository -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.entity.CharsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity +import com.red_velvet.marvel.data.entity.EventsSearch +import com.red_velvet.marvel.data.entity.SeriesSearch +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Creator -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.model.Story import com.red_velvet.marvel.ui.utils.State @@ -12,14 +17,15 @@ import io.reactivex.rxjava3.core.Observable interface MarvelRepository { + fun getAllComics( titleStartsWith: String? = null, dateDescriptor: String? = null, - ): Observable>> + ): Observable>> - fun getComicById(comicId: Int): Observable>> + fun getComicById(comicId: Int): Observable>> - fun getComicsByCharacterId(characterId: Int): Observable>> + fun getComicsByCharacterId(characterId: Int): Observable>> fun getAllSeries( titleStartsWith: String? = null, @@ -30,9 +36,9 @@ interface MarvelRepository { fun getSeriesById(seriesId: Int): Observable>> - fun getAllEvents(query: String? = null): Observable>> + fun getAllEvents(query: String? = null): Observable>> - fun getCharactersByEventId(eventId: Int): Observable>> + fun getCharactersByEventId(eventId: Int): Observable>> fun getCreatorsByEventId(eventId: Int): Observable>> @@ -42,18 +48,31 @@ interface MarvelRepository { fun getCreatorsByStoryId(storyId: Int): Observable>> - fun getComicsByStoryId(storyId: Int): Observable>> + fun getComicsByStoryId(storyId: Int): Observable>> - fun getCharactersByComicId(comicId: Int): Observable>> + fun getCharactersByComicId(comicId: Int): Observable>> - fun getEventById(eventId: Int): Observable>> + fun getEventById(eventId: Int): Observable>> - fun getAllCharacters(nameStartsWith: String? = null): Observable>> + fun getAllCharacters(nameStartsWith: String? = null): Observable>> - fun getCharacterById(characterId: Int): Observable>> + fun getCharacterById(characterId: Int): Observable>> fun getCreatorsBySeriesId(seriesId: Int): Observable>> fun getSeriesByCharacterId(characterId: Int): Observable>> + + fun refreshComics() + fun refreshCharacters() + fun refreshEvents() + fun refreshEventsSearch() + fun refreshSeries() + + fun getSeries(): Observable> + fun getEventsSearch(): Observable> + + fun getAllComics(): Observable> + fun getAllEvents(): Observable> + fun getAllCharacters(): Observable> } diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt index 2ee67b24..2693b96d 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt @@ -1,35 +1,54 @@ package com.red_velvet.marvel.data.repository +import com.red_velvet.marvel.data.entity.CharsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity +import com.red_velvet.marvel.data.entity.EventsSearch +import com.red_velvet.marvel.data.entity.SeriesSearch +import com.red_velvet.marvel.data.local.MovieDataBase import com.red_velvet.marvel.data.model.BaseResponse -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Creator -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.model.Story +import com.red_velvet.marvel.domain.mappers.ComicsMapper import com.red_velvet.marvel.data.remote.MarvelService +import com.red_velvet.marvel.domain.mappers.CharsMapper +import com.red_velvet.marvel.domain.mappers.EventMapper import com.red_velvet.marvel.ui.utils.State import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Single import retrofit2.Response +import javax.inject.Inject + +class MarvelRepositoryImpl @Inject constructor( + private val marvelServiceImpl: MarvelService, + private val daoMovie : MovieDataBase, + private val comicsMapper: ComicsMapper, + private val eventMapper: EventMapper, + private val charsMapper: CharsMapper -class MarvelRepositoryImpl( - private val marvelServiceImpl: MarvelService ) : MarvelRepository { override fun getAllComics( titleStartsWith: String?, dateDescriptor: String? - ): Observable>> { + ): Observable>> { return wrapWithState { marvelServiceImpl.getAllComics(titleStartsWith, dateDescriptor) } } - override fun getComicById(comicId: Int): Observable>> { + override fun getAllComics(): Observable> { + return daoMovie.movieDao().getAllComics() + } + + override fun getComicById(comicId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getComicDetailById(comicId) } } - override fun getComicsByCharacterId(characterId: Int): Observable>> { + override fun getComicsByCharacterId(characterId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getComicsByCharacterId(characterId) } } @@ -40,7 +59,7 @@ class MarvelRepositoryImpl( return wrapWithState { marvelServiceImpl.getAllSeries(titleStartsWith, contains) } } - override fun getCharactersByComicId(comicId: Int): Observable>> { + override fun getCharactersByComicId(comicId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getCharactersByComicId(comicId) } } @@ -48,23 +67,31 @@ class MarvelRepositoryImpl( return wrapWithState { marvelServiceImpl.getSeriesById(seriesId) } } - override fun getAllEvents(query: String?): Observable>> { + override fun getAllEvents(query: String?): Observable>> { return wrapWithState { marvelServiceImpl.getAllEvents(query) } } + override fun getAllEvents(): Observable> { + return daoMovie.movieDao().getAllEvents() + } + override fun getCreatorByComicId(comicId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getCreatorByComicId(comicId) } } - override fun getCharactersByEventId(eventId: Int): Observable>> { + override fun getCharactersByEventId(eventId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getCharactersByEventId(eventId) } } - override fun getAllCharacters(nameStartsWith: String?): Observable>> { + override fun getAllCharacters(nameStartsWith: String?): Observable>> { return wrapWithState { marvelServiceImpl.getAllCharacters(nameStartsWith) } } - override fun getCharacterById(characterId: Int): Observable>> { + override fun getAllCharacters(): Observable> { + return daoMovie.movieDao().getAllChars() + } + + override fun getCharacterById(characterId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getCharacterById(characterId) } } @@ -84,7 +111,7 @@ class MarvelRepositoryImpl( return wrapWithState { marvelServiceImpl.getCreatorsByStoryId(storyId) } } - override fun getComicsByStoryId(storyId: Int): Observable>> { + override fun getComicsByStoryId(storyId: Int): Observable>> { return wrapWithState { marvelServiceImpl.getComicsByStoryId(storyId) } } @@ -94,9 +121,97 @@ class MarvelRepositoryImpl( return wrapWithState { marvelServiceImpl.getSeriesByCharacterId(characterId) } } + override fun refreshComics() { + marvelServiceImpl.getAllComics().map { response -> + if (response.isSuccessful) + response.body()?.body?.results?.map { comic -> + daoMovie.movieDao().insertComics( + ComicsEntity( + comic.id!!, comic.title.toString(), + "${comic.thumbnail?.path}.${comic.thumbnail?.extension}" + ) + ) + + } + } + } + + + + override fun refreshEvents() { + marvelServiceImpl.getAllEvents().map { response -> + if (response.isSuccessful) + response.body()?.body?.results?.map { event -> + daoMovie.movieDao().insertEvents( + EventsEntity( + event.id!!, event.title.toString(), + "${event.thumbnail?.path}.${event.thumbnail?.extension}" + ) + ) + + } + } + } + + override fun refreshEventsSearch() { + marvelServiceImpl.getAllEvents().map { response -> + if (response.isSuccessful) + response.body()?.body?.results?.map { event -> + daoMovie.movieDao().insertEventsSearch( + EventsSearch( + event.id!!, event.title.toString(), + "${event.thumbnail?.path}.${event.thumbnail?.extension}" + ) + ) + + } + } + } + + override fun refreshSeries() { + marvelServiceImpl.getAllSeries().map { response -> + if (response.isSuccessful) + response.body()?.body?.results?.map { series -> + daoMovie.movieDao().insertSeries( + SeriesSearch( + series.id!!, series.title.toString(), + "${series.thumbnail?.path}.${series.thumbnail?.extension}" + ) + ) + + } + } + } + + override fun getSeries(): Observable> { + return daoMovie.movieDao().getAllSeries() + } + + override fun getEventsSearch(): Observable> { + return daoMovie.movieDao().getAllEventsSearch() + } + + + + override fun refreshCharacters(){ + marvelServiceImpl.getAllCharacters().map { response -> + if (response.isSuccessful) + response.body()?.body?.results?.map { character -> + daoMovie.movieDao().insertChars( + CharsEntity( + character.id!!, character.name.toString(), + "${character.thumbnail?.path}.${character.thumbnail?.extension}" + ) + ) + + } + } + } + + override fun getEventById( eventId: Int - ): Observable>> { + ): Observable>> { return wrapWithState { marvelServiceImpl.getEventById(eventId) } } diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt deleted file mode 100644 index 576ceab0..00000000 --- a/app/src/main/java/com/red_velvet/marvel/data/repository/MovieRepository.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.red_velvet.marvel.data.repository - -import com.red_velvet.marvel.data.entity.Comics -import io.reactivex.rxjava3.core.Completable - -class MovieRepository { - val dao = ComicsDataBase.getInstanceWithoutContext().comicsDao() - - fun insertData(comics: Comics): Completable { - return dao.insertComics(comics) - } - fun getComics()=dao.getAllComics() - -} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/di/MovieDataBaseModule.kt b/app/src/main/java/com/red_velvet/marvel/di/MovieDataBaseModule.kt new file mode 100644 index 00000000..1d85ea9f --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/di/MovieDataBaseModule.kt @@ -0,0 +1,20 @@ +package com.red_velvet.marvel.di + +import android.content.Context +import com.red_velvet.marvel.data.local.MovieDataBase +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +object MovieDataBaseModule { + + @Provides + @Singleton + fun provideMovieDataBase(context: Context): MovieDataBase { + return MovieDataBase.getInstance(context) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt b/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt new file mode 100644 index 00000000..58ebfb58 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt @@ -0,0 +1,34 @@ +package com.red_velvet.marvel.di + +import com.red_velvet.marvel.data.local.MovieDataBase +import com.red_velvet.marvel.data.remote.MarvelService +import com.red_velvet.marvel.data.repository.MarvelRepository +import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl +import com.red_velvet.marvel.domain.mappers.CharsMapper +import com.red_velvet.marvel.domain.mappers.ComicsMapper +import com.red_velvet.marvel.domain.mappers.EventMapper +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Module +@InstallIn(SingletonComponent::class) +class RepositoryModule { + @Provides + fun provideMarvelRepository( + marvelServiceImpl: MarvelService, + daoMovie: MovieDataBase, + comicsMapper: ComicsMapper, + eventMapper: EventMapper, + charsMapper: CharsMapper + ): MarvelRepository { + return MarvelRepositoryImpl( + marvelServiceImpl, + daoMovie, + comicsMapper, + eventMapper, + charsMapper + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt new file mode 100644 index 00000000..e154c74c --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt @@ -0,0 +1,15 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto +import com.red_velvet.marvel.domain.models.Chars +import com.red_velvet.marvel.domain.models.Comic +import javax.inject.Inject + +class CharsMapper @Inject constructor(): Mapper { + override fun map(input: CharacterDto): Chars { + return Chars( + id =input.id, title = input.name, + image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt new file mode 100644 index 00000000..b4359054 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt @@ -0,0 +1,13 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.model.ComicDto +import com.red_velvet.marvel.domain.models.Comic +import javax.inject.Inject + +class ComicsMapper @Inject constructor() : Mapper { + override fun map(input: ComicDto): Comic { + return Comic( + id =input.id, title = input.title, + image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + } +} diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt new file mode 100644 index 00000000..be387d65 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt @@ -0,0 +1,15 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.model.ComicDto +import com.red_velvet.marvel.data.model.EventDto +import com.red_velvet.marvel.domain.models.Comic +import com.red_velvet.marvel.domain.models.Event +import javax.inject.Inject + +class EventMapper @Inject constructor() : Mapper { + override fun map(input: EventDto): Event { + return Event( + id =input.id, title = input.title, + image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/Mapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/Mapper.kt new file mode 100644 index 00000000..c69ae112 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/Mapper.kt @@ -0,0 +1,6 @@ +package com.red_velvet.marvel.domain.mappers + +interface Mapper { + + fun map(input:I):O +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/models/Chars.kt b/app/src/main/java/com/red_velvet/marvel/domain/models/Chars.kt new file mode 100644 index 00000000..37bc5ee0 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/models/Chars.kt @@ -0,0 +1,7 @@ +package com.red_velvet.marvel.domain.models + +data class Chars( + val id: Int? = 0, + val image: String? = "", + val title: String? = "", +) diff --git a/app/src/main/java/com/red_velvet/marvel/domain/models/Comic.kt b/app/src/main/java/com/red_velvet/marvel/domain/models/Comic.kt new file mode 100644 index 00000000..464af0e5 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/models/Comic.kt @@ -0,0 +1,10 @@ +package com.red_velvet.marvel.domain.models + + +data class Comic ( + + val id: Int? = 0, + val image: String? = "", + val title: String? = "", + +) \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/models/event.kt b/app/src/main/java/com/red_velvet/marvel/domain/models/event.kt new file mode 100644 index 00000000..693c7caf --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/models/event.kt @@ -0,0 +1,7 @@ +package com.red_velvet.marvel.domain.models + +data class Event( + val id: Int? = 0, + val image: String? = "", + val title: String? = "", +) diff --git a/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt b/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt index 63a8f0cf..f2aa3594 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/MainActivity.kt @@ -10,7 +10,9 @@ import com.red_velvet.marvel.databinding.ActivityMainBinding import com.red_velvet.marvel.ui.base.BaseActivity import com.red_velvet.marvel.ui.utils.hideView import com.red_velvet.marvel.ui.utils.showView +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class MainActivity : BaseActivity() { override val LOG_TAG: String = "MainActivity" diff --git a/app/src/main/java/com/red_velvet/marvel/ui/MarvelApplication.kt b/app/src/main/java/com/red_velvet/marvel/ui/MarvelApplication.kt new file mode 100644 index 00000000..005b4ff2 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/ui/MarvelApplication.kt @@ -0,0 +1,9 @@ +package com.red_velvet.marvel.ui + +import android.app.Application +import dagger.hilt.android.HiltAndroidApp + + +@HiltAndroidApp +class MarvelApplication : Application(){ +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/base/BaseViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/base/BaseViewModel.kt index 4ba83050..64cd480d 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/base/BaseViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/base/BaseViewModel.kt @@ -2,6 +2,7 @@ package com.red_velvet.marvel.ui.base import androidx.lifecycle.ViewModel import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.disposables.CompositeDisposable @@ -9,7 +10,7 @@ import io.reactivex.rxjava3.kotlin.addTo import io.reactivex.rxjava3.kotlin.subscribeBy import io.reactivex.rxjava3.schedulers.Schedulers -abstract class BaseViewModel : ViewModel() { +abstract class BaseViewModel : ViewModel() { val compositeDisposable = CompositeDisposable() diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterComicsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterComicsAdapter.kt index 4f371cbc..12ea8bbe 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterComicsAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterComicsAdapter.kt @@ -1,12 +1,12 @@ package com.red_velvet.marvel.ui.characterDetails import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.base.BaseInteractionListener -class ComicsByCharacterAdapter(items: List, listener: SeriesInteractionListener) : - BaseAdapter(items, listener) { +class ComicsByCharacterAdapter(items: List, listener: SeriesInteractionListener) : + BaseAdapter(items, listener) { override val layoutId: Int = R.layout.item_character_comics } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsFragment.kt index 617a9cc9..db304d0d 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsFragment.kt @@ -8,7 +8,9 @@ import androidx.navigation.fragment.navArgs import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentCharacterBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class CharacterDetailsFragment : BaseFragment() { diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsViewModel.kt index ec81fa9b..01a7dacc 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characterDetails/CharacterDetailsViewModel.kt @@ -2,8 +2,8 @@ package com.red_velvet.marvel.ui.characterDetails import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository @@ -19,11 +19,11 @@ class CharacterDetailsViewModel : BaseViewModel(), SeriesInteractionListener, MarvelRepositoryImpl(RetrofitClient.apiService) } - private val _characterDetails: MutableLiveData>> = MutableLiveData() - val characterDetails: LiveData>> = _characterDetails + private val _characterDetails: MutableLiveData>> = MutableLiveData() + val characterDetails: LiveData>> = _characterDetails - private val _comics: MutableLiveData>> = MutableLiveData() - val comics: LiveData>> = _comics + private val _comics: MutableLiveData>> = MutableLiveData() + val comics: LiveData>> = _comics private val _series: MutableLiveData>> = MutableLiveData() val series: LiveData>> = _series @@ -48,7 +48,7 @@ class CharacterDetailsViewModel : BaseViewModel(), SeriesInteractionListener, ) } - private fun onGetCharacterState(state: State>) { + private fun onGetCharacterState(state: State>) { _characterDetails.postValue(state) } @@ -64,7 +64,7 @@ class CharacterDetailsViewModel : BaseViewModel(), SeriesInteractionListener, ) } - private fun onGetComicsByCharacterIdState(state: State>) { + private fun onGetComicsByCharacterIdState(state: State>) { _comics.postValue(state) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersAdapter.kt index d073e739..b6f5c41c 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersAdapter.kt @@ -1,14 +1,14 @@ package com.red_velvet.marvel.ui.characters import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.base.BaseInteractionListener class CharactersAdapter( - items: List, + items: List, listener: CharacterDetailsInteractionListener -) : BaseAdapter(items, listener) { +) : BaseAdapter(items, listener) { override val layoutId: Int = R.layout.characters_item } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersFragment.kt index 74632d43..74b98189 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersFragment.kt @@ -7,7 +7,9 @@ import androidx.navigation.fragment.findNavController import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentCharactersBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class CharactersFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_characters diff --git a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersViewModel.kt index 57c7bc46..8f51201e 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/characters/CharactersViewModel.kt @@ -2,26 +2,26 @@ package com.red_velvet.marvel.ui.characters import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.kotlin.addTo import java.util.concurrent.TimeUnit +import javax.inject.Inject -class CharactersViewModel : BaseViewModel(), CharacterDetailsInteractionListener { - private val _characters: MutableLiveData>> = MutableLiveData() - val characters: LiveData>> = _characters +@HiltViewModel +class CharactersViewModel @Inject constructor ( private val repository: MarvelRepository) : BaseViewModel(), CharacterDetailsInteractionListener { + private val _characters: MutableLiveData>> = MutableLiveData() + val characters: LiveData>> = _characters val searchQuery = MutableLiveData() - private val repository: MarvelRepository by lazy { - MarvelRepositoryImpl(RetrofitClient.apiService) - } private val _navigationToCharacterDetails: MutableLiveData> = MutableLiveData() val navigationToCharacterDetails: LiveData> = _navigationToCharacterDetails @@ -55,7 +55,7 @@ class CharactersViewModel : BaseViewModel(), CharacterDetailsInteractionListener ) } - private fun onGetCharactersState(state: State>) { + private fun onGetCharactersState(state: State>) { _characters.postValue(state) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsCharactersAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsCharactersAdapter.kt index 6e1c45f2..0d54d8f2 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsCharactersAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsCharactersAdapter.kt @@ -1,14 +1,14 @@ package com.red_velvet.marvel.ui.comicDetails import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.base.BaseInteractionListener class ComicDetailsCharactersAdapter( - items: List, + items: List, listener: ComicDetailsCharacterListenerInteraction, -) : BaseAdapter(items, listener) { +) : BaseAdapter(items, listener) { override val layoutId: Int = R.layout.item_comic_character } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsFragment.kt index 37d7fb5c..a9276055 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsFragment.kt @@ -8,7 +8,9 @@ import androidx.navigation.fragment.navArgs import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentComicDetailsBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class ComicDetailsFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_comic_details diff --git a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsViewModel.kt index 70e2e82e..a22a40c4 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/comicDetails/ComicDetailsViewModel.kt @@ -2,8 +2,8 @@ package com.red_velvet.marvel.ui.comicDetails import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Creator import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository @@ -11,22 +11,22 @@ import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State - -class ComicDetailsViewModel : BaseViewModel(), ComicDetailsCreatorListenerInteraction, +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject +@HiltViewModel +class ComicDetailsViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), ComicDetailsCreatorListenerInteraction, ComicDetailsCharacterListenerInteraction { - private val _comicsDetails: MutableLiveData>> = MutableLiveData() - val comicsDetails: LiveData>> = _comicsDetails + private val _comicsDetails: MutableLiveData>> = MutableLiveData() + val comicsDetails: LiveData>> = _comicsDetails private val _creators: MutableLiveData>> = MutableLiveData() val creators: LiveData>> = _creators - private val _characters: MutableLiveData>> = MutableLiveData() - val characters: LiveData>> = _characters + private val _characters: MutableLiveData>> = MutableLiveData() + val characters: LiveData>> = _characters + - private val repository: MarvelRepository by lazy { - MarvelRepositoryImpl(RetrofitClient.apiService) - } private val _navigationToCharacterDetails: MutableLiveData> = MutableLiveData() val navigationToCharacterDetails: LiveData> = _navigationToCharacterDetails @@ -62,7 +62,7 @@ class ComicDetailsViewModel : BaseViewModel(), ComicDetailsCreatorListenerIntera ) } - private fun onGetComicState(state: State>) { + private fun onGetComicState(state: State>) { _comicsDetails.postValue(state) } @@ -78,7 +78,7 @@ class ComicDetailsViewModel : BaseViewModel(), ComicDetailsCreatorListenerIntera _creators.postValue(State.Failed(e.message.toString())) } - private fun onGetCharactersByComicIdState(state: State>) { + private fun onGetCharactersByComicIdState(state: State>) { _characters.postValue(state) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/CharactersAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/CharactersAdapter.kt index 99c6bab4..7439d789 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/CharactersAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/CharactersAdapter.kt @@ -1,12 +1,12 @@ package com.red_velvet.marvel.ui.eventDetails import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.base.BaseInteractionListener -class CharactersAdapter(items: List, listener: CharactersInteractionListener) : - BaseAdapter(items, listener) { +class CharactersAdapter(items: List, listener: CharactersInteractionListener) : + BaseAdapter(items, listener) { override val layoutId: Int = R.layout.item_charactors } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailViewModel.kt index 4682f95f..2dc11846 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailViewModel.kt @@ -2,25 +2,29 @@ package com.red_velvet.marvel.ui.eventDetails import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.data.model.Creator -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.remote.RetrofitClient +import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject -class EventDetailViewModel : BaseViewModel(), CharactersInteractionListener, +@HiltViewModel +class EventDetailViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), CharactersInteractionListener, CreatorsInteractionListener { - private val repository by lazy { MarvelRepositoryImpl(RetrofitClient.apiService) } - private var _event: MutableLiveData>> = MutableLiveData() - val event: MutableLiveData>> = _event - private var _characters: MutableLiveData>> = MutableLiveData() - val characters: MutableLiveData>> = _characters + private var _event: MutableLiveData>> = MutableLiveData() + val event: MutableLiveData>> = _event + + private var _characters: MutableLiveData>> = MutableLiveData() + val characters: MutableLiveData>> = _characters private var _creators: MutableLiveData>> = MutableLiveData() val creators: MutableLiveData>> = _creators @@ -58,7 +62,7 @@ class EventDetailViewModel : BaseViewModel(), CharactersInteractionListener, ) } - private fun onGetEventState(state: State>) { + private fun onGetEventState(state: State>) { _event.postValue(state) } @@ -74,7 +78,7 @@ class EventDetailViewModel : BaseViewModel(), CharactersInteractionListener, _creators.postValue(State.Failed(e.message.toString())) } - private fun onGetEventCharactersState(state: State>) { + private fun onGetEventCharactersState(state: State>) { _characters.postValue(state) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailsFragment.kt index d5f741c7..ac3aed79 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/eventDetails/EventDetailsFragment.kt @@ -9,7 +9,9 @@ import androidx.navigation.fragment.navArgs import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentEventDetailsBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class EventDetailsFragment : BaseFragment() { diff --git a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsAdapter.kt index 5c8a9b45..8c588469 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsAdapter.kt @@ -1,12 +1,12 @@ package com.red_velvet.marvel.ui.events import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.base.BaseInteractionListener -class EventsAdapter(items: List, listener: EventsInteractionListener) : - BaseAdapter(items, listener) { +class EventsAdapter(items: List, listener: EventsInteractionListener) : + BaseAdapter(items, listener) { override val layoutId: Int = R.layout.item_event } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsFragment.kt index f7415ee9..bec41fdc 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsFragment.kt @@ -8,8 +8,9 @@ import androidx.navigation.fragment.findNavController import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentEventsBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint - +@AndroidEntryPoint class EventsFragment : BaseFragment() { override val layoutIdFragment: Int = R.layout.fragment_events diff --git a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsViewModel.kt index 11d3ce47..38f3508c 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/events/EventsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/events/EventsViewModel.kt @@ -2,32 +2,56 @@ package com.red_velvet.marvel.ui.events import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.entity.EventsSearch +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.remote.RetrofitClient +import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.kotlin.addTo +import io.reactivex.rxjava3.schedulers.Schedulers import java.util.concurrent.TimeUnit +import javax.inject.Inject -class EventsViewModel : BaseViewModel(), EventsInteractionListener { - private val repository by lazy { MarvelRepositoryImpl(RetrofitClient.apiService) } +@HiltViewModel +class EventsViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), EventsInteractionListener { - private val _events = MutableLiveData>>() - val events: LiveData>> = _events + + private val _events = MutableLiveData>>() + val events: LiveData>> = _events private val _navigationToEventDetails = MutableLiveData>() val navigationToEventDetails: LiveData> = _navigationToEventDetails val searchQuery = MutableLiveData() + private val _eventsSearch = MutableLiveData>() + val eventsSearch: LiveData> = _eventsSearch + init { getAllEvents() initSearchObservable() } + fun getHistorySearch(){ + + repository.getEventsSearch().subscribeOn(Schedulers.io()) .observeOn( + AndroidSchedulers + .mainThread() + ) + .subscribe( +::onGetHistory + ) + } + fun onGetHistory(eventsSearch:List){ + _eventsSearch.postValue(eventsSearch) + } + fun getAllEvents(query: String? = null) { bindStateUpdates( @@ -37,7 +61,7 @@ class EventsViewModel : BaseViewModel(), EventsInteractionListener { ) } - private fun onGetAllEventsState(state: State>) { + private fun onGetAllEventsState(state: State>) { _events.postValue(state) } @@ -52,11 +76,14 @@ class EventsViewModel : BaseViewModel(), EventsInteractionListener { } }.debounce(300, TimeUnit.MILLISECONDS) .distinctUntilChanged() + .subscribe { query -> if (query.isEmpty()) { getAllEvents() } else { - getAllEvents(query) + repository.getCashedEvents() + .switchIfEmpty(getAllEvents(query)) + getAllEvents(query) ?:getHistorySearch() } }.addTo(compositeDisposable) } diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt index ef9a287e..1b8472d5 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeFragment.kt @@ -10,7 +10,9 @@ import com.red_velvet.marvel.ui.base.BaseFragment import com.red_velvet.marvel.ui.home.adapter.CharactersAdapter import com.red_velvet.marvel.ui.home.adapter.ComicsAdapter import com.red_velvet.marvel.ui.home.adapter.EventsAdapter +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class HomeFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_home diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt index a009af1b..ce97b6cc 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt @@ -2,21 +2,22 @@ package com.red_velvet.marvel.ui.home import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Character -import com.red_velvet.marvel.data.model.Comic -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.entity.CharsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent -import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers +import io.reactivex.rxjava3.schedulers.Schedulers +import javax.inject.Inject -class HomeViewModel : BaseViewModel(), HomeInteractionListener { +@HiltViewModel +class HomeViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), HomeInteractionListener { - private val repository: MarvelRepository by lazy { - MarvelRepositoryImpl(RetrofitClient.apiService) - } private val _navigationToComicDetails: MutableLiveData> = MutableLiveData() val navigationToComicDetails: LiveData> = _navigationToComicDetails @@ -27,15 +28,15 @@ class HomeViewModel : BaseViewModel(), HomeInteractionListener { private val _navigationToCharacterDetails: MutableLiveData> = MutableLiveData() val navigationToCharacterDetails: LiveData> = _navigationToCharacterDetails - private val _comics = MutableLiveData>>(State.Loading) - val comicsLiveData: LiveData>> = _comics - private val _events = MutableLiveData>>(State.Loading) - val eventLiveData: LiveData>> = _events + private val _events = MutableLiveData>() + val eventLiveData: LiveData> = _events - private val _characters = MutableLiveData>>(State.Loading) - val characterLiveData: LiveData>> = _characters + private val _characters = MutableLiveData>() + val characterLiveData: LiveData> = _characters + private val _comics = MutableLiveData>() + val comics: LiveData> get() = _comics init { getComics() @@ -43,52 +44,54 @@ class HomeViewModel : BaseViewModel(), HomeInteractionListener { getCharacters() } + + fun getComics() { - bindStateUpdates( - repository.getAllComics(), - onError = ::onGetComicsFailure, - onNext = ::onGetComicsState - ) + repository.getAllComics().subscribeOn(Schedulers.io()) .observeOn( + AndroidSchedulers + .mainThread() + ) + .subscribe( + ::onGetComicsState + ) } - private fun onGetComicsFailure(throwable: Throwable) { - _comics.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - private fun onGetComicsState(state: State>) { - _comics.postValue(state) + + private fun onGetComicsState(comics: List) { + _comics.postValue(comics) } fun getEvents() { - bindStateUpdates( - repository.getAllEvents(), - onError = ::onGetEventsFailure, - onNext = ::onGetEventsState + repository.getAllEvents().subscribeOn(Schedulers.io()) .observeOn( + AndroidSchedulers + .mainThread() ) + .subscribe( + ::onGetEventsState + ) } - private fun onGetEventsFailure(throwable: Throwable) { - _events.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - private fun onGetEventsState(state: State>) { - _events.postValue(state) + + private fun onGetEventsState(eventsEntity: List) { + _events.postValue(eventsEntity) } fun getCharacters() { - bindStateUpdates( - repository.getAllCharacters(), - onError = ::onGetCharactersFailure, - onNext = ::onGetCharactersState + repository.getAllCharacters().subscribeOn(Schedulers.io()) .observeOn( + AndroidSchedulers + .mainThread() ) + .subscribe( + ::onGetCharactersState + ) } - private fun onGetCharactersFailure(throwable: Throwable) { - _characters.postValue(State.Failed(throwable.message ?: UNKNOWN_ERROR)) - } - private fun onGetCharactersState(state: State>) { - _characters.postValue(state) + + private fun onGetCharactersState(charsEntity: List) { + _characters.postValue(charsEntity) } override fun doOnComicClicked(comicId: Int) { diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt index 87d722b2..e703c9cb 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/CharactersAdapter.kt @@ -1,13 +1,13 @@ package com.red_velvet.marvel.ui.home.adapter import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Character +import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.home.HomeInteractionListener class CharactersAdapter( - items: List, + items: List, listener: HomeInteractionListener -) : BaseAdapter(items, listener) { +) : BaseAdapter(items, listener) { override val layoutId = R.layout.item_home_character } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt index e420717d..e652d808 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/ComicsAdapter.kt @@ -1,13 +1,13 @@ package com.red_velvet.marvel.ui.home.adapter import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.home.HomeInteractionListener class ComicsAdapter( - items: List, + items: List, listener: HomeInteractionListener -) : BaseAdapter(items, listener) { +) : BaseAdapter(items, listener) { override val layoutId = R.layout.item_home_comic } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt index c19f884d..f7744fbb 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/adapter/EventsAdapter.kt @@ -1,13 +1,13 @@ package com.red_velvet.marvel.ui.home.adapter import com.red_velvet.marvel.R -import com.red_velvet.marvel.data.model.Event +import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.ui.base.BaseAdapter import com.red_velvet.marvel.ui.home.HomeInteractionListener class EventsAdapter( - items: List, + items: List, listener: HomeInteractionListener -) : BaseAdapter(items, listener) { +) : BaseAdapter(items, listener) { override val layoutId = R.layout.item_home_event } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesFragment.kt index 64f55550..27a5b437 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesFragment.kt @@ -7,7 +7,9 @@ import androidx.navigation.fragment.findNavController import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentSeriesBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class SeriesFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_series diff --git a/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesViewModel.kt index ef46cfdc..9e7547c5 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/series/SeriesViewModel.kt @@ -10,12 +10,14 @@ import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.kotlin.addTo import java.util.concurrent.TimeUnit +import javax.inject.Inject - -class SeriesViewModel : BaseViewModel(), SeriesInteractionListener { +@HiltViewModel +class SeriesViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), SeriesInteractionListener { private val _navigationToSeriesDetails: MutableLiveData> = MutableLiveData() val navigationToSeriesDetails: LiveData> = _navigationToSeriesDetails @@ -23,7 +25,7 @@ class SeriesViewModel : BaseViewModel(), SeriesInteractionListener { private val _series: MutableLiveData>> = MutableLiveData() val series: LiveData>> = _series - val repository: MarvelRepository by lazy { MarvelRepositoryImpl(RetrofitClient.apiService) } + val searchQuery = MutableLiveData() diff --git a/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsFragment.kt index ac4f434a..9bc2f046 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsFragment.kt @@ -7,7 +7,9 @@ import androidx.navigation.fragment.navArgs import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentSeriesDetailsBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class SeriesDetailsFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_series_details diff --git a/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsViewModel.kt index 1205b392..c87afe91 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/seriesDetails/SeriesDetailsViewModel.kt @@ -9,10 +9,13 @@ import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject + +@HiltViewModel +class SeriesDetailsViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), CreatorListenerInteraction { -class SeriesDetailsViewModel : BaseViewModel(), CreatorListenerInteraction { - private val repository: MarvelRepository = MarvelRepositoryImpl(RetrofitClient.apiService) private val _series: MutableLiveData>> = MutableLiveData() val series: LiveData>> = _series diff --git a/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesFragment.kt index 4a8d5061..d74bd5e7 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesFragment.kt @@ -7,7 +7,9 @@ import androidx.navigation.fragment.findNavController import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentStoriesBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class StoriesFragment : BaseFragment() { override val layoutIdFragment = R.layout.fragment_stories diff --git a/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesViewModel.kt index 7655a8e9..cfd06147 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/stories/StoriesViewModel.kt @@ -4,15 +4,18 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import com.red_velvet.marvel.data.model.Story import com.red_velvet.marvel.data.remote.RetrofitClient +import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject +@HiltViewModel +class StoriesViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), StoriesInteractionListener { -class StoriesViewModel : BaseViewModel(), StoriesInteractionListener { - private val repository by lazy { MarvelRepositoryImpl(RetrofitClient.apiService) } private val _navigationToStoryDetails: MutableLiveData> = MutableLiveData() val navigationToStoryDetails: LiveData> = _navigationToStoryDetails diff --git a/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsFragment.kt b/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsFragment.kt index 96658c34..b3f1460d 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsFragment.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsFragment.kt @@ -7,8 +7,9 @@ import androidx.navigation.fragment.navArgs import com.red_velvet.marvel.R import com.red_velvet.marvel.databinding.FragmentStoryBinding import com.red_velvet.marvel.ui.base.BaseFragment +import dagger.hilt.android.AndroidEntryPoint - +@AndroidEntryPoint class StoryDetailsFragment : BaseFragment() { override val layoutIdFragment: Int = R.layout.fragment_story diff --git a/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsViewModel.kt index 6040a61e..3c4907cc 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/storyDetails/StoryDetailsViewModel.kt @@ -2,7 +2,7 @@ package com.red_velvet.marvel.ui.storyDetails import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import com.red_velvet.marvel.data.model.Comic +import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.Creator import com.red_velvet.marvel.data.model.Story import com.red_velvet.marvel.data.remote.RetrofitClient @@ -10,21 +10,22 @@ import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.State +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject -class StoryDetailsViewModel : BaseViewModel(), StoryCreatorInteractionListener { +@HiltViewModel +class StoryDetailsViewModel @Inject constructor (private val repository: MarvelRepository) : BaseViewModel(), StoryCreatorInteractionListener { private val _story: MutableLiveData>> = MutableLiveData() val story: LiveData>> = _story - private val _comics: MutableLiveData>> = MutableLiveData() - val comics: LiveData>> = _comics + private val _comics: MutableLiveData>> = MutableLiveData() + val comics: LiveData>> = _comics private val _creators: MutableLiveData>> = MutableLiveData() val creators: LiveData>> = _creators - private val repository: MarvelRepository by lazy { - MarvelRepositoryImpl(RetrofitClient.apiService) - } + fun loadStoryDetails(storyId: Int) { getStoryById(storyId) @@ -68,7 +69,7 @@ class StoryDetailsViewModel : BaseViewModel(), StoryCreatorInteractionListener { _comics.postValue(State.Failed(error.message.toString())) } - private fun onGetComicsState(state: State>) { + private fun onGetComicsState(state: State>) { _comics.postValue(state) } diff --git a/app/src/main/res/layout/characters_item.xml b/app/src/main/res/layout/characters_item.xml index 1f71195a..7a48b1f7 100644 --- a/app/src/main/res/layout/characters_item.xml +++ b/app/src/main/res/layout/characters_item.xml @@ -6,7 +6,7 @@ + type="com.red_velvet.marvel.data.model.CharacterDto" /> diff --git a/app/src/main/res/layout/item_character_comics.xml b/app/src/main/res/layout/item_character_comics.xml index da01aee2..c94b1515 100644 --- a/app/src/main/res/layout/item_character_comics.xml +++ b/app/src/main/res/layout/item_character_comics.xml @@ -7,7 +7,7 @@ + type="com.red_velvet.marvel.data.model.ComicDto" /> + type="com.red_velvet.marvel.data.model.CharacterDto" /> + type="com.red_velvet.marvel.data.model.CharacterDto" /> diff --git a/app/src/main/res/layout/item_event.xml b/app/src/main/res/layout/item_event.xml index 4a4a25f9..4c379c54 100644 --- a/app/src/main/res/layout/item_event.xml +++ b/app/src/main/res/layout/item_event.xml @@ -6,7 +6,7 @@ + type="com.red_velvet.marvel.data.model.EventDto" /> diff --git a/app/src/main/res/layout/item_home_character.xml b/app/src/main/res/layout/item_home_character.xml index 7e6ae166..b53e809b 100644 --- a/app/src/main/res/layout/item_home_character.xml +++ b/app/src/main/res/layout/item_home_character.xml @@ -7,7 +7,7 @@ + type="com.red_velvet.marvel.data.model.CharacterDto" /> + type="com.red_velvet.marvel.data.model.ComicDto" /> + type="com.red_velvet.marvel.data.model.EventDto" /> Date: Sat, 20 May 2023 04:21:00 +0300 Subject: [PATCH 7/7] chash and di --- .../red_velvet/marvel/data/local/MovieDao.kt | 8 +- .../data/repository/MarvelRepository.kt | 11 +- .../data/repository/MarvelRepositoryImpl.kt | 114 ++++++++---------- .../red_velvet/marvel/di/RepositoryModule.kt | 16 ++- .../domain/mappers/CharsEntityMapper.kt | 17 +++ .../marvel/domain/mappers/CharsMapper.kt | 9 +- .../domain/mappers/ComicsEntityMapper.kt | 15 +++ .../marvel/domain/mappers/ComicsMapper.kt | 7 +- .../domain/mappers/EventEntityMapper.kt | 17 +++ .../marvel/domain/mappers/EventMapper.kt | 8 +- .../domain/models/{event.kt => Event.kt} | 0 .../marvel/ui/home/HomeViewModel.kt | 40 ++++-- app/src/main/res/layout/fragment_home.xml | 47 ++------ 13 files changed, 177 insertions(+), 132 deletions(-) create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsEntityMapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsEntityMapper.kt create mode 100644 app/src/main/java/com/red_velvet/marvel/domain/mappers/EventEntityMapper.kt rename app/src/main/java/com/red_velvet/marvel/domain/models/{event.kt => Event.kt} (100%) diff --git a/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt index dd0f61dc..08a79bde 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/local/MovieDao.kt @@ -10,24 +10,22 @@ import com.red_velvet.marvel.data.entity.EventsSearch import com.red_velvet.marvel.data.entity.SeriesSearch import io.reactivex.rxjava3.core.Completable import io.reactivex.rxjava3.core.Observable -import io.reactivex.rxjava3.core.Single -import retrofit2.Response interface MovieDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertChars(charsEntity: CharsEntity): Completable + fun insertChars(charsEntity: List): Completable @Query("SELECT * FROM CHARS_TABLE") fun getAllChars() : Observable> @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertComics(comicsEntity: ComicsEntity):Completable + fun insertComics(comicsEntity: List):Completable @Query("SELECT * FROM COMICS_TABLE") fun getAllComics() : Observable> @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertEvents(eventsEntity: EventsEntity): Completable + fun insertEvents(eventsEntity: List): Completable @Query("SELECT * FROM EVENTS_TABLE") fun getAllEvents() : Observable> diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt index a45420e4..4132d2f9 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepository.kt @@ -12,6 +12,9 @@ import com.red_velvet.marvel.data.model.Creator import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.model.Story +import com.red_velvet.marvel.domain.models.Chars +import com.red_velvet.marvel.domain.models.Comic +import com.red_velvet.marvel.domain.models.Event import com.red_velvet.marvel.ui.utils.State import io.reactivex.rxjava3.core.Observable @@ -66,13 +69,11 @@ interface MarvelRepository { fun refreshCharacters() fun refreshEvents() fun refreshEventsSearch() - fun refreshSeries() - fun getSeries(): Observable> fun getEventsSearch(): Observable> - fun getAllComics(): Observable> - fun getAllEvents(): Observable> - fun getAllCharacters(): Observable> + fun getAllComics(): Observable> + fun getAllEvents(): Observable> + fun getAllCharacters(): Observable> } diff --git a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt index 2693b96d..0f3569c0 100644 --- a/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt +++ b/app/src/main/java/com/red_velvet/marvel/data/repository/MarvelRepositoryImpl.kt @@ -5,7 +5,6 @@ import com.red_velvet.marvel.data.entity.CharsEntity import com.red_velvet.marvel.data.entity.ComicsEntity import com.red_velvet.marvel.data.entity.EventsEntity import com.red_velvet.marvel.data.entity.EventsSearch -import com.red_velvet.marvel.data.entity.SeriesSearch import com.red_velvet.marvel.data.local.MovieDataBase import com.red_velvet.marvel.data.model.BaseResponse import com.red_velvet.marvel.data.model.CharacterDto @@ -14,22 +13,32 @@ import com.red_velvet.marvel.data.model.Creator import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.data.model.Series import com.red_velvet.marvel.data.model.Story -import com.red_velvet.marvel.domain.mappers.ComicsMapper import com.red_velvet.marvel.data.remote.MarvelService +import com.red_velvet.marvel.domain.mappers.CharsEntityMapper import com.red_velvet.marvel.domain.mappers.CharsMapper +import com.red_velvet.marvel.domain.mappers.ComicsEntityMapper +import com.red_velvet.marvel.domain.mappers.ComicsMapper +import com.red_velvet.marvel.domain.mappers.EventEntityMapper import com.red_velvet.marvel.domain.mappers.EventMapper +import com.red_velvet.marvel.domain.models.Chars +import com.red_velvet.marvel.domain.models.Comic +import com.red_velvet.marvel.domain.models.Event import com.red_velvet.marvel.ui.utils.State import io.reactivex.rxjava3.core.Observable import io.reactivex.rxjava3.core.Single +import io.reactivex.rxjava3.schedulers.Schedulers import retrofit2.Response import javax.inject.Inject class MarvelRepositoryImpl @Inject constructor( private val marvelServiceImpl: MarvelService, private val daoMovie : MovieDataBase, - private val comicsMapper: ComicsMapper, - private val eventMapper: EventMapper, - private val charsMapper: CharsMapper + private val comicsMapper: ComicsMapper, + private val eventMapper: EventMapper, + private val charsMapper: CharsMapper, + private val comicsEntityMapper: ComicsEntityMapper, + private val eventEntityMapper: EventEntityMapper, + private val charsEntityMapper: CharsEntityMapper ) : MarvelRepository { @@ -40,8 +49,10 @@ class MarvelRepositoryImpl @Inject constructor( return wrapWithState { marvelServiceImpl.getAllComics(titleStartsWith, dateDescriptor) } } - override fun getAllComics(): Observable> { - return daoMovie.movieDao().getAllComics() + override fun getAllComics(): Observable> { + return daoMovie.movieDao().getAllComics().map { + it.map { comicsMapper.map(it) } + } } override fun getComicById(comicId: Int): Observable>> { @@ -71,8 +82,10 @@ class MarvelRepositoryImpl @Inject constructor( return wrapWithState { marvelServiceImpl.getAllEvents(query) } } - override fun getAllEvents(): Observable> { - return daoMovie.movieDao().getAllEvents() + override fun getAllEvents(): Observable> { + return daoMovie.movieDao().getAllEvents().map { + it.map { eventMapper.map(it) } + } } override fun getCreatorByComicId(comicId: Int): Observable>> { @@ -87,8 +100,10 @@ class MarvelRepositoryImpl @Inject constructor( return wrapWithState { marvelServiceImpl.getAllCharacters(nameStartsWith) } } - override fun getAllCharacters(): Observable> { - return daoMovie.movieDao().getAllChars() + override fun getAllCharacters(): Observable> { + return daoMovie.movieDao().getAllChars().map { + it.map { charsMapper.map(it) } + } } override fun getCharacterById(characterId: Int): Observable>> { @@ -122,35 +137,29 @@ class MarvelRepositoryImpl @Inject constructor( } override fun refreshComics() { - marvelServiceImpl.getAllComics().map { response -> - if (response.isSuccessful) - response.body()?.body?.results?.map { comic -> - daoMovie.movieDao().insertComics( - ComicsEntity( - comic.id!!, comic.title.toString(), - "${comic.thumbnail?.path}.${comic.thumbnail?.extension}" - ) - ) - + marvelServiceImpl.getAllComics() + .observeOn(Schedulers.io()) + .map { response -> + if (response.isSuccessful){ + val comicEntities= response.body()?.body?.results?.map { + comicsEntityMapper.map(it) } - } + daoMovie.movieDao().insertComics(comicEntities!!) + } }.subscribe() } override fun refreshEvents() { - marvelServiceImpl.getAllEvents().map { response -> - if (response.isSuccessful) - response.body()?.body?.results?.map { event -> - daoMovie.movieDao().insertEvents( - EventsEntity( - event.id!!, event.title.toString(), - "${event.thumbnail?.path}.${event.thumbnail?.extension}" - ) - ) - - } - } + marvelServiceImpl.getAllEvents() + .observeOn(Schedulers.io()) + .map { response -> + if (response.isSuccessful){ + val eventEntities= response.body()?.body?.results?.map { + eventEntityMapper.map(it) + } + daoMovie.movieDao().insertEvents(eventEntities!!) + } }.subscribe() } override fun refreshEventsSearch() { @@ -168,24 +177,8 @@ class MarvelRepositoryImpl @Inject constructor( } } - override fun refreshSeries() { - marvelServiceImpl.getAllSeries().map { response -> - if (response.isSuccessful) - response.body()?.body?.results?.map { series -> - daoMovie.movieDao().insertSeries( - SeriesSearch( - series.id!!, series.title.toString(), - "${series.thumbnail?.path}.${series.thumbnail?.extension}" - ) - ) - } - } - } - override fun getSeries(): Observable> { - return daoMovie.movieDao().getAllSeries() - } override fun getEventsSearch(): Observable> { return daoMovie.movieDao().getAllEventsSearch() @@ -194,18 +187,15 @@ class MarvelRepositoryImpl @Inject constructor( override fun refreshCharacters(){ - marvelServiceImpl.getAllCharacters().map { response -> - if (response.isSuccessful) - response.body()?.body?.results?.map { character -> - daoMovie.movieDao().insertChars( - CharsEntity( - character.id!!, character.name.toString(), - "${character.thumbnail?.path}.${character.thumbnail?.extension}" - ) - ) - - } - } + marvelServiceImpl.getAllCharacters() + .observeOn(Schedulers.io()) + .map { response -> + if (response.isSuccessful){ + val charsEntities= response.body()?.body?.results?.map { + charsEntityMapper.map(it) + } + daoMovie.movieDao().insertChars(charsEntities!!) + } }.subscribe() } diff --git a/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt b/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt index 58ebfb58..86d95933 100644 --- a/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt +++ b/app/src/main/java/com/red_velvet/marvel/di/RepositoryModule.kt @@ -4,8 +4,11 @@ import com.red_velvet.marvel.data.local.MovieDataBase import com.red_velvet.marvel.data.remote.MarvelService import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl +import com.red_velvet.marvel.domain.mappers.CharsEntityMapper import com.red_velvet.marvel.domain.mappers.CharsMapper +import com.red_velvet.marvel.domain.mappers.ComicsEntityMapper import com.red_velvet.marvel.domain.mappers.ComicsMapper +import com.red_velvet.marvel.domain.mappers.EventEntityMapper import com.red_velvet.marvel.domain.mappers.EventMapper import dagger.Module import dagger.Provides @@ -20,15 +23,22 @@ class RepositoryModule { marvelServiceImpl: MarvelService, daoMovie: MovieDataBase, comicsMapper: ComicsMapper, - eventMapper: EventMapper, - charsMapper: CharsMapper + eventMapper:EventMapper, + charsMapper: CharsMapper, + comicsEntityMapper: ComicsEntityMapper, + eventEntityMapper: EventEntityMapper, + charsEntityMapper: CharsEntityMapper ): MarvelRepository { return MarvelRepositoryImpl( marvelServiceImpl, daoMovie, comicsMapper, eventMapper, - charsMapper + charsMapper, + comicsEntityMapper + ,eventEntityMapper, + charsEntityMapper + ) } } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsEntityMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsEntityMapper.kt new file mode 100644 index 00000000..0be993a7 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsEntityMapper.kt @@ -0,0 +1,17 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.entity.CharsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.model.CharacterDto +import com.red_velvet.marvel.data.model.ComicDto +import javax.inject.Inject + +class CharsEntityMapper @Inject constructor() : Mapper { + override fun map(input: CharacterDto): CharsEntity { + return CharsEntity( + id = input.id ?: 0, + title = input.name ?: "", + img = "${input.thumbnail?.path}.${input.thumbnail?.extension}" + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt index e154c74c..c38543a8 100644 --- a/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/CharsMapper.kt @@ -1,15 +1,16 @@ package com.red_velvet.marvel.domain.mappers +import com.red_velvet.marvel.data.entity.CharsEntity import com.red_velvet.marvel.data.model.CharacterDto import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.domain.models.Chars import com.red_velvet.marvel.domain.models.Comic import javax.inject.Inject -class CharsMapper @Inject constructor(): Mapper { - override fun map(input: CharacterDto): Chars { +class CharsMapper @Inject constructor(): Mapper { + override fun map(input: CharsEntity): Chars { return Chars( - id =input.id, title = input.name, - image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + id =input.id, title = input.title, + image =input.img ) } } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsEntityMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsEntityMapper.kt new file mode 100644 index 00000000..1a32e9f2 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsEntityMapper.kt @@ -0,0 +1,15 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.model.ComicDto +import javax.inject.Inject + +class ComicsEntityMapper @Inject constructor() : Mapper { + override fun map(input: ComicDto): ComicsEntity { + return ComicsEntity( + id = input.id ?: 0, + title = input.title ?: "", + img = "${input.thumbnail?.path}.${input.thumbnail?.extension}" + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt index b4359054..374e602f 100644 --- a/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/ComicsMapper.kt @@ -1,13 +1,14 @@ package com.red_velvet.marvel.domain.mappers +import com.red_velvet.marvel.data.entity.ComicsEntity import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.domain.models.Comic import javax.inject.Inject -class ComicsMapper @Inject constructor() : Mapper { - override fun map(input: ComicDto): Comic { +class ComicsMapper @Inject constructor() : Mapper { + override fun map(input: ComicsEntity): Comic { return Comic( id =input.id, title = input.title, - image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + image = input.img ) } } diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventEntityMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventEntityMapper.kt new file mode 100644 index 00000000..217fa1f8 --- /dev/null +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventEntityMapper.kt @@ -0,0 +1,17 @@ +package com.red_velvet.marvel.domain.mappers + +import com.red_velvet.marvel.data.entity.ComicsEntity +import com.red_velvet.marvel.data.entity.EventsEntity +import com.red_velvet.marvel.data.model.ComicDto +import com.red_velvet.marvel.data.model.EventDto +import javax.inject.Inject + +class EventEntityMapper @Inject constructor() : Mapper { + override fun map(input: EventDto): EventsEntity { + return EventsEntity( + id = input.id ?: 0, + title = input.title ?: "", + img = "${input.thumbnail?.path}.${input.thumbnail?.extension}" + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt index be387d65..38d408bb 100644 --- a/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt +++ b/app/src/main/java/com/red_velvet/marvel/domain/mappers/EventMapper.kt @@ -1,15 +1,17 @@ package com.red_velvet.marvel.domain.mappers +import android.provider.CalendarContract.EventsEntity +import com.red_velvet.marvel.data.entity.ComicsEntity import com.red_velvet.marvel.data.model.ComicDto import com.red_velvet.marvel.data.model.EventDto import com.red_velvet.marvel.domain.models.Comic import com.red_velvet.marvel.domain.models.Event import javax.inject.Inject -class EventMapper @Inject constructor() : Mapper { - override fun map(input: EventDto): Event { +class EventMapper @Inject constructor() : Mapper { + override fun map(input: EventsEntity): Event { return Event( id =input.id, title = input.title, - image = "${input.thumbnail?.path}.${input.thumbnail?.extension}" ) + image = input.img ) } } \ No newline at end of file diff --git a/app/src/main/java/com/red_velvet/marvel/domain/models/event.kt b/app/src/main/java/com/red_velvet/marvel/domain/models/Event.kt similarity index 100% rename from app/src/main/java/com/red_velvet/marvel/domain/models/event.kt rename to app/src/main/java/com/red_velvet/marvel/domain/models/Event.kt diff --git a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt index ce97b6cc..b8c605b1 100644 --- a/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/red_velvet/marvel/ui/home/HomeViewModel.kt @@ -8,6 +8,9 @@ import com.red_velvet.marvel.data.entity.EventsEntity import com.red_velvet.marvel.data.remote.RetrofitClient import com.red_velvet.marvel.data.repository.MarvelRepository import com.red_velvet.marvel.data.repository.MarvelRepositoryImpl +import com.red_velvet.marvel.domain.models.Chars +import com.red_velvet.marvel.domain.models.Comic +import com.red_velvet.marvel.domain.models.Event import com.red_velvet.marvel.ui.base.BaseViewModel import com.red_velvet.marvel.ui.utils.SingleEvent import dagger.hilt.android.lifecycle.HiltViewModel @@ -29,14 +32,14 @@ class HomeViewModel @Inject constructor (private val repository: MarvelRepositor val navigationToCharacterDetails: LiveData> = _navigationToCharacterDetails - private val _events = MutableLiveData>() - val eventLiveData: LiveData> = _events + private val _events = MutableLiveData>() + val eventLiveData: LiveData> = _events - private val _characters = MutableLiveData>() - val characterLiveData: LiveData> = _characters + private val _characters = MutableLiveData>() + val characterLiveData: LiveData> = _characters - private val _comics = MutableLiveData>() - val comics: LiveData> get() = _comics + private val _comics = MutableLiveData>() + val comics: LiveData> get() = _comics init { getComics() @@ -52,15 +55,19 @@ class HomeViewModel @Inject constructor (private val repository: MarvelRepositor .mainThread() ) .subscribe( - ::onGetComicsState + ::onGetComicsSucccess, + ::onGetComicsFailed ) } - private fun onGetComicsState(comics: List) { + private fun onGetComicsSucccess(comics: List) { _comics.postValue(comics) } + private fun onGetComicsFailed(throwable: Throwable) { + _comics.postValue(emptyList()) + } fun getEvents() { repository.getAllEvents().subscribeOn(Schedulers.io()) .observeOn( @@ -68,13 +75,17 @@ class HomeViewModel @Inject constructor (private val repository: MarvelRepositor .mainThread() ) .subscribe( - ::onGetEventsState + ::onGetEventsSuccess, + ::onGetEventsFailed ) } + private fun onGetEventsFailed(throwable: Throwable) { + _events.postValue(emptyList()) + } - private fun onGetEventsState(eventsEntity: List) { + private fun onGetEventsSuccess(eventsEntity: List) { _events.postValue(eventsEntity) } @@ -84,13 +95,16 @@ class HomeViewModel @Inject constructor (private val repository: MarvelRepositor .mainThread() ) .subscribe( - ::onGetCharactersState + ::onGetCharactersState, + ::onGetCharsFailed ) } + private fun onGetCharsFailed(throwable: Throwable) { + _events.postValue(emptyList()) + } - - private fun onGetCharactersState(charsEntity: List) { + private fun onGetCharactersState(charsEntity: List) { _characters.postValue(charsEntity) } diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 79d2885e..c2d30623 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -35,7 +35,7 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" - app:showWhenSuccess="@{viewModel.comicsLiveData}" /> + /> + /> - - + + app:layout_constraintEnd_toEndOf="@id/recycler_chars" + app:layout_constraintStart_toStartOf="@id/recycler_chars" + app:layout_constraintTop_toBottomOf="@id/recycler_chars" + " />