11package org.schabi.newpipe.local.subscription
22
3- import android.app.Activity
43import android.content.Context
54import android.content.DialogInterface
65import android.os.Bundle
@@ -14,8 +13,6 @@ import android.view.View
1413import android.view.ViewGroup
1514import android.webkit.MimeTypeMap
1615import android.widget.Toast
17- import androidx.activity.result.ActivityResult
18- import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
1916import androidx.annotation.StringRes
2017import androidx.appcompat.app.AlertDialog
2118import androidx.lifecycle.ViewModelProvider
@@ -26,9 +23,6 @@ import com.xwray.groupie.GroupAdapter
2623import com.xwray.groupie.Section
2724import com.xwray.groupie.viewbinding.GroupieViewHolder
2825import io.reactivex.rxjava3.disposables.CompositeDisposable
29- import java.text.SimpleDateFormat
30- import java.util.Date
31- import java.util.Locale
3226import org.schabi.newpipe.R
3327import org.schabi.newpipe.database.feed.model.FeedGroupEntity.Companion.GROUP_ALL_ID
3428import org.schabi.newpipe.databinding.DialogTitleBinding
@@ -52,10 +46,6 @@ import org.schabi.newpipe.local.subscription.item.FeedGroupCarouselItem
5246import org.schabi.newpipe.local.subscription.item.GroupsHeader
5347import org.schabi.newpipe.local.subscription.item.Header
5448import org.schabi.newpipe.local.subscription.item.ImportSubscriptionsHintPlaceholderItem
55- import org.schabi.newpipe.local.subscription.workers.SubscriptionExportWorker
56- import org.schabi.newpipe.local.subscription.workers.SubscriptionImportInput
57- import org.schabi.newpipe.streams.io.NoFileManagerSafeGuard
58- import org.schabi.newpipe.streams.io.StoredFileHelper
5949import org.schabi.newpipe.util.NavigationHelper
6050import org.schabi.newpipe.util.OnClickGesture
6151import org.schabi.newpipe.util.ServiceHelper
@@ -68,6 +58,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
6858
6959 private lateinit var viewModel: SubscriptionViewModel
7060 private lateinit var subscriptionManager: SubscriptionManager
61+ private lateinit var importExportHelper: SubscriptionsImportExportHelper
7162 private val disposables: CompositeDisposable = CompositeDisposable ()
7263
7364 private val groupAdapter = GroupAdapter <GroupieViewHolder <FeedItemCarouselBinding >>()
@@ -76,11 +67,6 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
7667 private lateinit var feedGroupsSortMenuItem: GroupsHeader
7768 private val subscriptionsSection = Section ()
7869
79- private val requestExportLauncher =
80- registerForActivityResult(StartActivityForResult (), this ::requestExportResult)
81- private val requestImportLauncher =
82- registerForActivityResult(StartActivityForResult (), this ::requestImportResult)
83-
8470 @State
8571 @JvmField
8672 var itemsListState: Parcelable ? = null
@@ -100,6 +86,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
10086 override fun onAttach (context : Context ) {
10187 super .onAttach(context)
10288 subscriptionManager = SubscriptionManager (requireContext())
89+ importExportHelper = SubscriptionsImportExportHelper (this )
10390 }
10491
10592 override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View ? {
@@ -139,7 +126,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
139126 // -- Import --
140127 val importSubMenu = menu.addSubMenu(R .string.import_from)
141128
142- addMenuItemToSubmenu(importSubMenu, R .string.previous_export) { onImportPreviousSelected() }
129+ addMenuItemToSubmenu(importSubMenu, R .string.previous_export) { importExportHelper. onImportPreviousSelected() }
143130 .setIcon(R .drawable.ic_backup)
144131
145132 for (service in ServiceList .all()) {
@@ -157,7 +144,7 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
157144 // -- Export --
158145 val exportSubMenu = menu.addSubMenu(R .string.export_to)
159146
160- addMenuItemToSubmenu(exportSubMenu, R .string.file) { onExportSelected() }
147+ addMenuItemToSubmenu(exportSubMenu, R .string.file) { importExportHelper. onExportSelected() }
161148 .setIcon(R .drawable.ic_save)
162149 }
163150
@@ -193,48 +180,10 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
193180 NavigationHelper .openSubscriptionsImportFragment(fragmentManager, serviceId)
194181 }
195182
196- private fun onImportPreviousSelected () {
197- NoFileManagerSafeGuard .launchSafe(
198- requestImportLauncher,
199- StoredFileHelper .getPicker(activity, JSON_MIME_TYPE ),
200- TAG ,
201- requireContext()
202- )
203- }
204-
205- private fun onExportSelected () {
206- val date = SimpleDateFormat (" yyyyMMddHHmm" , Locale .ENGLISH ).format(Date ())
207- val exportName = " newpipe_subscriptions_$date .json"
208-
209- NoFileManagerSafeGuard .launchSafe(
210- requestExportLauncher,
211- StoredFileHelper .getNewPicker(activity, exportName, JSON_MIME_TYPE , null ),
212- TAG ,
213- requireContext()
214- )
215- }
216-
217183 private fun openReorderDialog () {
218184 FeedGroupReorderDialog ().show(parentFragmentManager, null )
219185 }
220186
221- private fun requestExportResult (result : ActivityResult ) {
222- val data = result.data?.data
223- if (data != null && result.resultCode == Activity .RESULT_OK ) {
224- SubscriptionExportWorker .schedule(activity, data)
225- }
226- }
227-
228- private fun requestImportResult (result : ActivityResult ) {
229- val data = result.data?.dataString
230- if (data != null && result.resultCode == Activity .RESULT_OK ) {
231- ImportConfirmationDialog .show(
232- this ,
233- SubscriptionImportInput .PreviousExportMode (data)
234- )
235- }
236- }
237-
238187 // ////////////////////////////////////////////////////////////////////////
239188 // Fragment Views
240189 // ////////////////////////////////////////////////////////////////////////
0 commit comments