Skip to content

Commit

Permalink
Twelve: Use ProviderIdentifier for all fragment arguments
Browse files Browse the repository at this point in the history
Change-Id: If159248edd7a8cb0f55f1d553fed210ae30fe65d
  • Loading branch information
SebaUbuntu committed Feb 13, 2025
1 parent 59cea6d commit f010219
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
providersViewModel.navigationProvider.value?.let {
findNavController().navigateSafe(
R.id.action_mainFragment_to_fragment_provider_information_bottom_sheet_dialog,
ManageProviderFragment.createBundle(it.type, it.typeId),
ManageProviderFragment.createBundle(providerIdentifier = it),
)
true
} ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.lineageos.twelve.R
import org.lineageos.twelve.datasources.MediaError
import org.lineageos.twelve.ext.getParcelable
import org.lineageos.twelve.ext.getSerializable
import org.lineageos.twelve.ext.getViewProperty
import org.lineageos.twelve.ext.selectItem
import org.lineageos.twelve.models.ProviderArgument
import org.lineageos.twelve.models.ProviderArgument.Companion.validateArgument
import org.lineageos.twelve.models.ProviderIdentifier
import org.lineageos.twelve.models.ProviderType
import org.lineageos.twelve.models.RequestStatus
import org.lineageos.twelve.ui.recyclerview.SimpleListAdapter
Expand All @@ -59,10 +61,10 @@ class ManageProviderFragment : Fragment(R.layout.fragment_manage_provider) {
private val toolbar by getViewProperty<MaterialToolbar>(R.id.toolbar)

// Arguments
private val providerIdentifier: ProviderIdentifier?
get() = arguments?.getParcelable(ARG_PROVIDER_IDENTIFIER, ProviderIdentifier::class)
private val providerType: ProviderType?
get() = arguments?.getSerializable(ARG_PROVIDER_TYPE, ProviderType::class)
private val providerTypeId: Long?
get() = arguments?.getLong(ARG_PROVIDER_TYPE_ID, -1L).takeIf { it != -1L }

// Providers
private var selectedProviderType: ProviderType? = null
Expand Down Expand Up @@ -127,13 +129,7 @@ class ManageProviderFragment : Fragment(R.layout.fragment_manage_provider) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel.setProviderIds(
providerType?.let { providerType ->
providerTypeId?.let {
providerType to it
}
}
)
viewModel.setProviderIdentifier(providerIdentifier)

selectedProviderType = providerType?.also {
require(manageableProviderTypes.contains(it)) { "Invalid provider type: $it" }
Expand Down Expand Up @@ -340,8 +336,8 @@ class ManageProviderFragment : Fragment(R.layout.fragment_manage_provider) {
companion object {
private val LOG_TAG = ManageProviderFragment::class.simpleName!!

private const val ARG_PROVIDER_IDENTIFIER = "provider_identifier"
private const val ARG_PROVIDER_TYPE = "provider_type"
private const val ARG_PROVIDER_TYPE_ID = "provider_type_id"

private val argumentsDiffCallback = object : DiffUtil.ItemCallback<ProviderArgument<*>>() {
override fun areItemsTheSame(
Expand All @@ -359,16 +355,16 @@ class ManageProviderFragment : Fragment(R.layout.fragment_manage_provider) {

/**
* Create a [Bundle] to use as the arguments for this fragment.
* @param providerType A [ProviderType] to either use as an hint for the creation of a new
* instance or the type of the provider to edit or delete
* @param providerTypeId The type specific ID of the provider to edit or delete
* @param providerIdentifier The identifier of the provider to edit or delete
* @param providerType A [ProviderType] to use as an hint for the creation of a new
* instance, ignored when [providerIdentifier] is provided
*/
fun createBundle(
providerIdentifier: ProviderIdentifier? = null,
providerType: ProviderType? = null,
providerTypeId: Long? = null,
) = bundleOf(
ARG_PROVIDER_IDENTIFIER to providerIdentifier,
ARG_PROVIDER_TYPE to providerType,
ARG_PROVIDER_TYPE_ID to providerTypeId,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import org.lineageos.twelve.R
import org.lineageos.twelve.ext.getSerializable
import org.lineageos.twelve.ext.getParcelable
import org.lineageos.twelve.ext.getViewProperty
import org.lineageos.twelve.ext.navigateSafe
import org.lineageos.twelve.models.DataSourceInformation
import org.lineageos.twelve.models.ProviderType
import org.lineageos.twelve.models.ProviderIdentifier
import org.lineageos.twelve.models.RequestStatus
import org.lineageos.twelve.models.RequestStatus.Companion.fold
import org.lineageos.twelve.ui.recyclerview.SimpleListAdapter
Expand Down Expand Up @@ -74,12 +74,10 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
}

// Arguments
private val providerType: ProviderType
get() = requireArguments().getSerializable(ARG_PROVIDER_TYPE, ProviderType::class)!!
private val providerTypeId: Long
get() = requireArguments().getLong(ARG_PROVIDER_TYPE_ID, -1L).takeIf {
it != -1L
}!!
private val providerIdentifier: ProviderIdentifier
get() = requireArguments().getParcelable(
ARG_PROVIDER_IDENTIFIER, ProviderIdentifier::class
)!!

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -90,7 +88,9 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
onSuccess = {
findNavController().navigateSafe(
R.id.action_providerInformationBottomSheetDialogFragment_to_fragment_manage_provider,
ManageProviderFragment.createBundle(providerType, providerTypeId),
ManageProviderFragment.createBundle(
providerIdentifier = providerIdentifier
),
)
},
onError = {},
Expand All @@ -103,7 +103,7 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(

statusRecyclerView.adapter = statusAdapter

viewModel.setProviderIds(providerType to providerTypeId)
viewModel.setProviderIdentifier(providerIdentifier)

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down Expand Up @@ -201,20 +201,16 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
companion object {
private val LOG_TAG = ProviderInformationBottomSheetDialogFragment::class.simpleName!!

private const val ARG_PROVIDER_TYPE = "provider_type"
private const val ARG_PROVIDER_TYPE_ID = "provider_type_id"
private const val ARG_PROVIDER_IDENTIFIER = "provider_identifier"

/**
* Create a [Bundle] to use as the arguments for this fragment.
* @param providerType The [ProviderType] of the provider to manage
* @param providerTypeId The type specific ID of the provider to manage
* @param providerIdentifier The [ProviderIdentifier] of the provider to manage
*/
fun createBundle(
providerType: ProviderType,
providerTypeId: Long,
providerIdentifier: ProviderIdentifier,
) = bundleOf(
ARG_PROVIDER_TYPE to providerType,
ARG_PROVIDER_TYPE_ID to providerTypeId,
ARG_PROVIDER_IDENTIFIER to providerIdentifier,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ProviderSelectorDialogFragment : MaterialDialogFragment(
view.trailingView?.setOnClickListener {
findNavController().navigateSafe(
R.id.action_providerSelectorDialogFragment_to_fragment_provider_information_bottom_sheet_dialog,
ManageProviderFragment.createBundle(item.type, item.typeId),
ManageProviderFragment.createBundle(providerIdentifier = item),
NavOptions.Builder()
.setPopUpTo(R.id.mainFragment, false)
.build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.lineageos.twelve.models.RequestStatus
@OptIn(ExperimentalCoroutinesApi::class)
class ManageProviderViewModel(application: Application) : ProviderViewModel(application) {
/**
* The user defined provider type. The one in [providerIds] will always take
* The user defined provider type. The one in [providerIdentifier] will always take
* precedence over this.
*/
private val _selectedProviderType = MutableStateFlow<ProviderType?>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
Expand All @@ -20,27 +21,16 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import org.lineageos.twelve.datasources.MediaError
import org.lineageos.twelve.models.ProviderIdentifier
import org.lineageos.twelve.models.ProviderType
import org.lineageos.twelve.models.RequestStatus
import org.lineageos.twelve.models.RequestStatus.Companion.fold

open class ProviderViewModel(application: Application) : TwelveViewModel(application) {
private val _providerIdentifier = MutableStateFlow<ProviderIdentifier?>(null)

/**
* The provider identifiers to manage.
*/
private val providerIds = MutableStateFlow<Pair<ProviderType, Long>?>(null)

@OptIn(ExperimentalCoroutinesApi::class)
protected val providerIdentifier = providerIds
.mapLatest {
it?.let { ProviderIdentifier(it.first, it.second) }
}
.flowOn(Dispatchers.IO)
.stateIn(
viewModelScope,
started = SharingStarted.Eagerly,
initialValue = null
)
protected val providerIdentifier = _providerIdentifier.asStateFlow()

@OptIn(ExperimentalCoroutinesApi::class)
val provider = providerIdentifier
Expand Down Expand Up @@ -99,8 +89,8 @@ open class ProviderViewModel(application: Application) : TwelveViewModel(applica
RequestStatus.Loading()
)

fun setProviderIds(providerIds: Pair<ProviderType, Long>?) {
this.providerIds.value = providerIds
fun setProviderIdentifier(providerIdentifier: ProviderIdentifier?) {
_providerIdentifier.value = providerIdentifier
}

/**
Expand Down

0 comments on commit f010219

Please sign in to comment.