Fix onActivityResult deprecation in SubscriptionFragment

This commit is contained in:
TacoTheDank 2021-06-15 22:09:19 -04:00
parent cae160b5be
commit a1432e939f

View file

@ -15,6 +15,8 @@ import android.view.MenuInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.localbroadcastmanager.content.LocalBroadcastManager
@ -82,6 +84,11 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem private lateinit var feedGroupsSortMenuItem: HeaderWithMenuItem
private val subscriptionsSection = Section() private val subscriptionsSection = Section()
private val requestExportLauncher =
registerForActivityResult(StartActivityForResult(), this::requestExportResult)
private val requestImportLauncher =
registerForActivityResult(StartActivityForResult(), this::requestImportResult)
@State @State
@JvmField @JvmField
var itemsListState: Parcelable? = null var itemsListState: Parcelable? = null
@ -184,16 +191,15 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
} }
private fun onImportPreviousSelected() { private fun onImportPreviousSelected() {
startActivityForResult(StoredFileHelper.getPicker(activity), REQUEST_IMPORT_CODE) requestImportLauncher.launch(StoredFileHelper.getPicker(activity))
} }
private fun onExportSelected() { private fun onExportSelected() {
val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date()) val date = SimpleDateFormat("yyyyMMddHHmm", Locale.ENGLISH).format(Date())
val exportName = "newpipe_subscriptions_$date.json" val exportName = "newpipe_subscriptions_$date.json"
startActivityForResult( requestExportLauncher.launch(
StoredFileHelper.getNewPicker(activity, exportName, "application/json", null), StoredFileHelper.getNewPicker(activity, exportName, "application/json", null)
REQUEST_EXPORT_CODE
) )
} }
@ -201,22 +207,23 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
FeedGroupReorderDialog().show(parentFragmentManager, null) FeedGroupReorderDialog().show(parentFragmentManager, null)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { fun requestExportResult(result: ActivityResult) {
super.onActivityResult(requestCode, resultCode, data) if (result.data != null && result.resultCode == Activity.RESULT_OK) {
if (data != null && data.data != null && resultCode == Activity.RESULT_OK) { activity.startService(
if (requestCode == REQUEST_EXPORT_CODE) { Intent(activity, SubscriptionsExportService::class.java)
activity.startService( .putExtra(SubscriptionsExportService.KEY_FILE_PATH, result.data?.data)
Intent(activity, SubscriptionsExportService::class.java) )
.putExtra(SubscriptionsExportService.KEY_FILE_PATH, data.data) }
) }
} else if (requestCode == REQUEST_IMPORT_CODE) {
ImportConfirmationDialog.show( fun requestImportResult(result: ActivityResult) {
this, if (result.data != null && result.resultCode == Activity.RESULT_OK) {
Intent(activity, SubscriptionsImportService::class.java) ImportConfirmationDialog.show(
.putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE) this,
.putExtra(KEY_VALUE, data.data) Intent(activity, SubscriptionsImportService::class.java)
) .putExtra(KEY_MODE, PREVIOUS_EXPORT_MODE)
} .putExtra(KEY_VALUE, result.data?.data)
)
} }
} }
@ -436,9 +443,4 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
val minWidth = resources.getDimensionPixelSize(R.dimen.channel_item_grid_min_width) val minWidth = resources.getDimensionPixelSize(R.dimen.channel_item_grid_min_width)
return max(1, floor(resources.displayMetrics.widthPixels / minWidth.toDouble()).toInt()) return max(1, floor(resources.displayMetrics.widthPixels / minWidth.toDouble()).toInt())
} }
companion object {
private const val REQUEST_EXPORT_CODE = 666
private const val REQUEST_IMPORT_CODE = 667
}
} }