-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...xample/rickandmortyapp/presentation/ui/fragments/episodes/detail/EpisodeDetailFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ") | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ample/rickandmortyapp/presentation/ui/fragments/episodes/detail/EpisodeDetailViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |