From dd0ad19fb986a95453a2f6d140c25dfbd38fa598 Mon Sep 17 00:00:00 2001
From: Kelvin <kelvin@futo.org>
Date: Mon, 6 Nov 2023 14:25:09 +0100
Subject: [PATCH] NewLine subs import, fix no-recent video subscriptions

---
 app/src/main/AndroidManifest.xml              | 20 +++++++++++++++++++
 .../platformplayer/activities/MainActivity.kt | 20 +++++++++++++++++++
 .../main/ImportSubscriptionsFragment.kt       |  2 +-
 .../SmartSubscriptionAlgorithm.kt             | 10 +++++++---
 .../SubscriptionsTaskFetchAlgorithm.kt        |  2 +-
 app/src/main/res/values/strings.xml           |  1 +
 6 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 41aa8571..2fd04972 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -92,6 +92,26 @@
                 <data android:host="*" />
                 <data android:scheme="file" />
 
+                <data android:mimeType="text/plain" />
+            </intent-filter>
+            <intent-filter android:autoVerify="true">
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <action android:name="android.intent.action.SEND" />
+                <action android:name="android.intent.action.VIEW" />
+                <data android:host="*" />
+                <data android:scheme="content" />
+
+                <data android:mimeType="text/plain" />
+            </intent-filter>
+            <intent-filter android:autoVerify="true">
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <action android:name="android.intent.action.SEND" />
+                <action android:name="android.intent.action.VIEW" />
+                <data android:host="*" />
+                <data android:scheme="file" />
+
                 <data android:mimeType="application/zip" />
             </intent-filter>
             <intent-filter android:autoVerify="true">
diff --git a/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt b/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt
index 5b47e4d3..877ded06 100644
--- a/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt
+++ b/app/src/main/java/com/futo/platformplayer/activities/MainActivity.kt
@@ -591,6 +591,9 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
             StateBackup.importZipBytes(this, lifecycleScope, data);
             return true;
         }
+        else if(file.lowercase().endsWith(".txt") || mime == "text/plain") {
+            return handleUnknownText(String(data));
+        }
         return false;
     }
     fun handleFile(file: String): Boolean {
@@ -608,6 +611,9 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
             StateBackup.importZipBytes(this, lifecycleScope, readSharedFile(file));
             return true;
         }
+        else if(file.lowercase().endsWith(".txt")) {
+            return handleUnknownText(String(readSharedFile(file)));
+        }
         return false;
     }
     fun handleReconstruction(recon: String) {
@@ -633,6 +639,20 @@ class MainActivity : AppCompatActivity, IWithResultLauncher {
         }
     }
 
+    fun handleUnknownText(text: String): Boolean {
+        try {
+            if(text.startsWith("@/Subscription") || text.startsWith("Subscriptions")) {
+                val lines = text.split("\n").map { it.trim() }.drop(1).filter { it.isNotEmpty() };
+                navigate(_fragImportSubscriptions, lines);
+                return true;
+            }
+        }
+        catch(ex: Throwable) {
+            Logger.e(TAG, ex.message, ex);
+            UIDialogs.showGeneralErrorDialog(this, getString(R.string.failed_to_parse_text_file), ex);
+        }
+        return false;
+    }
     fun handleUnknownJson(name: String?, json: String): Boolean {
 
         val context = this;
diff --git a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ImportSubscriptionsFragment.kt b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ImportSubscriptionsFragment.kt
index 636172cd..db2fa74a 100644
--- a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ImportSubscriptionsFragment.kt
+++ b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ImportSubscriptionsFragment.kt
@@ -210,7 +210,7 @@ class ImportSubscriptionsFragment : MainFragment() {
 
     companion object {
         val TAG = "ImportSubscriptionsFragment";
-        private const val MAXIMUM_BATCH_SIZE = 90;
+        private const val MAXIMUM_BATCH_SIZE = 100;
         fun newInstance() = ImportSubscriptionsFragment().apply {}
     }
 }
\ No newline at end of file
diff --git a/app/src/main/java/com/futo/platformplayer/subscription/SmartSubscriptionAlgorithm.kt b/app/src/main/java/com/futo/platformplayer/subscription/SmartSubscriptionAlgorithm.kt
index 720fb75c..2a1b0dcc 100644
--- a/app/src/main/java/com/futo/platformplayer/subscription/SmartSubscriptionAlgorithm.kt
+++ b/app/src/main/java/com/futo/platformplayer/subscription/SmartSubscriptionAlgorithm.kt
@@ -42,9 +42,13 @@ class SmartSubscriptionAlgorithm(
                               if(sub.shouldFetchPosts()) ResultCapabilities.TYPE_POSTS else null,
                               if(sub.shouldFetchLiveStreams()) ResultCapabilities.TYPE_LIVE else null
                         ).filterNotNull().filter { capabilities.hasType(it) };
-                        return@flatMap types.map {
-                            SubscriptionTask(client, sub, url, it);
-                        };
+
+                        if(!types.isEmpty())
+                            return@flatMap types.map {
+                                SubscriptionTask(client, sub, url, it);
+                            };
+                        else
+                            listOf(SubscriptionTask(client, sub, url, ResultCapabilities.TYPE_VIDEOS, true))
                     }
                 };
         };
diff --git a/app/src/main/java/com/futo/platformplayer/subscription/SubscriptionsTaskFetchAlgorithm.kt b/app/src/main/java/com/futo/platformplayer/subscription/SubscriptionsTaskFetchAlgorithm.kt
index a12584d6..88b6e454 100644
--- a/app/src/main/java/com/futo/platformplayer/subscription/SubscriptionsTaskFetchAlgorithm.kt
+++ b/app/src/main/java/com/futo/platformplayer/subscription/SubscriptionsTaskFetchAlgorithm.kt
@@ -57,7 +57,7 @@ abstract class SubscriptionsTaskFetchAlgorithm(
             for(clientTasks in tasksGrouped) {
                 val clientTaskCount = clientTasks.value.filter { !it.fromCache }.size;
                 val clientCacheCount = clientTasks.value.size - clientTaskCount;
-                if(clientCacheCount > 0 && StateApp.instance.contextOrNull?.let { it is MainActivity && it.isFragmentActive<SubscriptionsFeedFragment>() } == true) {
+                if(clientCacheCount > 0 && clientTaskCount > 0 && StateApp.instance.contextOrNull?.let { it is MainActivity && it.isFragmentActive<SubscriptionsFeedFragment>() } == true) {
                     UIDialogs.toast("[${clientTasks.key.name}] only updating ${clientTaskCount} most urgent channels (rqs). (${clientCacheCount} cached)");
                 }
             }
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 238f8e16..dee4fc7d 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -435,6 +435,7 @@
     <string name="unknown_url_format">Unknown url format</string>
     <string name="failed_to_handle_file">Failed to handle file</string>
     <string name="unknown_reconstruction_type">Unknown reconstruction type</string>
+    <string name="failed_to_parse_text_file">Failed to parse text file</string>
     <string name="failed_to_parse_newpipe_subscriptions">Failed to parse NewPipe Subscriptions</string>
     <string name="failed_to_generate_qr_code">Failed to generate QR code</string>
     <string name="share_text">Share Text</string>
-- 
GitLab