Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #35 from kuylar/feat/ltv3-warning
Browse files Browse the repository at this point in the history
Add a warning dialog if the selected instance is updated to LightTube v3
  • Loading branch information
kuylar authored Jun 4, 2024
2 parents 306e58e + 5545de6 commit 6d27b67
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/dev/kuylar/lighttube/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import okhttp3.OkHttpClient
import okhttp3.Request
import java.security.MessageDigest
import kotlin.concurrent.thread
import kotlin.random.Random
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.math.sqrt
Expand Down Expand Up @@ -494,7 +495,14 @@ class Utils {
return parseQueryString(query)["u"] ?: ""
}

@SuppressLint("NotifyDataSetChanged")
fun randomMotd(motd: List<String>): CharSequence {
return when (motd.size) {
0 -> ""
1 -> motd[0]
else -> motd[Random.nextInt(0, motd.size)]
}

@SuppressLint("NotifyDataSetChanged")
fun rebindAllRecyclerViews(recycler: RecyclerView) {
val adapter = recycler.adapter
val layoutManager = recycler.layoutManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package dev.kuylar.lighttube.api.models

import com.google.gson.annotations.JsonAdapter
import dev.kuylar.lighttube.gson.serialization.MotdDeserializer

class InstanceInfo (
val type: String,
val version: String,
val motd: String,
val allowsApi: Boolean,
@JsonAdapter(MotdDeserializer::class) val motd: List<String>,
val allowsAPI: Boolean,
val allowsNewUsers: Boolean,
val allowsOauthApi: Boolean,
val allowsThirdPartyProxyUsage: Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.kuylar.lighttube.gson.serialization

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import java.lang.reflect.Type

class MotdDeserializer: JsonDeserializer<List<String>> {
override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): List<String> {
if (json == null) return listOf("")

return if (json.isJsonPrimitive && json.asJsonPrimitive.isString) {
listOf(json.asJsonPrimitive.asString)
} else if (json.isJsonArray) {
json.asJsonArray.map { it.asString }
} else {
throw Exception("Failed to deserialize MOTD: Unknown type for ${json.asString}")
}
}
}
25 changes: 22 additions & 3 deletions app/src/main/java/dev/kuylar/lighttube/ui/fragment/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package dev.kuylar.lighttube.ui.fragment

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.edit
import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import dev.kuylar.lighttube.R
import dev.kuylar.lighttube.Utils
import dev.kuylar.lighttube.databinding.FragmentHomeBinding
import dev.kuylar.lighttube.ui.activity.MainActivity
import dev.kuylar.lighttube.ui.activity.SetupActivity
import java.io.IOException
import kotlin.concurrent.thread


class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding

Expand All @@ -29,17 +35,30 @@ class HomeFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
val a = (activity as MainActivity)
val sp = a.getSharedPreferences("main", Activity.MODE_PRIVATE)
binding.homeMotd.text = sp.getString("cachedMotd", "Search something to get started!")
binding.homeMotd.text = Utils.randomMotd(sp.getStringSet("cachedMotds", mutableSetOf("Search something to get started!"))!!.toList())

a.setLoading(true)
thread {
try {
val info = a.getApi().getInstanceInfo()
a.runOnUiThread {
a.setLoading(false)
binding.homeMotd.text = info.motd
if (info.type == "lighttube/2.0") {
MaterialAlertDialogBuilder(requireContext()).apply {
setTitle(R.string.lighttube_3_title)
setMessage(R.string.lighttube_3_body)
setCancelable(false)
setPositiveButton(R.string.lighttube_3_github) { dialog, _ ->
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/lighttube-org/LightTube-Android")))
}
setNegativeButton(R.string.lighttube_3_instance) { dialog, _ ->
startActivity(Intent(requireContext(), SetupActivity::class.java))
}
}.show()
}
binding.homeMotd.text = Utils.randomMotd(info.motd)
sp.edit {
putString("cachedMotd", info.motd)
putStringSet("cachedMotds", info.motd.toMutableSet())
}
}
} catch (e: IOException) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ Your data will be stored in the instance you chose (%s).</string>
<string name="comments_title">Comments</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="lighttube_3_title">Unsupported instance</string>
<string name="lighttube_3_body">LightTube v3 is not supported in this version of the Android app. Check for updates in https://github.com/lighttube-org/LightTube-Android</string>
<string name="lighttube_3_github">Open GitHub</string>
<string name="lighttube_3_instance">Change instance</string>
</resources>

0 comments on commit 6d27b67

Please sign in to comment.