Skip to content

Commit f010219

Browse files
committed
Twelve: Use ProviderIdentifier for all fragment arguments
Change-Id: If159248edd7a8cb0f55f1d553fed210ae30fe65d
1 parent 59cea6d commit f010219

File tree

6 files changed

+34
-52
lines changed

6 files changed

+34
-52
lines changed

app/src/main/java/org/lineageos/twelve/fragments/MainFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class MainFragment : Fragment(R.layout.fragment_main) {
310310
providersViewModel.navigationProvider.value?.let {
311311
findNavController().navigateSafe(
312312
R.id.action_mainFragment_to_fragment_provider_information_bottom_sheet_dialog,
313-
ManageProviderFragment.createBundle(it.type, it.typeId),
313+
ManageProviderFragment.createBundle(providerIdentifier = it),
314314
)
315315
true
316316
} ?: false

app/src/main/java/org/lineageos/twelve/fragments/ManageProviderFragment.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import kotlinx.coroutines.flow.collectLatest
3030
import kotlinx.coroutines.launch
3131
import org.lineageos.twelve.R
3232
import org.lineageos.twelve.datasources.MediaError
33+
import org.lineageos.twelve.ext.getParcelable
3334
import org.lineageos.twelve.ext.getSerializable
3435
import org.lineageos.twelve.ext.getViewProperty
3536
import org.lineageos.twelve.ext.selectItem
3637
import org.lineageos.twelve.models.ProviderArgument
3738
import org.lineageos.twelve.models.ProviderArgument.Companion.validateArgument
39+
import org.lineageos.twelve.models.ProviderIdentifier
3840
import org.lineageos.twelve.models.ProviderType
3941
import org.lineageos.twelve.models.RequestStatus
4042
import org.lineageos.twelve.ui.recyclerview.SimpleListAdapter
@@ -59,10 +61,10 @@ class ManageProviderFragment : Fragment(R.layout.fragment_manage_provider) {
5961
private val toolbar by getViewProperty<MaterialToolbar>(R.id.toolbar)
6062

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

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

130-
viewModel.setProviderIds(
131-
providerType?.let { providerType ->
132-
providerTypeId?.let {
133-
providerType to it
134-
}
135-
}
136-
)
132+
viewModel.setProviderIdentifier(providerIdentifier)
137133

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

339+
private const val ARG_PROVIDER_IDENTIFIER = "provider_identifier"
343340
private const val ARG_PROVIDER_TYPE = "provider_type"
344-
private const val ARG_PROVIDER_TYPE_ID = "provider_type_id"
345341

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

360356
/**
361357
* Create a [Bundle] to use as the arguments for this fragment.
362-
* @param providerType A [ProviderType] to either use as an hint for the creation of a new
363-
* instance or the type of the provider to edit or delete
364-
* @param providerTypeId The type specific ID of the provider to edit or delete
358+
* @param providerIdentifier The identifier of the provider to edit or delete
359+
* @param providerType A [ProviderType] to use as an hint for the creation of a new
360+
* instance, ignored when [providerIdentifier] is provided
365361
*/
366362
fun createBundle(
363+
providerIdentifier: ProviderIdentifier? = null,
367364
providerType: ProviderType? = null,
368-
providerTypeId: Long? = null,
369365
) = bundleOf(
366+
ARG_PROVIDER_IDENTIFIER to providerIdentifier,
370367
ARG_PROVIDER_TYPE to providerType,
371-
ARG_PROVIDER_TYPE_ID to providerTypeId,
372368
)
373369
}
374370
}

app/src/main/java/org/lineageos/twelve/fragments/ProviderInformationBottomSheetDialogFragment.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import kotlinx.coroutines.CoroutineScope
2727
import kotlinx.coroutines.flow.collectLatest
2828
import kotlinx.coroutines.launch
2929
import org.lineageos.twelve.R
30-
import org.lineageos.twelve.ext.getSerializable
30+
import org.lineageos.twelve.ext.getParcelable
3131
import org.lineageos.twelve.ext.getViewProperty
3232
import org.lineageos.twelve.ext.navigateSafe
3333
import org.lineageos.twelve.models.DataSourceInformation
34-
import org.lineageos.twelve.models.ProviderType
34+
import org.lineageos.twelve.models.ProviderIdentifier
3535
import org.lineageos.twelve.models.RequestStatus
3636
import org.lineageos.twelve.models.RequestStatus.Companion.fold
3737
import org.lineageos.twelve.ui.recyclerview.SimpleListAdapter
@@ -74,12 +74,10 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
7474
}
7575

7676
// Arguments
77-
private val providerType: ProviderType
78-
get() = requireArguments().getSerializable(ARG_PROVIDER_TYPE, ProviderType::class)!!
79-
private val providerTypeId: Long
80-
get() = requireArguments().getLong(ARG_PROVIDER_TYPE_ID, -1L).takeIf {
81-
it != -1L
82-
}!!
77+
private val providerIdentifier: ProviderIdentifier
78+
get() = requireArguments().getParcelable(
79+
ARG_PROVIDER_IDENTIFIER, ProviderIdentifier::class
80+
)!!
8381

8482
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
8583
super.onViewCreated(view, savedInstanceState)
@@ -90,7 +88,9 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
9088
onSuccess = {
9189
findNavController().navigateSafe(
9290
R.id.action_providerInformationBottomSheetDialogFragment_to_fragment_manage_provider,
93-
ManageProviderFragment.createBundle(providerType, providerTypeId),
91+
ManageProviderFragment.createBundle(
92+
providerIdentifier = providerIdentifier
93+
),
9494
)
9595
},
9696
onError = {},
@@ -103,7 +103,7 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
103103

104104
statusRecyclerView.adapter = statusAdapter
105105

106-
viewModel.setProviderIds(providerType to providerTypeId)
106+
viewModel.setProviderIdentifier(providerIdentifier)
107107

108108
viewLifecycleOwner.lifecycleScope.launch {
109109
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
@@ -201,20 +201,16 @@ class ProviderInformationBottomSheetDialogFragment : BottomSheetDialogFragment(
201201
companion object {
202202
private val LOG_TAG = ProviderInformationBottomSheetDialogFragment::class.simpleName!!
203203

204-
private const val ARG_PROVIDER_TYPE = "provider_type"
205-
private const val ARG_PROVIDER_TYPE_ID = "provider_type_id"
204+
private const val ARG_PROVIDER_IDENTIFIER = "provider_identifier"
206205

207206
/**
208207
* Create a [Bundle] to use as the arguments for this fragment.
209-
* @param providerType The [ProviderType] of the provider to manage
210-
* @param providerTypeId The type specific ID of the provider to manage
208+
* @param providerIdentifier The [ProviderIdentifier] of the provider to manage
211209
*/
212210
fun createBundle(
213-
providerType: ProviderType,
214-
providerTypeId: Long,
211+
providerIdentifier: ProviderIdentifier,
215212
) = bundleOf(
216-
ARG_PROVIDER_TYPE to providerType,
217-
ARG_PROVIDER_TYPE_ID to providerTypeId,
213+
ARG_PROVIDER_IDENTIFIER to providerIdentifier,
218214
)
219215
}
220216
}

app/src/main/java/org/lineageos/twelve/fragments/ProviderSelectorDialogFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ProviderSelectorDialogFragment : MaterialDialogFragment(
5959
view.trailingView?.setOnClickListener {
6060
findNavController().navigateSafe(
6161
R.id.action_providerSelectorDialogFragment_to_fragment_provider_information_bottom_sheet_dialog,
62-
ManageProviderFragment.createBundle(item.type, item.typeId),
62+
ManageProviderFragment.createBundle(providerIdentifier = item),
6363
NavOptions.Builder()
6464
.setPopUpTo(R.id.mainFragment, false)
6565
.build(),

app/src/main/java/org/lineageos/twelve/viewmodels/ManageProviderViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.lineageos.twelve.models.RequestStatus
2525
@OptIn(ExperimentalCoroutinesApi::class)
2626
class ManageProviderViewModel(application: Application) : ProviderViewModel(application) {
2727
/**
28-
* The user defined provider type. The one in [providerIds] will always take
28+
* The user defined provider type. The one in [providerIdentifier] will always take
2929
* precedence over this.
3030
*/
3131
private val _selectedProviderType = MutableStateFlow<ProviderType?>(null)

app/src/main/java/org/lineageos/twelve/viewmodels/ProviderViewModel.kt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import kotlinx.coroutines.flow.MutableStateFlow
1313
import kotlinx.coroutines.flow.SharingStarted
14+
import kotlinx.coroutines.flow.asStateFlow
1415
import kotlinx.coroutines.flow.filterNotNull
1516
import kotlinx.coroutines.flow.flatMapLatest
1617
import kotlinx.coroutines.flow.flowOf
@@ -20,27 +21,16 @@ import kotlinx.coroutines.flow.stateIn
2021
import kotlinx.coroutines.withContext
2122
import org.lineageos.twelve.datasources.MediaError
2223
import org.lineageos.twelve.models.ProviderIdentifier
23-
import org.lineageos.twelve.models.ProviderType
2424
import org.lineageos.twelve.models.RequestStatus
2525
import org.lineageos.twelve.models.RequestStatus.Companion.fold
2626

2727
open class ProviderViewModel(application: Application) : TwelveViewModel(application) {
28+
private val _providerIdentifier = MutableStateFlow<ProviderIdentifier?>(null)
29+
2830
/**
2931
* The provider identifiers to manage.
3032
*/
31-
private val providerIds = MutableStateFlow<Pair<ProviderType, Long>?>(null)
32-
33-
@OptIn(ExperimentalCoroutinesApi::class)
34-
protected val providerIdentifier = providerIds
35-
.mapLatest {
36-
it?.let { ProviderIdentifier(it.first, it.second) }
37-
}
38-
.flowOn(Dispatchers.IO)
39-
.stateIn(
40-
viewModelScope,
41-
started = SharingStarted.Eagerly,
42-
initialValue = null
43-
)
33+
protected val providerIdentifier = _providerIdentifier.asStateFlow()
4434

4535
@OptIn(ExperimentalCoroutinesApi::class)
4636
val provider = providerIdentifier
@@ -99,8 +89,8 @@ open class ProviderViewModel(application: Application) : TwelveViewModel(applica
9989
RequestStatus.Loading()
10090
)
10191

102-
fun setProviderIds(providerIds: Pair<ProviderType, Long>?) {
103-
this.providerIds.value = providerIds
92+
fun setProviderIdentifier(providerIdentifier: ProviderIdentifier?) {
93+
_providerIdentifier.value = providerIdentifier
10494
}
10595

10696
/**

0 commit comments

Comments
 (0)