Migrate remaining deprecated onActivityResult code.

This commit is contained in:
casey langen 2022-07-23 20:14:12 -07:00
parent ec9aafe0df
commit 803e3e7ae1
12 changed files with 57 additions and 118 deletions

View File

@ -1,6 +1,5 @@
package io.casey.musikcube.remote.framework
import android.content.Intent
import android.os.Bundle
interface IMixin {
@ -10,6 +9,5 @@ interface IMixin {
fun onPause()
fun onStop()
fun onSaveInstanceState(bundle: Bundle)
fun onActivityResult(request: Int, result: Int, data: Intent?)
fun onDestroy()
}

View File

@ -1,9 +1,8 @@
package io.casey.musikcube.remote.framework
import android.content.Intent
import android.os.Bundle
abstract class MixinBase : IMixin {
abstract class MixinBase: IMixin {
enum class State {
Unknown, Created, Started, Resumed, Paused, Stopped, Destroyed
}
@ -34,9 +33,6 @@ abstract class MixinBase : IMixin {
state = State.Stopped
}
override fun onActivityResult(request: Int, result: Int, data: Intent?) {
}
override fun onSaveInstanceState(bundle: Bundle) {
}

View File

@ -1,12 +1,8 @@
package io.casey.musikcube.remote.framework
import android.content.Intent
import android.os.Bundle
class MixinSet : MixinBase() {
private data class ActivityResult (val request: Int, val result: Int, val data: Intent?)
private var activityResult: ActivityResult? = null
class MixinSet: MixinBase() {
private val components: MutableMap<Class<out IMixin>, IMixin> = mutableMapOf()
private var bundle = Bundle()
@ -54,12 +50,6 @@ class MixinSet : MixinBase() {
override fun onResume() {
super.onResume()
components.values.forEach { it.onResume() }
val ar = activityResult
if (ar != null) {
components.values.forEach { it.onActivityResult(ar.request, ar.result, ar.data) }
activityResult = null
}
}
override fun onPause() {
@ -77,16 +67,6 @@ class MixinSet : MixinBase() {
components.values.forEach { it.onSaveInstanceState(bundle) }
}
override fun onActivityResult(request: Int, result: Int, data: Intent?) {
super.onActivityResult(request, result, data)
if (active) {
components.values.forEach { it.onActivityResult(request, result, data) }
}
else {
activityResult = ActivityResult(request, result, data)
}
}
override fun onDestroy() {
super.onDestroy()
components.values.forEach { it.onDestroy() }

View File

@ -2,6 +2,7 @@ package io.casey.musikcube.remote.ui.navigation
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
@ -21,7 +22,10 @@ import io.casey.musikcube.remote.ui.download.activity.TrackDownloadActivity
import io.casey.musikcube.remote.ui.home.activity.MainActivity
import io.casey.musikcube.remote.ui.playqueue.activity.PlayQueueActivity
import io.casey.musikcube.remote.ui.playqueue.fragment.PlayQueueFragment
import io.casey.musikcube.remote.ui.shared.extension.*
import io.casey.musikcube.remote.ui.shared.extension.pushContainerId
import io.casey.musikcube.remote.ui.shared.extension.pushWithToolbar
import io.casey.musikcube.remote.ui.shared.extension.withTransitionType
import io.casey.musikcube.remote.ui.shared.extension.withoutTransport
import io.casey.musikcube.remote.ui.shared.fragment.BaseFragment
import io.casey.musikcube.remote.ui.tracks.activity.EditPlaylistActivity
import io.casey.musikcube.remote.ui.tracks.activity.TrackListActivity
@ -146,28 +150,22 @@ object Navigate {
*
*/
fun toPlaylistChooser(requestCode: Int,
activity: AppCompatActivity,
fragment: BaseFragment? = null) =
CategoryBrowseActivity.getStartIntent(
fun toPlaylistChooser(launcher: ActivityResultLauncher<Intent>,
activity: AppCompatActivity) =
launcher.launch(
CategoryBrowseActivity.getStartIntent(
activity,
Metadata.Category.PLAYLISTS,
NavigationType.Select,
activity.getString(R.string.playlist_edit_pick_playlist))
.withoutTransport()
.withTransitionType(Transition.Vertical)
.apply {
startActivityForResult(this, requestCode, activity, fragment)
}
.withTransitionType(Transition.Vertical))
fun toPlaylistEditor(requestCode: Int,
fun toPlaylistEditor(launcher: ActivityResultLauncher<Intent>,
playlistName: String,
playlistId: Long,
activity: AppCompatActivity,
fragment: BaseFragment? = null) =
EditPlaylistActivity.getStartIntent(activity, playlistName, playlistId).apply {
startActivityForResult(this, requestCode, activity, fragment)
}
activity: AppCompatActivity) =
launcher.launch(EditPlaylistActivity.getStartIntent(activity, playlistName, playlistId))
/*
*

View File

@ -1,7 +1,6 @@
package io.casey.musikcube.remote.ui.shared.activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.media.AudioManager
import android.os.Bundle
@ -84,11 +83,6 @@ abstract class BaseActivity: AppCompatActivity(), ViewModel.Provider, Runner.Tas
mixins.onStop()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
mixins.onActivityResult(requestCode, resultCode, data)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mixins.onSaveInstanceState(outState)

View File

@ -1,14 +1,15 @@
package io.casey.musikcube.remote.util
import androidx.activity.ComponentActivity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import java.io.Serializable
@RequiresApi(33)
@ -43,3 +44,6 @@ inline fun <reified T: Serializable> Bundle.getSerializableCompat(name: String):
fun ComponentActivity.launcher(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> =
this.registerForActivityResult(ActivityResultContracts.StartActivityForResult(), callback)
fun Fragment.launcher(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> =
this.registerForActivityResult(ActivityResultContracts.StartActivityForResult(), callback)

View File

@ -531,16 +531,3 @@ fun fallback(input: String?, fallback: String): String =
fun fallback(input: String?, fallback: Int): String =
if (input.isNullOrEmpty()) Application.instance.getString(fallback) else input
fun startActivityForResult(intent: Intent,
requestCode: Int,
activity: AppCompatActivity?,
fragment: BaseFragment? = null) =
when {
fragment != null ->
fragment.startActivityForResult(intent, requestCode)
activity != null ->
activity.startActivityForResult(intent, requestCode)
else ->
throw IllegalArgumentException("")
}

View File

@ -1,6 +1,5 @@
package io.casey.musikcube.remote.ui.shared.fragment
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
@ -37,11 +36,6 @@ open class BaseDialogFragment: DialogFragment(), ViewModel.Provider {
mixins.onStop()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
mixins.onActivityResult(requestCode, resultCode, data)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mixins.onSaveInstanceState(outState)

View File

@ -1,7 +1,6 @@
package io.casey.musikcube.remote.ui.shared.fragment
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.os.Handler
@ -113,11 +112,6 @@ open class BaseFragment: Fragment(), ViewModel.Provider, IBackHandler {
mixins.onStop()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
mixins.onActivityResult(requestCode, resultCode, data)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mixins.onSaveInstanceState(outState)

View File

@ -9,6 +9,9 @@ import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.PopupMenu
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import io.casey.musikcube.remote.Application
@ -31,6 +34,7 @@ import io.casey.musikcube.remote.ui.shared.fragment.BaseDialogFragment
import io.casey.musikcube.remote.ui.shared.fragment.BaseFragment
import io.casey.musikcube.remote.ui.tracks.activity.EditPlaylistActivity
import io.casey.musikcube.remote.util.getSerializableCompat
import io.casey.musikcube.remote.util.launcher
import io.reactivex.Observable
import io.reactivex.rxkotlin.subscribeBy
import java.io.Serializable
@ -60,50 +64,48 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity,
override fun onCreate(bundle: Bundle) {
super.onCreate(bundle)
provider.attach()
ConfirmDeletePlaylistDialog.rebind(activity, this)
EnterPlaylistNameDialog.rebind(activity, this)
ConfirmRemoveFromPlaylistDialog.rebind(activity, this)
}
override fun onResume() {
super.onResume()
provider.attach()
}
override fun onPause() {
super.onPause()
provider.detach()
}
override fun onDestroy() {
super.onDestroy()
provider.destroy()
}
override fun onActivityResult(request: Int, result: Int, data: Intent?) {
if (pendingCode == request) {
if (result == Activity.RESULT_OK && data != null) {
val playlistId = data.getLongExtra(Category.Extra.ID, -1L)
val playlistName = data.getStringExtra(Category.Extra.NAME) ?: ""
if (playlistId != -1L) {
completion?.invoke(playlistId, playlistName)
}
}
pendingCode = -1
completion = null
private fun launcher(callback: ActivityResultCallback<ActivityResult>): ActivityResultLauncher<Intent> =
when (fragment) {
null -> activity.launcher(callback)
else -> fragment.launcher(callback)
}
else if (result == Activity.RESULT_OK && request == REQUEST_EDIT_PLAYLIST && data != null) {
private val editPlaylistLauncher = launcher { result ->
val data = result.data
if (result.resultCode == Activity.RESULT_OK && data != null) {
val playlistName = data.getStringExtra(EditPlaylistActivity.EXTRA_PLAYLIST_NAME) ?: ""
val playlistId = data.getLongExtra(EditPlaylistActivity.EXTRA_PLAYLIST_ID, -1L)
showSnackbar(
activity.findViewById(android.R.id.content),
activity.getString(R.string.playlist_edit_add_success, playlistName),
activity.getString(R.string.button_view),
viewPlaylist(playlistId, playlistName))
}
super.onActivityResult(request, result, data)
}
}
private val choosePlaylistLauncher = launcher { result ->
val data = result.data
if (result.resultCode == Activity.RESULT_OK && data != null) {
val playlistId = data.getLongExtra(Category.Extra.ID, -1L)
val playlistName = data.getStringExtra(Category.Extra.NAME) ?: ""
if (playlistId != -1L) {
completion?.invoke(playlistId, playlistName)
}
}
pendingCode = -1
completion = null
}
fun createPlaylist() =
@ -187,7 +189,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity,
private fun showPlaylistChooser(callback: (Long, String) -> Unit) {
completion = callback
pendingCode = REQUEST_ADD_TO_PLAYLIST
Navigate.toPlaylistChooser(pendingCode, activity, fragment)
Navigate.toPlaylistChooser(choosePlaylistLauncher, activity)
}
fun showForTrack(track: ITrack, anchorView: View) {
@ -251,7 +253,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity,
ConfirmDeletePlaylistDialog.show(activity, this, playlistName, playlistId)
}
R.id.menu_playlist_edit -> {
Navigate.toPlaylistEditor(REQUEST_EDIT_PLAYLIST, playlistName, playlistId, activity, fragment)
Navigate.toPlaylistEditor(editPlaylistLauncher, playlistName, playlistId, activity)
}
R.id.menu_playlist_rename -> {
EnterPlaylistNameDialog.showForRename(activity, this, playlistName, playlistId)

View File

@ -18,18 +18,10 @@ class MetadataProxyMixin : MixinBase() {
DaggerViewComponent.builder()
.appComponent(Application.appComponent)
.build().inject(this)
}
override fun onResume() {
super.onResume()
provider.attach()
}
override fun onPause() {
super.onPause()
provider.detach()
}
override fun onDestroy() {
super.onDestroy()
provider.destroy()

View File

@ -29,6 +29,7 @@ import io.casey.musikcube.remote.ui.tracks.activity.TrackListActivity
import io.casey.musikcube.remote.ui.tracks.adapter.TrackListAdapter
import io.casey.musikcube.remote.ui.tracks.constant.Track
import io.casey.musikcube.remote.util.Debouncer
import io.casey.musikcube.remote.util.launcher
import io.reactivex.Observable
import io.reactivex.rxkotlin.subscribeBy
@ -52,8 +53,6 @@ class TrackListFragment: BaseFragment(), IFilterable, ITitleProvider, ITransport
data = mixin(MetadataProxyMixin())
playback = mixin(PlaybackMixin())
super.onCreate(savedInstanceState)
extras.run {
categoryType = getString(Track.Extra.CATEGORY_TYPE, "")
categoryId = getLong(Track.Extra.SELECTED_ID, 0)
@ -66,6 +65,8 @@ class TrackListFragment: BaseFragment(), IFilterable, ITitleProvider, ITransport
mixin(ItemContextMenuMixin(appCompatActivity, menuListener, this))
queryFactory = createCategoryQueryFactory(categoryType, categoryId)
super.onCreate(savedInstanceState)
}
override fun onPause() {
@ -118,8 +119,9 @@ class TrackListFragment: BaseFragment(), IFilterable, ITitleProvider, ITransport
tracks.setOnMetadataLoadedListener(slidingWindowListener)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == Track.RequestCode.EDIT_PLAYLIST && resultCode == AppCompatActivity.RESULT_OK && data != null) {
private val editPlaylistLauncher = launcher { result ->
val data = result.data
if (result.resultCode == AppCompatActivity.RESULT_OK && data != null) {
val playlistName = data.getStringExtra(EditPlaylistActivity.EXTRA_PLAYLIST_NAME) ?: ""
val playlistId = data.getLongExtra(EditPlaylistActivity.EXTRA_PLAYLIST_ID, -1L)
@ -134,7 +136,6 @@ class TrackListFragment: BaseFragment(), IFilterable, ITitleProvider, ITransport
})
}
}
super.onActivityResult(requestCode, resultCode, data)
}
override val title: String
@ -159,12 +160,11 @@ class TrackListFragment: BaseFragment(), IFilterable, ITitleProvider, ITransport
override fun optionsItemSelected(menuItem: MenuItem): Boolean =
when (menuItem.itemId == R.id.action_edit) {
true -> {
appCompatActivity.startActivityForResult(
editPlaylistLauncher.launch(
EditPlaylistActivity.getStartIntent(
appCompatActivity,
categoryValue,
categoryId),
Track.RequestCode.EDIT_PLAYLIST)
categoryId))
true
}
else -> false