Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Manga Tab in Favorite Fragment #83

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
implementation project(path: ':testing')
implementation project(path: ':domain')
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

// test dependencies
testImplementation "junit:junit:$junit"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package divyansh.tech.animeclassroom.favorites.screens.manga

import divyansh.tech.animeclassroom.common.AnimeClickCallback
import divyansh.tech.animeclassroom.common.CommonViewModel
import divyansh.tech.animeclassroom.favorites.FavoritesFragmentDirections

class FavoriteMangaCallbacks(private val viewModel: CommonViewModel): AnimeClickCallback {
override fun onMangaClicked(mangaUrl: String) {
viewModel.changeNavigation(
FavoritesFragmentDirections.actionFavoritesFragmentToMangaDetailFragment(
url = mangaUrl
)
)
}

override fun onAnimeClicked(animeUrl: String) {}
override fun onEpisodeClicked(episodeUrl: String) {}
override fun onGenreClicked(genreUrl: String) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package divyansh.tech.animeclassroom.favorites.screens.manga

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import dagger.hilt.android.AndroidEntryPoint
import divyansh.tech.animeclassroom.EventObserver
import divyansh.tech.animeclassroom.ResultWrapper
import divyansh.tech.animeclassroom.databinding.FragmentFavoriteMangaBinding
import divyansh.tech.animeclassroom.favorites.screens.manga.epoxy.EpoxyFavoriteMangaController

@AndroidEntryPoint
class FavoriteMangaFragment : Fragment() {

companion object {
fun getInstance() = FavoriteMangaFragment()
}

private lateinit var _favouriteMangaFragmentBinding: FragmentFavoriteMangaBinding
private val binding: FragmentFavoriteMangaBinding
get() = _favouriteMangaFragmentBinding

private val viewModel by viewModels<FavoriteMangaViewModel>()
private val favoriteMangaController: EpoxyFavoriteMangaController by lazy {
EpoxyFavoriteMangaController(FavoriteMangaCallbacks(viewModel))
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View{
_favouriteMangaFragmentBinding = FragmentFavoriteMangaBinding.inflate(inflater)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupRecyclerView()
setupObservers()
}

private fun setupObservers() {
viewModel.favouriteMangaListState.observe(viewLifecycleOwner) { result ->
when (result) {
is ResultWrapper.Success -> {
result.data?.let {
binding.noMangaText.visibility =
if (it.isEmpty()) View.VISIBLE else View.GONE
favoriteMangaController.setData(it)
}
}
is ResultWrapper.Error -> Log.i("FAVORITES", result.message.toString())
is ResultWrapper.Loading -> {}
}
}

viewModel.navigation.observe(
viewLifecycleOwner,
EventObserver{
findNavController().navigate(it)
}
)
}

private fun setupRecyclerView() {
with(binding.favoriteMangaList) {
layoutManager = GridLayoutManager(requireContext(), 3)
adapter = favoriteMangaController.adapter
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package divyansh.tech.animeclassroom.favorites.screens.manga

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import divyansh.tech.animeclassroom.Manga.MangaHomeLocalRepo
import divyansh.tech.animeclassroom.ResultWrapper
import divyansh.tech.animeclassroom.common.CommonViewModel
import divyansh.tech.animeclassroom.di.DispatcherModule
import divyansh.tech.animeclassroom.mangaModels.Manga
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch
import javax.inject.Inject

//TODO: Implement MangaHomeLocalRepo and Replace Manga Model with OfflineManga Model

@HiltViewModel
class FavoriteMangaViewModel @Inject constructor(
favouriteMangaHomeLocalRepo: MangaHomeLocalRepo,
@DispatcherModule.IODispatcher coroutineDispatcher: CoroutineDispatcher
): CommonViewModel(){

private val _favouriteMangaListState: MutableLiveData<ResultWrapper<List<Manga>>> =
MutableLiveData()
val favouriteMangaListState: LiveData<ResultWrapper<List<Manga>>> = _favouriteMangaListState

init{
viewModelScope.launch(coroutineDispatcher){
_favouriteMangaListState.postValue(ResultWrapper.Loading())
_favouriteMangaListState.postValue(ResultWrapper.Success(ArrayList()))
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package divyansh.tech.animeclassroom.favorites.screens.manga.epoxy

import com.airbnb.epoxy.TypedEpoxyController
import divyansh.tech.animeclassroom.common.AnimeClickCallback
import divyansh.tech.animeclassroom.mangaModels.Manga

class EpoxyFavoriteMangaController(
private val mangaClickCallback: AnimeClickCallback
): TypedEpoxyController<List<Manga>>() {

override fun buildModels(data: List<Manga>?) {
data?.let{
it.forEach{
epoxyFavoriteManga {
id(it.mangaUrl)
manga(it)
mangaCallback(mangaClickCallback)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package divyansh.tech.animeclassroom.favorites.screens.manga.epoxy

import androidx.databinding.ViewDataBinding
import com.airbnb.epoxy.DataBindingEpoxyModel
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import divyansh.tech.animeclassroom.R
import divyansh.tech.animeclassroom.BR
import divyansh.tech.animeclassroom.common.AnimeClickCallback
import divyansh.tech.animeclassroom.mangaModels.Manga

@EpoxyModelClass(layout = R.layout.recycler_item_favorite_manga)
abstract class EpoxyFavoriteMangaModel: DataBindingEpoxyModel() {

@EpoxyAttribute
lateinit var manga: Manga

@EpoxyAttribute
lateinit var mangaCallback: AnimeClickCallback

override fun setDataBindingVariables(binding: ViewDataBinding) {
binding.setVariable(BR.manga, manga)
binding.setVariable(BR.mangaCallback, mangaCallback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import divyansh.tech.animeclassroom.favorites.screens.DynamicTabFragment
import divyansh.tech.animeclassroom.favorites.screens.animes.FavoriteAnimeFragment
import divyansh.tech.animeclassroom.favorites.screens.manga.FavoriteMangaFragment


class ViewPagerAdapter(private val list: MutableList<Fragment> = mutableListOf(),
Expand All @@ -26,7 +26,7 @@ class ViewPagerAdapter(private val list: MutableList<Fragment> = mutableListOf()
fun addFragments(){
pageIds.clear()
list.add(FavoriteAnimeFragment.getInstance())
list.add(DynamicTabFragment.getInstance("Mangas"))
list.add(FavoriteMangaFragment.getInstance())
pageIds.addAll(list.map { it.hashCode().toLong() })
notifyDataSetChanged()
}
Expand Down
32 changes: 32 additions & 0 deletions app/src/main/res/layout/fragment_favorite_manga.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/favoriteMangaList"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/noMangaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="@string/empty_favorites_manga_list"
android:textColor="@color/recycler_anime_title"
android:textSize="14sp"
android:ellipsize="end"
android:minLines="2"
android:maxLines="2"
android:fontFamily="sans-serif"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3 changes: 1 addition & 2 deletions app/src/main/res/layout/recycler_item_favorite_anime.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
type="divyansh.tech.animeclassroom.models.home.OfflineAnimeModel" />
<variable
name = "animeCallback"
type = "divyansh.tech.animeclassroom.common.AnimeClickCallback"
/>
type = "divyansh.tech.animeclassroom.common.AnimeClickCallback" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/res/layout/recycler_item_favorite_manga.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="manga"
type="divyansh.tech.animeclassroom.mangaModels.Manga"/>
<variable
name="mangaCallback"
type="divyansh.tech.animeclassroom.common.AnimeClickCallback" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:foreground="?selectableItemBackgroundBorderless"
android:layout_marginBottom="20dp"
android:onClick="@{() -> mangaCallback.onMangaClicked(manga.mangaUrl)}">

<androidx.cardview.widget.CardView
android:id="@+id/animeImageCardView"
android:layout_width="105dp"
android:layout_height="180dp"
app:cardCornerRadius="4dp"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
imageUrl="@{manga.imageUrl}"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>

<TextView
android:id="@+id/animeTitle"
android:layout_width="0dp"
android:fontFamily="sans-serif"
android:layout_height="wrap_content"
android:ellipsize="end"
android:minLines="2"
android:maxLines="2"
android:layout_marginTop="4dp"
android:text="@{manga.name}"
android:textColor="@color/recycler_anime_title"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
android:gravity="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/animeImageCardView"
tools:text="One Piece" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
3 changes: 3 additions & 0 deletions app/src/main/res/navigation/home_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
<action
android:id="@+id/action_favoritesFragment_to_animeDetailFragment"
app:destination="@id/animeDetailFragment" />
<action
android:id="@+id/action_favoritesFragment_to_mangaDetailFragment"
app:destination="@id/mangaDetailFragment" />
</fragment>

<action
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
<item>System</item>
</string-array>
<string name="empty_favorites_anime_list">Empty Favorite Animes List</string>
<string name="empty_favorites_manga_list">Empty Favorite Manga List</string>
<string name="support_us">Support Us</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>