Fixed minor bugs

This commit is contained in:
archroid 2022-03-17 22:46:09 +03:30
parent ca6196da54
commit c0d3522bb8
3 changed files with 43 additions and 39 deletions

View File

@ -1,33 +1,27 @@
package com.github.libretube package com.github.libretube
import android.Manifest 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.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.view.View
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
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 android.content.ContentValues.TAG
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import androidx.core.app.ActivityCompat
import androidx.preference.PreferenceManager
import com.blankj.utilcode.util.UriUtils import com.blankj.utilcode.util.UriUtils
import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.obj.Subscribe import com.github.libretube.obj.Subscribe
import retrofit2.HttpException import retrofit2.HttpException
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.IOException import java.io.IOException
import java.util.zip.ZipFile import java.util.zip.ZipFile
@ -42,7 +36,7 @@ class Settings : PreferenceFragmentCompat() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
if(uri != null){ if (uri != null) {
var zipfile = ZipFile(UriUtils.uri2File(uri)) var zipfile = ZipFile(UriUtils.uri2File(uri))
var zipentry = var zipentry =
@ -58,15 +52,19 @@ class Settings : PreferenceFragmentCompat() {
var subscribedCount = 0 var subscribedCount = 0
for(text in subscriptions.lines()){ for (text in subscriptions.lines()) {
if(text.take(24) != "Channel Id,Channel Url,C" && !text.take(24).isEmpty()){ if (text.take(24) != "Channel Id,Channel Url,C" && !text.take(24).isEmpty()) {
subscribe(text.take(24)) subscribe(text.take(24))
subscribedCount++ subscribedCount++
Log.d(TAG, "subscribed: " + text +" total: " + subscribedCount) Log.d(TAG, "subscribed: " + text + " total: " + subscribedCount)
} }
} }
Toast.makeText(context, "Subscribed to " + subscribedCount + " channels.", Toast.LENGTH_SHORT).show() Toast.makeText(
context,
"Subscribed to " + subscribedCount + " channels.",
Toast.LENGTH_SHORT
).show()
} }
} }
@ -81,7 +79,7 @@ class Settings : PreferenceFragmentCompat() {
RetrofitInstance.url = newValue.toString() RetrofitInstance.url = newValue.toString()
RetrofitInstance.lazyMgr.reset() RetrofitInstance.lazyMgr.reset()
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
if(sharedPref?.getString("token","")!="") { if (sharedPref?.getString("token", "") != "") {
with(sharedPref!!.edit()) { with(sharedPref!!.edit()) {
putString("token", "") putString("token", "")
apply() apply()
@ -91,12 +89,12 @@ class Settings : PreferenceFragmentCompat() {
true true
} }
val login = findPreference<Preference>("login_register") val login = findPreference<Preference>("login_register")
login?.setOnPreferenceClickListener { login?.setOnPreferenceClickListener {
val newFragment = LoginDialog() val newFragment = LoginDialog()
newFragment.show(childFragmentManager, "Login") newFragment.show(childFragmentManager, "Login")
true true
} }
val importFromYt = findPreference<Preference>("import_from_yt") val importFromYt = findPreference<Preference>("import_from_yt")
importFromYt?.setOnPreferenceClickListener { importFromYt?.setOnPreferenceClickListener {
@ -139,7 +137,7 @@ class Settings : PreferenceFragmentCompat() {
true true
} }
} }
private fun fetchInstance() { private fun fetchInstance() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
@ -150,15 +148,15 @@ 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())
return@launchWhenCreated return@launchWhenCreated
} }
val listEntries: MutableList<String> = ArrayList() val listEntries: MutableList<String> = ArrayList()
val listEntryValues: MutableList<String> = ArrayList() val listEntryValues: MutableList<String> = ArrayList()
for(item in response){ for (item in response) {
listEntries.add(item.name!!) listEntries.add(item.name!!)
listEntryValues.add(item.api_url!!) listEntryValues.add(item.api_url!!)
} }
@ -168,17 +166,19 @@ 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 =
val text = preference.entry Preference.SummaryProvider<ListPreference> { preference ->
if (TextUtils.isEmpty(text)) { val text = preference.entry
"kavin.rocks (Official)" if (TextUtils.isEmpty(text)) {
} else { "kavin.rocks (Official)"
text } else {
text
}
} }
}
} }
} }
} }
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
@ -186,14 +186,16 @@ class Settings : PreferenceFragmentCompat() {
} }
private fun subscribe(channel_id: String){ private fun subscribe(channel_id: String) {
fun run() { fun run() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
RetrofitInstance.api.subscribe(sharedPref?.getString("token","")!!, Subscribe(channel_id)) RetrofitInstance.api.subscribe(
}catch(e: IOException) { sharedPref?.getString("token", "")!!,
println(e) Subscribe(channel_id)
)
} catch (e: IOException) {
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
return@launchWhenCreated return@launchWhenCreated
} catch (e: HttpException) { } catch (e: HttpException) {

View File

@ -30,5 +30,6 @@
<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="import_from_yt">Import subscriptions from youtube</string>
<string name="dark_theme">Dark theme</string>
</resources> </resources>

View File

@ -36,4 +36,5 @@
android:hint="This is my hint" android:hint="This is my hint"
/> />
</androidx.preference.PreferenceScreen> </androidx.preference.PreferenceScreen>