fixed command templates export/import crashing on older models
This commit is contained in:
parent
e915fb3eab
commit
053ad0a1a9
3 changed files with 32 additions and 24 deletions
|
|
@ -3,6 +3,8 @@ package com.deniscerri.ytdlnis.database.viewmodel
|
|||
import android.app.Application
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context.CLIPBOARD_SERVICE
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
|
|
@ -13,6 +15,7 @@ import com.deniscerri.ytdlnis.database.models.TemplateShortcut
|
|||
import com.deniscerri.ytdlnis.database.repository.CommandTemplateRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
|
|
@ -70,7 +73,9 @@ class CommandTemplateViewModel(private val application: Application) : AndroidVi
|
|||
val allTemplates = repository.getAll()
|
||||
val allShortcuts = repository.getAllShortCuts()
|
||||
var count = 0
|
||||
val clipboard = application.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clipboard = withContext(Dispatchers.Main){
|
||||
application.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
||||
}
|
||||
val clip = clipboard.primaryClip!!.getItemAt(0).text.toString()
|
||||
try{
|
||||
jsonFormat.decodeFromString<CommandTemplateExport>(clip).run {
|
||||
|
|
@ -100,10 +105,14 @@ class CommandTemplateViewModel(private val application: Application) : AndroidVi
|
|||
}
|
||||
|
||||
|
||||
fun exportToClipboard() = viewModelScope.launch(Dispatchers.IO) {
|
||||
fun exportToClipboard() = viewModelScope.launch {
|
||||
try{
|
||||
val allTemplates = repository.getAll()
|
||||
val allShortcuts = repository.getAllShortCuts()
|
||||
val allTemplates = withContext(Dispatchers.IO){
|
||||
repository.getAll()
|
||||
}
|
||||
val allShortcuts = withContext(Dispatchers.IO){
|
||||
repository.getAllShortCuts()
|
||||
}
|
||||
val output = jsonFormat.encodeToString(
|
||||
CommandTemplateExport(
|
||||
templates = allTemplates,
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ class HomeFragment : Fragment(), HomeAdapter.OnItemClickListener, View.OnClickLi
|
|||
homeAdapter!!.submitList(it)
|
||||
resultsList = it
|
||||
if(resultViewModel.itemCount.value!! > 1 || resultViewModel.itemCount.value!! == -1){
|
||||
if (it[0].playlistTitle.isNotEmpty() && it[0].playlistTitle != getString(R.string.trendingPlaylist) && it.size > 1){
|
||||
if (it.size > 1 && it[0].playlistTitle.isNotEmpty() && it[0].playlistTitle != getString(R.string.trendingPlaylist) && it.size > 1){
|
||||
downloadAllFabCoordinator!!.visibility = VISIBLE
|
||||
}else{
|
||||
downloadAllFabCoordinator!!.visibility = GONE
|
||||
|
|
|
|||
|
|
@ -397,10 +397,9 @@ class InfoUtil(context: Context) {
|
|||
|
||||
@Throws(JSONException::class)
|
||||
fun getTrending(context: Context): ArrayList<ResultItem?> {
|
||||
init()
|
||||
items = ArrayList()
|
||||
return if (key!!.isEmpty()) {
|
||||
if (useInvidous) getTrendingFromInvidous(context) else ArrayList()
|
||||
items
|
||||
} else getTrendingFromKey(context)
|
||||
}
|
||||
|
||||
|
|
@ -434,23 +433,23 @@ class InfoUtil(context: Context) {
|
|||
return items
|
||||
}
|
||||
|
||||
private fun getTrendingFromInvidous(context: Context): ArrayList<ResultItem?> {
|
||||
val url = invidousURL + "trending?type=music®ion=" + countryCODE
|
||||
val res = genericArrayRequest(url)
|
||||
try {
|
||||
for (i in 0 until res.length()) {
|
||||
val element = res.getJSONObject(i)
|
||||
if (element.getString("type") != "video") continue
|
||||
val v = createVideofromInvidiousJSON(element)
|
||||
if (v == null || v.thumb.isEmpty()) continue
|
||||
v.playlistTitle = context.getString(R.string.trendingPlaylist)
|
||||
items.add(v)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return items
|
||||
}
|
||||
// private fun getTrendingFromInvidous(context: Context): ArrayList<ResultItem?> {
|
||||
// val url = invidousURL + "trending?type=music®ion=" + countryCODE
|
||||
// val res = genericArrayRequest(url)
|
||||
// try {
|
||||
// for (i in 0 until res.length()) {
|
||||
// val element = res.getJSONObject(i)
|
||||
// if (element.getString("type") != "video") continue
|
||||
// val v = createVideofromInvidiousJSON(element)
|
||||
// if (v == null || v.thumb.isEmpty()) continue
|
||||
// v.playlistTitle = context.getString(R.string.trendingPlaylist)
|
||||
// items.add(v)
|
||||
// }
|
||||
// } catch (e: Exception) {
|
||||
// e.printStackTrace()
|
||||
// }
|
||||
// return items
|
||||
// }
|
||||
|
||||
fun getIDFromYoutubeURL(inputQuery: String) : String {
|
||||
var el: Array<String?> =
|
||||
|
|
|
|||
Loading…
Reference in a new issue