mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Merge pull request #79 from archroid/master
Theme toggle switch and youtube subscription importer added!
This commit is contained in:
commit
4471e22977
@ -74,4 +74,7 @@ dependencies {
|
|||||||
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.2'
|
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.2'
|
||||||
|
|
||||||
implementation 'com.arthenica:ffmpeg-kit-min:4.5.1.LTS'
|
implementation 'com.arthenica:ffmpeg-kit-min:4.5.1.LTS'
|
||||||
|
|
||||||
|
implementation 'com.blankj:utilcode:1.30.0'
|
||||||
|
|
||||||
}
|
}
|
@ -1,19 +1,78 @@
|
|||||||
package com.github.libretube
|
package com.github.libretube
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.ContentValues.TAG
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.os.Environment
|
||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.preference.ListPreference
|
import androidx.preference.ListPreference
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
|
import androidx.preference.SwitchPreferenceCompat
|
||||||
|
import com.blankj.utilcode.util.UriUtils
|
||||||
|
import com.github.libretube.obj.Subscribe
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.util.zip.ZipFile
|
||||||
|
|
||||||
class Settings : PreferenceFragmentCompat() {
|
class Settings : PreferenceFragmentCompat() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
lateinit var getContent: ActivityResultLauncher<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
|
||||||
|
|
||||||
|
if (uri != null) {
|
||||||
|
var zipfile = ZipFile(UriUtils.uri2File(uri))
|
||||||
|
|
||||||
|
var zipentry =
|
||||||
|
zipfile.getEntry("Takeout/YouTube and YouTube Music/subscriptions/subscriptions.csv")
|
||||||
|
|
||||||
|
var inputStream = zipfile.getInputStream(zipentry)
|
||||||
|
|
||||||
|
val baos = ByteArrayOutputStream()
|
||||||
|
|
||||||
|
inputStream.use { it.copyTo(baos) }
|
||||||
|
|
||||||
|
var subscriptions = baos.toByteArray().decodeToString()
|
||||||
|
|
||||||
|
var subscribedCount = 0
|
||||||
|
|
||||||
|
for (text in subscriptions.lines()) {
|
||||||
|
if (text.take(24) != "Channel Id,Channel Url,C" && !text.take(24).isEmpty()) {
|
||||||
|
subscribe(text.take(24))
|
||||||
|
subscribedCount++
|
||||||
|
Log.d(TAG, "subscribed: " + text + " total: " + subscribedCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Subscribed to " + subscribedCount + " channels.",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
setPreferencesFromResource(R.xml.settings, rootKey)
|
setPreferencesFromResource(R.xml.settings, rootKey)
|
||||||
val instance = findPreference<ListPreference>("instance")
|
val instance = findPreference<ListPreference>("instance")
|
||||||
@ -39,6 +98,56 @@ class Settings : PreferenceFragmentCompat() {
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val importFromYt = findPreference<Preference>("import_from_yt")
|
||||||
|
importFromYt?.setOnPreferenceClickListener {
|
||||||
|
|
||||||
|
//check StorageAccess
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
Log.d("myz", "" + Build.VERSION.SDK_INT)
|
||||||
|
if (!Environment.isExternalStorageManager()) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this.requireActivity(), arrayOf(
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
|
Manifest.permission.MANAGE_EXTERNAL_STORAGE
|
||||||
|
), 1
|
||||||
|
) //permission request code is just an int
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ActivityCompat.checkSelfPermission(
|
||||||
|
requireContext(),
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
||||||
|
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
|
||||||
|
requireContext(),
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
ActivityCompat.requestPermissions(
|
||||||
|
this.requireActivity(),
|
||||||
|
arrayOf(
|
||||||
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||||
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
||||||
|
),
|
||||||
|
1
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getContent.launch("application/zip")
|
||||||
|
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
val themeToggle = findPreference<ListPreference>("theme_togglee")
|
||||||
|
themeToggle?.setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
when (newValue.toString()) {
|
||||||
|
"A" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||||
|
"L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||||
|
"D" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchInstance() {
|
private fun fetchInstance() {
|
||||||
@ -50,7 +159,7 @@ class Settings : PreferenceFragmentCompat() {
|
|||||||
Log.e("settings", "IOException, you might not have internet connection")
|
Log.e("settings", "IOException, you might not have internet connection")
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
} catch (e: HttpException) {
|
} catch (e: HttpException) {
|
||||||
Log.e("settings", "HttpException, unexpected response ${e.toString()}")
|
Log.e("settings", "HttpException, unexpected response $e")
|
||||||
return@launchWhenCreated
|
return@launchWhenCreated
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("settings", e.toString())
|
Log.e("settings", e.toString())
|
||||||
@ -68,7 +177,8 @@ class Settings : PreferenceFragmentCompat() {
|
|||||||
val instance = findPreference<ListPreference>("instance")
|
val instance = findPreference<ListPreference>("instance")
|
||||||
instance?.entries = entries
|
instance?.entries = entries
|
||||||
instance?.entryValues = entryValues
|
instance?.entryValues = entryValues
|
||||||
instance?.summaryProvider = Preference.SummaryProvider<ListPreference> { preference ->
|
instance?.summaryProvider =
|
||||||
|
Preference.SummaryProvider<ListPreference> { preference ->
|
||||||
val text = preference.entry
|
val text = preference.entry
|
||||||
if (TextUtils.isEmpty(text)) {
|
if (TextUtils.isEmpty(text)) {
|
||||||
"kavin.rocks (Official)"
|
"kavin.rocks (Official)"
|
||||||
@ -79,9 +189,33 @@ class Settings : PreferenceFragmentCompat() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Fragment?.runOnUiThread(action: () -> Unit) {
|
private fun Fragment?.runOnUiThread(action: () -> Unit) {
|
||||||
this ?: return
|
this ?: return
|
||||||
if (!isAdded) return // Fragment not attached to an Activity
|
if (!isAdded) return // Fragment not attached to an Activity
|
||||||
activity?.runOnUiThread(action)
|
activity?.runOnUiThread(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun subscribe(channel_id: String) {
|
||||||
|
fun run() {
|
||||||
|
lifecycleScope.launchWhenCreated {
|
||||||
|
val response = try {
|
||||||
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
||||||
|
RetrofitInstance.api.subscribe(
|
||||||
|
sharedPref?.getString("token", "")!!,
|
||||||
|
Subscribe(channel_id)
|
||||||
|
)
|
||||||
|
} catch (e: IOException) {
|
||||||
|
Log.e(TAG, "IOException, you might not have internet connection")
|
||||||
|
return@launchWhenCreated
|
||||||
|
} catch (e: HttpException) {
|
||||||
|
Log.e(TAG, "HttpException, unexpected response$e")
|
||||||
|
return@launchWhenCreated
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -404,4 +404,17 @@
|
|||||||
<item>ZM</item>
|
<item>ZM</item>
|
||||||
<item>ZW</item>
|
<item>ZW</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
|
<string-array name="themes">
|
||||||
|
<item>System Default</item>
|
||||||
|
<item>Light Theme</item>
|
||||||
|
<item>Dark Theme</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
|
<string-array name="themesValue">
|
||||||
|
<item>A</item>
|
||||||
|
<item>L</item>
|
||||||
|
<item>D</item>
|
||||||
|
</string-array>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -29,4 +29,7 @@
|
|||||||
<string name="downloadfailed">Download Failed!</string>
|
<string name="downloadfailed">Download Failed!</string>
|
||||||
<string name="vlc">Open in VLC</string>
|
<string name="vlc">Open in VLC</string>
|
||||||
<string name="vlcerror">Can\'t open in VLC. Maybe it\'s not installed yet?</string>
|
<string name="vlcerror">Can\'t open in VLC. Maybe it\'s not installed yet?</string>
|
||||||
|
<string name="import_from_yt">Import subscriptions from youtube</string>
|
||||||
|
<string name="app_theme">App theme</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
@ -30,4 +30,29 @@
|
|||||||
app:key="login_register"
|
app:key="login_register"
|
||||||
app:title="@string/login_register"
|
app:title="@string/login_register"
|
||||||
/>
|
/>
|
||||||
|
<androidx.preference.Preference
|
||||||
|
app:key="import_from_yt"
|
||||||
|
app:title="@string/import_from_yt"
|
||||||
|
android:hint="This is my hint"
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- <ListPreference-->
|
||||||
|
<!-- app:key="theme_toggleee"-->
|
||||||
|
<!-- app:title="@string/region"-->
|
||||||
|
<!-- app:entries="@array/regions"-->
|
||||||
|
<!-- app:entryValues="@array/regionsValue"-->
|
||||||
|
<!-- app:defaultValue="US"-->
|
||||||
|
<!-- app:useSimpleSummaryProvider="true"-->
|
||||||
|
<!-- />-->
|
||||||
|
|
||||||
|
<ListPreference
|
||||||
|
app:title="@string/app_theme"
|
||||||
|
app:key="theme_togglee"
|
||||||
|
app:entries="@array/themes"
|
||||||
|
app:entryValues="@array/themesValue"
|
||||||
|
app:defaultValue="A"
|
||||||
|
/>
|
||||||
|
|
||||||
|
|
||||||
</androidx.preference.PreferenceScreen>
|
</androidx.preference.PreferenceScreen>
|
Loading…
x
Reference in New Issue
Block a user