Skip to content
Permalink
Browse files

Tidy up episode details arguments

  • Loading branch information
chrisbanes committed Nov 26, 2019
1 parent a7111dc commit 1a04b69b2e542d60a51e9febbdb6704acbf9571a
@@ -0,0 +1,22 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.tivi.episodedetails

enum class Action {
WATCH,
UNWATCH
}
@@ -16,8 +16,8 @@

package app.tivi.episodedetails

sealed class EpisodeDetailsAction
object RefreshAction : EpisodeDetailsAction()
object AddEpisodeWatchAction : EpisodeDetailsAction()
data class RemoveEpisodeWatchAction(val watchId: Long) : EpisodeDetailsAction()
object RemoveAllEpisodeWatchesAction : EpisodeDetailsAction()
internal sealed class EpisodeDetailsAction
internal object RefreshAction : EpisodeDetailsAction()
internal object AddEpisodeWatchAction : EpisodeDetailsAction()
internal data class RemoveEpisodeWatchAction(val watchId: Long) : EpisodeDetailsAction()
internal object RemoveAllEpisodeWatchesAction : EpisodeDetailsAction()
@@ -26,7 +26,7 @@ import com.airbnb.epoxy.TypedEpoxyController
import javax.inject.Inject
import kotlin.math.roundToInt

class EpisodeDetailsEpoxyController @Inject constructor(
internal class EpisodeDetailsEpoxyController @Inject constructor(
@PerActivity private val context: Context,
private val dateFormatter: TiviDateFormatter
) : TypedEpoxyController<EpisodeDetailsViewState>() {
@@ -17,7 +17,6 @@
package app.tivi.episodedetails

import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -27,31 +26,25 @@ import app.tivi.common.epoxy.SwipeAwayCallbacks
import app.tivi.episodedetails.databinding.FragmentEpisodeDetailsBinding
import app.tivi.extensions.resolveThemeColor
import com.airbnb.epoxy.EpoxyTouchHelper
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import javax.inject.Inject
import kotlinx.android.parcel.Parcelize

class EpisodeDetailsFragment : TiviFragmentWithBinding<FragmentEpisodeDetailsBinding>() {
companion object {
@JvmStatic
fun create(id: Long): EpisodeDetailsFragment {
return EpisodeDetailsFragment().apply {
arguments = bundleOf(MvRx.KEY_ARG to Arguments(id))
arguments = bundleOf("episode_id" to id)
}
}
}

@Parcelize
data class Arguments(val episodeId: Long) : Parcelable

private val viewModel: EpisodeDetailsViewModel by fragmentViewModel()
@Inject lateinit var episodeDetailsViewModelFactory: EpisodeDetailsViewModel.Factory

@Inject lateinit var controller: EpisodeDetailsEpoxyController

@Inject lateinit var textCreator: EpisodeDetailsTextCreator
@Inject internal lateinit var episodeDetailsViewModelFactory: EpisodeDetailsViewModel.Factory
@Inject internal lateinit var controller: EpisodeDetailsEpoxyController
@Inject internal lateinit var textCreator: EpisodeDetailsTextCreator

override fun createBinding(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): FragmentEpisodeDetailsBinding {
return FragmentEpisodeDetailsBinding.inflate(layoutInflater, container, false)
@@ -63,8 +56,8 @@ class EpisodeDetailsFragment : TiviFragmentWithBinding<FragmentEpisodeDetailsBin
binding.epDetailsFab.setOnClickListener {
withState(viewModel) { state ->
when (state.action) {
EpisodeDetailsViewState.Action.WATCH -> viewModel.submitAction(AddEpisodeWatchAction)
EpisodeDetailsViewState.Action.UNWATCH -> viewModel.submitAction(RemoveAllEpisodeWatchesAction)
Action.WATCH -> viewModel.submitAction(AddEpisodeWatchAction)
Action.UNWATCH -> viewModel.submitAction(RemoveAllEpisodeWatchesAction)
}
}
}
@@ -22,7 +22,7 @@ import app.tivi.data.entities.Season
import app.tivi.inject.PerActivity
import javax.inject.Inject

class EpisodeDetailsTextCreator @Inject constructor(
internal class EpisodeDetailsTextCreator @Inject constructor(
@PerActivity private val context: Context
) {
fun seasonEpisodeTitleText(season: Season?, episode: Episode?): String? {
@@ -25,7 +25,6 @@ import app.tivi.domain.interactors.UpdateEpisodeDetails
import app.tivi.domain.launchObserve
import app.tivi.domain.observers.ObserveEpisodeDetails
import app.tivi.domain.observers.ObserveEpisodeWatches
import app.tivi.episodedetails.EpisodeDetailsViewState.Action
import com.airbnb.mvrx.FragmentViewModelContext
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.Success
@@ -37,7 +36,7 @@ import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import org.threeten.bp.OffsetDateTime

class EpisodeDetailsViewModel @AssistedInject constructor(
internal class EpisodeDetailsViewModel @AssistedInject constructor(
@Assisted initialState: EpisodeDetailsViewState,
private val updateEpisodeDetails: UpdateEpisodeDetails,
observeEpisodeDetails: ObserveEpisodeDetails,
@@ -110,9 +109,20 @@ class EpisodeDetailsViewModel @AssistedInject constructor(
}

companion object : MvRxViewModelFactory<EpisodeDetailsViewModel, EpisodeDetailsViewState> {
override fun create(viewModelContext: ViewModelContext, state: EpisodeDetailsViewState): EpisodeDetailsViewModel? {
val fragment: EpisodeDetailsFragment = (viewModelContext as FragmentViewModelContext).fragment()
return fragment.episodeDetailsViewModelFactory.create(state)
override fun create(
viewModelContext: ViewModelContext,
state: EpisodeDetailsViewState
): EpisodeDetailsViewModel? {
val f: EpisodeDetailsFragment = (viewModelContext as FragmentViewModelContext).fragment()
return f.episodeDetailsViewModelFactory.create(state)
}

override fun initialState(
viewModelContext: ViewModelContext
): EpisodeDetailsViewState? {
val f: EpisodeDetailsFragment = (viewModelContext as FragmentViewModelContext).fragment()
val args = f.requireArguments()
return EpisodeDetailsViewState(episodeId = args.getLong("episode_id"))
}
}
}
@@ -27,10 +27,4 @@ data class EpisodeDetailsViewState(
val episode: Episode? = null,
val watches: List<EpisodeWatchEntry> = emptyList(),
val action: Action = Action.WATCH
) : MvRxState {
constructor(args: EpisodeDetailsFragment.Arguments) : this(args.episodeId)

enum class Action {
WATCH, UNWATCH
}
}
) : MvRxState
@@ -20,7 +20,7 @@
xmlns:tools="http://schemas.android.com/tools">

<data>
<import type="app.tivi.episodedetails.EpisodeDetailsViewState.Action" />
<import type="app.tivi.episodedetails.Action" />

<variable
name="state"

0 comments on commit 1a04b69

Please sign in to comment.
You can’t perform that action at this time.