Fix subscription imports on R.

This commit is contained in:
Kavin 2022-04-16 07:21:50 +01:00
parent 0c76b99fe2
commit 015f34292d
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
5 changed files with 78 additions and 94 deletions

View File

@ -30,6 +30,7 @@ android {
} }
} }
compileOptions { compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
@ -76,6 +77,5 @@ dependencies {
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' coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5"
} }

View File

@ -12,30 +12,31 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.libretube.obj.Login import com.github.libretube.obj.Login
class CreatePlaylistDialog: DialogFragment() { class CreatePlaylistDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
val builder = AlertDialog.Builder(it) val builder = AlertDialog.Builder(it)
// Get the layout inflater // Get the layout inflater
val inflater = requireActivity().layoutInflater; val inflater = requireActivity().layoutInflater;
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token","") val token = sharedPref?.getString("token", "")
var view: View Log.e("dafaq", token!!)
Log.e("dafaq",token!!)
if(token!=""){ assert(token != "")
val sharedPref2 = context?.getSharedPreferences("username", Context.MODE_PRIVATE)
val user = sharedPref2?.getString("username","") val sharedPref2 = context?.getSharedPreferences("username", Context.MODE_PRIVATE)
view = inflater.inflate(R.layout.dialog_logout, null) val user = sharedPref2?.getString("username", "")
view.findViewById<TextView>(R.id.user).text = view.findViewById<TextView>(R.id.user).text.toString()+" ("+user+")" val view: View = inflater.inflate(R.layout.dialog_logout, null)
view.findViewById<Button>(R.id.logout).setOnClickListener { view.findViewById<TextView>(R.id.user).text =
Toast.makeText(context,R.string.loggedout, Toast.LENGTH_SHORT).show() view.findViewById<TextView>(R.id.user).text.toString() + " (" + user + ")"
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) view.findViewById<Button>(R.id.logout).setOnClickListener {
with (sharedPref!!.edit()) { Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
putString("token","") val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
apply() with(sharedPref!!.edit()) {
} putString("token", "")
dialog?.dismiss() apply()
} }
dialog?.dismiss()
} }
builder.setView(view) builder.setView(view)
builder.create() builder.create()

View File

@ -53,7 +53,7 @@ interface PipedApi {
suspend fun unsubscribe(@Header("Authorization") token: String, @Body subscribe: Subscribe): Message suspend fun unsubscribe(@Header("Authorization") token: String, @Body subscribe: Subscribe): Message
@POST("import") @POST("import")
suspend fun importSubscriptions(@Query("override") override: String, @Header("Authorization") token: String, @Body channels: List<String>): Message suspend fun importSubscriptions(@Query("override") override: Boolean, @Header("Authorization") token: String, @Body channels: List<String>): Message
@GET("user/playlists") @GET("user/playlists")
suspend fun playlists(@Header("Authorization") token: String): List<Playlists> suspend fun playlists(@Header("Authorization") token: String): List<Playlists>

View File

@ -20,14 +20,15 @@ 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 com.blankj.utilcode.util.UriUtils
import retrofit2.HttpException import retrofit2.HttpException
import java.io.* import java.io.*
import java.util.zip.ZipFile import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
class Settings : PreferenceFragmentCompat() { class Settings : PreferenceFragmentCompat() {
val TAG = "Settings" val TAG = "Settings"
companion object { companion object {
lateinit var getContent: ActivityResultLauncher<String> lateinit var getContent: ActivityResultLauncher<String>
} }
@ -36,76 +37,52 @@ class Settings : PreferenceFragmentCompat() {
getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
if (uri != null) { if (uri != null) {
try{ try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Open a specific media item using ParcelFileDescriptor.
val resolver: ContentResolver =
requireActivity()
.contentResolver
// Open a specific media item using ParcelFileDescriptor. // "rw" for read-and-write;
val resolver: ContentResolver = // "rwt" for truncating or overwriting existing file contents.
requireActivity() val readOnlyMode = "r"
.contentResolver // uri - I have got from onActivityResult
val type = resolver.getType(uri)
// "rw" for read-and-write; var inputStream: InputStream? = resolver.openInputStream(uri)
// "rwt" for truncating or overwriting existing file contents.
val readOnlyMode = "r"
// uri - I have got from onActivityResult
//uri = data.getData();
val parcelFile = resolver.openFileDescriptor(uri, readOnlyMode)
val fileReader = FileReader(parcelFile!!.fileDescriptor)
val reader = BufferedReader(fileReader)
var line: String?
var channels: MutableList<String> = emptyList<String>().toMutableList()
var subscribedCount = 0
while (reader.readLine().also { line = it } != null) {
if (line!!.replace(" ","") != "" && subscribedCount >0) {
val channel = line!!.split(",")[0]
channels.add(channel)
Log.d(TAG, "subscribed: " + line + " total: " + subscribedCount) if (type == "application/zip") {
val zis = ZipInputStream(inputStream)
var entry: ZipEntry? = zis.nextEntry
while (entry != null) {
if (entry.name.endsWith(".csv")) {
inputStream = zis
break
} }
subscribedCount++ entry = zis.nextEntry
} }
subscribe(channels)
reader.close()
fileReader.close()
}else{
Log.d(TAG,UriUtils.uri2File(uri).toString())
val file = UriUtils.uri2File(uri)
var inputStream: InputStream? = null
if (file.extension == "zip") {
var zipfile = ZipFile(file)
var zipentry =
zipfile.getEntry("Takeout/YouTube and YouTube Music/subscriptions/subscriptions.csv")
inputStream = zipfile.getInputStream(zipentry)
}else if(file.extension == "csv"){
inputStream = file.inputStream()
}
val baos = ByteArrayOutputStream()
inputStream?.use { it.copyTo(baos) }
var subscriptions = baos.toByteArray().decodeToString()
var channels: MutableList<String> = emptyList<String>().toMutableList()
var subscribedCount = 0
for (text in subscriptions.lines().subList(1,subscriptions.lines().size)) {
if (text.replace(" ","") != "") {
val channel = text.split(",")[0]
channels.add(channel)
subscribedCount++
Log.d(TAG, "subscribed: " + text + " total: " + subscribedCount)
}
}
subscribe(channels)
} }
}catch (e: Exception){ val channels = ArrayList<String>()
Log.e(TAG,e.toString())
inputStream?.bufferedReader()?.readLines()?.forEach {
if (it.isNotBlank()) {
val channelId = it.substringBefore(",")
if (channelId.length == 24)
channels.add(channelId)
}
}
inputStream?.close()
subscribe(channels)
} catch (e: Exception) {
Log.e(TAG, e.toString())
Toast.makeText( Toast.makeText(
context, context,
R.string.error, R.string.error,
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
} }
} }
@ -141,11 +118,14 @@ class Settings : PreferenceFragmentCompat() {
val importFromYt = findPreference<Preference>("import_from_yt") val importFromYt = findPreference<Preference>("import_from_yt")
importFromYt?.setOnPreferenceClickListener { importFromYt?.setOnPreferenceClickListener {
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token","")!! val token = sharedPref?.getString("token", "")!!
//check StorageAccess //check StorageAccess
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", "" + Build.VERSION.SDK_INT) Log.d("myz", "" + Build.VERSION.SDK_INT)
if (ContextCompat.checkSelfPermission(this.requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE) if (ContextCompat.checkSelfPermission(
this.requireContext(),
Manifest.permission.READ_EXTERNAL_STORAGE
)
!= PackageManager.PERMISSION_GRANTED != PackageManager.PERMISSION_GRANTED
) { ) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
@ -154,9 +134,9 @@ class Settings : PreferenceFragmentCompat() {
Manifest.permission.MANAGE_EXTERNAL_STORAGE Manifest.permission.MANAGE_EXTERNAL_STORAGE
), 1 ), 1
) //permission request code is just an int ) //permission request code is just an int
}else if (token != ""){ } else if (token != "") {
getContent.launch("*/*") getContent.launch("*/*")
}else{ } else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
} }
} else { } else {
@ -176,9 +156,9 @@ class Settings : PreferenceFragmentCompat() {
), ),
1 1
) )
}else if (token != ""){ } else if (token != "") {
getContent.launch("*/*") getContent.launch("*/*")
}else{ } else {
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
} }
} }
@ -249,7 +229,11 @@ class Settings : PreferenceFragmentCompat() {
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.importSubscriptions("false",sharedPref?.getString("token", "")!!,channels) RetrofitInstance.api.importSubscriptions(
false,
sharedPref?.getString("token", "")!!,
channels
)
} catch (e: IOException) { } 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
@ -257,7 +241,7 @@ class Settings : PreferenceFragmentCompat() {
Log.e(TAG, "HttpException, unexpected response$e") Log.e(TAG, "HttpException, unexpected response$e")
return@launchWhenCreated return@launchWhenCreated
} }
if(response.message == "ok"){ if (response.message == "ok") {
Toast.makeText( Toast.makeText(
context, context,
R.string.importsuccess, R.string.importsuccess,

View File

@ -19,8 +19,6 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.StringUtils.getString
import com.blankj.utilcode.util.ThreadUtils.runOnUiThread
import com.github.libretube.* import com.github.libretube.*
import com.github.libretube.obj.PlaylistId import com.github.libretube.obj.PlaylistId
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
@ -54,8 +52,8 @@ class PlaylistsAdapter(private val playlists: MutableList<Playlists>): RecyclerV
holder.v.findViewById<TextView>(R.id.playlist_title).text = playlist.name holder.v.findViewById<TextView>(R.id.playlist_title).text = playlist.name
holder.v.findViewById<ImageView>(R.id.delete_playlist).setOnClickListener { holder.v.findViewById<ImageView>(R.id.delete_playlist).setOnClickListener {
val builder = AlertDialog.Builder(holder.v.context) val builder = AlertDialog.Builder(holder.v.context)
builder.setTitle(getString(R.string.deletePlaylist)) builder.setTitle(R.string.deletePlaylist)
builder.setMessage(getString(R.string.areYouSure)) builder.setMessage(R.string.areYouSure)
builder.setPositiveButton(R.string.yes) { dialog, which -> builder.setPositiveButton(R.string.yes) { dialog, which ->
val sharedPref = holder.v.context.getSharedPreferences("token", Context.MODE_PRIVATE) val sharedPref = holder.v.context.getSharedPreferences("token", Context.MODE_PRIVATE)
val token = sharedPref?.getString("token","")!! val token = sharedPref?.getString("token","")!!
@ -92,7 +90,8 @@ class PlaylistsAdapter(private val playlists: MutableList<Playlists>): RecyclerV
if(response.message == "ok"){ if(response.message == "ok"){
Log.d(TAG,"deleted!") Log.d(TAG,"deleted!")
playlists.removeAt(position) playlists.removeAt(position)
runOnUiThread{notifyDataSetChanged()} // FIXME: This needs to run on UI thread?
notifyDataSetChanged()
/*if(playlists.isEmpty()){ /*if(playlists.isEmpty()){
view.findViewById<ImageView>(R.id.boogh2).visibility=View.VISIBLE view.findViewById<ImageView>(R.id.boogh2).visibility=View.VISIBLE
}*/ }*/