diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt index 64bf5e1dd..f55e14669 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/settings/activity/SettingsActivity.kt @@ -10,7 +10,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.* -import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.fragment.app.DialogFragment import com.uacf.taskrunner.Task @@ -28,6 +27,7 @@ import io.casey.musikcube.remote.ui.shared.mixin.MetadataProxyMixin import io.casey.musikcube.remote.ui.shared.mixin.PlaybackMixin import io.casey.musikcube.remote.util.getParcelableCompat import io.casey.musikcube.remote.util.getParcelableExtraCompat +import io.casey.musikcube.remote.util.launcher import java.util.* import javax.inject.Inject import io.casey.musikcube.remote.ui.settings.constants.Prefs.Default as Defaults @@ -215,7 +215,7 @@ class SettingsActivity : BaseActivity() { } } - private val connectionsActivityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult -> + private val connectionsActivityLauncher = launcher { activityResult -> if (activityResult.resultCode == RESULT_OK && activityResult.data != null) { activityResult.data?.let { data -> val connection = data.getParcelableExtraCompat(ConnectionsActivity.EXTRA_SELECTED_CONNECTION) @@ -227,7 +227,6 @@ class SettingsActivity : BaseActivity() { sslCheckbox.setCheckWithoutEvent(connection.ssl, sslCheckChanged) certCheckbox.setCheckWithoutEvent(connection.noValidate, certValidationChanged) } - } } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Compat.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Compat.kt new file mode 100644 index 000000000..1b16d2108 --- /dev/null +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Compat.kt @@ -0,0 +1,45 @@ +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.result.ActivityResult +import androidx.activity.result.ActivityResultCallback +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.RequiresApi +import java.io.Serializable + +@RequiresApi(33) +@Suppress("deprecation") +inline fun Intent.getParcelableExtraCompat(name: String): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + this.getParcelableExtra(name) + } + else { + this.getParcelableExtra(name, T::class.java) + } + +@RequiresApi(33) +@Suppress("deprecation") +inline fun Bundle.getParcelableCompat(name: String): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + this.getParcelable(name) + } + else { + this.getParcelable(name, T::class.java) + } + +@RequiresApi(33) +@Suppress("deprecation") +inline fun Bundle.getSerializableCompat(name: String): T? = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + this.getSerializable(name) as T? + } + else { + this.getSerializable(name, T::class.java) + } + +fun ComponentActivity.launcher(callback: ActivityResultCallback): ActivityResultLauncher = + this.registerForActivityResult(ActivityResultContracts.StartActivityForResult(), callback) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt index 6a8f795fa..7789f9a4f 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/mixin/ItemContextMenuMixin.kt @@ -1,5 +1,6 @@ package io.casey.musikcube.remote.ui.shared.mixin +import android.annotation.SuppressLint import android.app.Activity import android.app.Dialog import android.content.DialogInterface @@ -29,8 +30,10 @@ import io.casey.musikcube.remote.ui.shared.extension.showSnackbar 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.reactivex.Observable import io.reactivex.rxkotlin.subscribeBy +import java.io.Serializable import javax.inject.Inject class ItemContextMenuMixin(private val activity: AppCompatActivity, @@ -106,6 +109,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, fun createPlaylist() = EnterPlaylistNameDialog.showForCreate(activity, this) + @SuppressLint("CheckResult") fun createPlaylist(playlistName: String) { @Suppress("unused") provider.createPlaylist(playlistName).subscribeBy( @@ -123,8 +127,8 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, }) } + @SuppressLint("CheckResult") fun renamePlaylist(newName: String, id: Long) { - @Suppress("unused") provider.renamePlaylist(id, newName).subscribeBy( onNext = { success -> if (success) { @@ -159,10 +163,10 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, Navigate.toTracks(Metadata.Category.PLAYLISTS, playlistId, playlistName, activity, fragment) } + @SuppressLint("CheckResult") private fun addWithErrorHandler(playlistId: Long, playlistName: String, observable: Observable) { val error = R.string.playlist_edit_add_error - @Suppress("unused") observable.subscribeBy( onNext = { success -> if (success) { @@ -340,8 +344,8 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, } } + @SuppressLint("CheckResult") private fun deletePlaylistConfirmed(playlistId: Long, playlistName: String) { - @Suppress("unused") if (playlistId != -1L) { provider.deletePlaylist(playlistId).subscribeBy( onNext = { success -> @@ -359,8 +363,8 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, } } + @SuppressLint("CheckResult") private fun removeFromPlaylistConfirmed(playlistId: Long, playlistName: String, externalId: String, position: Int) { - @Suppress("unused") provider .removeTracksFromPlaylist(playlistId, listOf(externalId), listOf(position)) .subscribeBy( @@ -392,7 +396,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, val trackExternalId = args.getString(EXTRA_TRACK_EXTERNAL_ID, "") val trackPosition = args.getInt(EXTRA_TRACK_POSITION, -1) - return AlertDialog.Builder(activity!!) + return AlertDialog.Builder(requireActivity()) .setTitle(R.string.playlist_confirm_delete_title) .setMessage(getString(R.string.playlist_confirm_delete_message, trackTitle)) .setNegativeButton(R.string.button_no, null) @@ -440,7 +444,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, val args = this.arguments ?: Bundle() val playlistName = args.getString(EXTRA_PLAYLIST_NAME, "") ?: "" - val dlg = AlertDialog.Builder(activity!!) + val dlg = AlertDialog.Builder(requireActivity()) .setTitle(R.string.playlist_confirm_delete_title) .setMessage(getString(R.string.playlist_confirm_delete_message, playlistName)) .setNegativeButton(R.string.button_no, null) @@ -485,7 +489,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val args = arguments ?: Bundle() val editText = EditText(activity) - val action = args.getSerializable(EXTRA_ACTION) + val action = args.getSerializableCompat(EXTRA_ACTION) val name = args.getString(EXTRA_NAME, "") val id = args.getLong(EXTRA_ID, -1) @@ -499,7 +503,7 @@ class ItemContextMenuMixin(private val activity: AppCompatActivity, editText.requestFocus() - val activity = this.activity!! + val activity = requireActivity() val dlg = AlertDialog.Builder(activity) .setTitle(R.string.playlist_name_title) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt index 726738725..9876fdff7 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt @@ -116,16 +116,16 @@ class EditPlaylistActivity: BaseActivity() { ItemTouchHelper.LEFT) { override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { - val from = viewHolder.adapterPosition - val to = target.adapterPosition + val from = viewHolder.bindingAdapterPosition + val to = target.bindingAdapterPosition viewModel.move(from, to) adapter.notifyItemMoved(from, to) return true } override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { - viewModel.remove(viewHolder.adapterPosition) - adapter.notifyItemRemoved(viewHolder.adapterPosition) + viewModel.remove(viewHolder.bindingAdapterPosition) + adapter.notifyItemRemoved(viewHolder.bindingAdapterPosition) } } diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt deleted file mode 100644 index bc72080c0..000000000 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/util/Extensions.kt +++ /dev/null @@ -1,26 +0,0 @@ -package io.casey.musikcube.remote.util - -import android.content.Intent -import android.os.Bundle -import android.os.Parcelable -import androidx.annotation.RequiresApi - -@RequiresApi(33) -@Suppress("deprecation") -inline fun Intent.getParcelableExtraCompat(name: String): T? = - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - this.getParcelableExtra(name) - } - else { - this.getParcelableExtra(name, T::class.java) - } - -@RequiresApi(33) -@Suppress("deprecation") -inline fun Bundle.getParcelableCompat(name: String): T? = - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { - this.getParcelable(name) - } - else { - this.getParcelable(name, T::class.java) - } \ No newline at end of file