Skip to content

Commit

Permalink
detail for episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
RenHeyzer committed Jan 11, 2022
1 parent 5d8b84b commit f7b0d10
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.rickandmortyapp.presentation.ui.fragments.episodes.detail

import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import by.kirich1409.viewbindingdelegate.viewBinding
import com.example.rickandmortyapp.R
import com.example.rickandmortyapp.base.BaseFragment
import com.example.rickandmortyapp.common.extensions.toFormatDate
import com.example.rickandmortyapp.databinding.FragmentEpisodeDetailBinding
import com.example.rickandmortyapp.presentation.state.UIState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class EpisodeDetailFragment :
BaseFragment<FragmentEpisodeDetailBinding, EpisodeDetailViewModel>(
R.layout.fragment_episode_detail
) {

override val binding by viewBinding(FragmentEpisodeDetailBinding::bind)
override val viewModel by viewModels<EpisodeDetailViewModel>()
private val args by navArgs<EpisodeDetailFragmentArgs>()

override fun setupRequests() {
viewModel.getEpisodeById(args.id)
}

override fun setupObserves() = with(binding) {
viewModel.episodeDetailState.observe(viewLifecycleOwner, {
when (it) {
is UIState.Loading -> {
}
is UIState.Error -> {
}
is UIState.Success -> {
it.data.apply {
detailName.text = name
detailAirDate.text = airDate
detailCreated.text = toFormatDate(created)
detailEpisode.text = episode
?.replace("S", "Season ")
?.replace("E", " Episode ")
}
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.rickandmortyapp.presentation.ui.fragments.episodes.detail

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.rickandmortyapp.base.BaseViewModel
import com.example.rickandmortyapp.domain.models.RickAndMorty
import com.example.rickandmortyapp.domain.usecases.GetEpisodeByIdUseCase
import com.example.rickandmortyapp.presentation.state.UIState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class EpisodeDetailViewModel @Inject constructor(
private val getEpisodeId: GetEpisodeByIdUseCase
) : BaseViewModel() {

private val _episodeDetailState = MutableLiveData<UIState<RickAndMorty.EpisodesItem>>()
val episodeDetailState: LiveData<UIState<RickAndMorty.EpisodesItem>> = _episodeDetailState

fun getEpisodeById(id: Int) = viewModelScope.launch {
subscribeTo(_episodeDetailState) {
getEpisodeId(id)
}
}
}
91 changes: 91 additions & 0 deletions app/src/main/res/layout/fragment_episode_detail.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/item_group_padding_size"
android:backgroundTint="@color/darkest_gray"
android:padding="@dimen/item_group_padding_size"
app:cardCornerRadius="@dimen/twenty_five_dp"
tools:context=".presentation.ui.fragments.episodes.detail.EpisodeDetailFragment">

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/episodes_detail_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/detail_background"
android:padding="@dimen/twenty_five_dp">

<TextView
android:id="@+id/detail_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/twenty_five_dp"
android:fontFamily="@font/passion_one"
android:textColor="@color/white"
android:textSize="@dimen/thirty_dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail_episode"
tools:text="@string/item_title_hint" />

<TextView
android:id="@+id/detail_episode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/passion_one"
android:textAlignment="textEnd"
android:textColor="@color/white"
android:textSize="@dimen/thirty_five_sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/item_episode_text" />

<TextView
android:id="@+id/was_created_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fourteen_dp"
android:text="@string/was_created_in"
android:textColor="@color/gray"
android:textSize="@dimen/fourteen_sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail_air_date" />

<TextView
android:id="@+id/detail_created"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/passion_one"
android:textColor="@color/white"
android:textSize="@dimen/twenty_sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/was_created_in"
tools:text="@string/item_created_text" />

<TextView
android:id="@+id/air_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/fourteen_dp"
android:text="@string/air_date"
android:textColor="@color/gray"
android:textSize="@dimen/fourteen_sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/detail_name" />

<TextView
android:id="@+id/detail_air_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/passion_one"
android:textColor="@color/white"
android:textSize="@dimen/twenty_sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/air_date"
tools:text="@string/air_date" />

</androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

0 comments on commit f7b0d10

Please sign in to comment.