fix: optimization # 2

This commit is contained in:
rebelonion 2024-05-05 20:03:07 -05:00
parent 14f29d09df
commit b187cf06be
10 changed files with 123 additions and 70 deletions

View file

@ -51,7 +51,7 @@ android {
} }
debug { debug {
applicationIdSuffix ".beta" applicationIdSuffix ".beta"
versionNameSuffix "-beta02" versionNameSuffix "-beta03"
manifestPlaceholders.icon_placeholder = "@mipmap/ic_launcher_beta" manifestPlaceholders.icon_placeholder = "@mipmap/ic_launcher_beta"
manifestPlaceholders.icon_placeholder_round = "@mipmap/ic_launcher_beta_round" manifestPlaceholders.icon_placeholder_round = "@mipmap/ic_launcher_beta_round"
debuggable false debuggable false

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
import ani.dantotsu.addons.download.DownloadAddonManager import ani.dantotsu.addons.download.DownloadAddonManager
@ -93,7 +94,7 @@ class App : MultiDexApplication() {
Logger.init(this) Logger.init(this)
Thread.setDefaultUncaughtExceptionHandler(FinalExceptionHandler()) Thread.setDefaultUncaughtExceptionHandler(FinalExceptionHandler())
Logger.log("App: Logging started") Logger.log(Log.WARN, "App: Logging started")
initializeNetwork() initializeNetwork()

View file

@ -33,8 +33,8 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.media3.common.util.UnstableApi import androidx.media3.common.util.UnstableApi
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import ani.dantotsu.addons.torrent.TorrentServerService
import ani.dantotsu.addons.torrent.TorrentAddonManager import ani.dantotsu.addons.torrent.TorrentAddonManager
import ani.dantotsu.addons.torrent.TorrentServerService
import ani.dantotsu.connections.anilist.Anilist import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.connections.anilist.AnilistHomeViewModel import ani.dantotsu.connections.anilist.AnilistHomeViewModel
import ani.dantotsu.databinding.ActivityMainBinding import ani.dantotsu.databinding.ActivityMainBinding
@ -336,9 +336,6 @@ class MainActivity : AppCompatActivity() {
startActivity(Intent(this, NoInternet::class.java)) startActivity(Intent(this, NoInternet::class.java))
} else { } else {
val model: AnilistHomeViewModel by viewModels() val model: AnilistHomeViewModel by viewModels()
model.genres.observe(this) {
if (it != null) {
if (it) {
val navbar = binding.includedNavbar.navbar val navbar = binding.includedNavbar.navbar
bottomBar = navbar bottomBar = navbar
navbar.visibility = View.VISIBLE navbar.visibility = View.VISIBLE
@ -370,11 +367,6 @@ class MainActivity : AppCompatActivity() {
) )
} }
} }
} else {
binding.mainProgressBar.visibility = View.GONE
}
}
}
//Load Data //Load Data
if (!load && !launched) { if (!load && !launched) {
scope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {

View file

@ -35,6 +35,8 @@ object Anilist {
var rateLimitReset: Long = 0 var rateLimitReset: Long = 0
var initialized = false
val sortBy = listOf( val sortBy = listOf(
"SCORE_DESC", "SCORE_DESC",
"POPULARITY_DESC", "POPULARITY_DESC",

View file

@ -57,6 +57,7 @@ class AnilistQueries {
Anilist.unreadNotificationCount = user.unreadNotificationCount ?: 0 Anilist.unreadNotificationCount = user.unreadNotificationCount ?: 0
val unread = PrefManager.getVal<Int>(PrefName.UnreadCommentNotifications) val unread = PrefManager.getVal<Int>(PrefName.UnreadCommentNotifications)
Anilist.unreadNotificationCount += unread Anilist.unreadNotificationCount += unread
Anilist.initialized = true
return true return true
} }

View file

@ -5,6 +5,7 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.lifecycleScope
import ani.dantotsu.BuildConfig import ani.dantotsu.BuildConfig
import ani.dantotsu.R import ani.dantotsu.R
import ani.dantotsu.connections.discord.Discord import ani.dantotsu.connections.discord.Discord
@ -12,31 +13,26 @@ import ani.dantotsu.connections.mal.MAL
import ani.dantotsu.media.Media import ani.dantotsu.media.Media
import ani.dantotsu.others.AppUpdater import ani.dantotsu.others.AppUpdater
import ani.dantotsu.profile.User import ani.dantotsu.profile.User
import ani.dantotsu.profile.activity.ActivityItemBuilder
import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.tryWithSuspend import ani.dantotsu.tryWithSuspend
import ani.dantotsu.util.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
suspend fun getUserId(context: Context, block: () -> Unit) { suspend fun getUserId(context: Context, block: () -> Unit) {
val anilist = if (Anilist.userid == null && Anilist.token != null) { if (!Anilist.initialized) {
if (Anilist.query.getUserData()) { if (Anilist.query.getUserData()) {
tryWithSuspend { tryWithSuspend {
if (MAL.token != null && !MAL.query.getUserData()) if (MAL.token != null && !MAL.query.getUserData())
snackString(context.getString(R.string.error_loading_mal_user_data)) snackString(context.getString(R.string.error_loading_mal_user_data))
} }
true
} else { } else {
snackString(context.getString(R.string.error_loading_anilist_user_data)) snackString(context.getString(R.string.error_loading_anilist_user_data))
false
} }
} else true }
block.invoke()
if (anilist) block.invoke()
} }
class AnilistHomeViewModel : ViewModel() { class AnilistHomeViewModel : ViewModel() {
@ -90,6 +86,7 @@ class AnilistHomeViewModel : ViewModel() {
MutableLiveData<ArrayList<Media>>(null) MutableLiveData<ArrayList<Media>>(null)
fun getHidden(): LiveData<ArrayList<Media>> = hidden fun getHidden(): LiveData<ArrayList<Media>> = hidden
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
suspend fun initHomePage() { suspend fun initHomePage() {
val res = Anilist.query.initHomePage() val res = Anilist.query.initHomePage()
@ -104,15 +101,20 @@ class AnilistHomeViewModel : ViewModel() {
res["status"]?.let { userStatus.postValue(it as ArrayList<User>?) } res["status"]?.let { userStatus.postValue(it as ArrayList<User>?) }
} }
suspend fun loadMain(context: FragmentActivity) { suspend fun loadMain(context: FragmentActivity) {
Anilist.getSavedToken() Anilist.getSavedToken()
MAL.getSavedToken() MAL.getSavedToken()
Discord.getSavedToken() Discord.getSavedToken()
if (!BuildConfig.FLAVOR.contains("fdroid")) { if (!BuildConfig.FLAVOR.contains("fdroid")) {
if (PrefManager.getVal(PrefName.CheckUpdate)) AppUpdater.check(context) if (PrefManager.getVal(PrefName.CheckUpdate))
context.lifecycleScope.launch(Dispatchers.IO) {
AppUpdater.check(context, true)
}
}
val ret = Anilist.query.getGenresAndTags()
withContext(Dispatchers.Main) {
genres.value = ret
} }
genres.postValue(Anilist.query.getGenresAndTags())
} }
val empty = MutableLiveData<Boolean>(null) val empty = MutableLiveData<Boolean>(null)

View file

@ -36,6 +36,7 @@ import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.statusBarHeight import ani.dantotsu.statusBarHeight
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -268,14 +269,26 @@ class AnimeFragment : Fragment() {
true true
} }
var running = false
val live = Refresh.activity.getOrPut(this.hashCode()) { MutableLiveData(false) } val live = Refresh.activity.getOrPut(this.hashCode()) { MutableLiveData(false) }
live.observe(viewLifecycleOwner) { live.observe(viewLifecycleOwner) {
if (it) { if (it && !running) {
running = true
scope.launch { scope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
Anilist.userid = PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
?.toIntOrNull()
if (Anilist.userid == null) {
getUserId(requireContext()) { getUserId(requireContext()) {
load() load()
} }
} else {
CoroutineScope(Dispatchers.IO).launch {
getUserId(requireContext()) {
load()
}
}
}
model.loaded = true model.loaded = true
model.loadTrending(1) model.loadTrending(1)
model.loadAll() model.loadAll()
@ -287,6 +300,7 @@ class AnimeFragment : Fragment() {
} }
live.postValue(false) live.postValue(false)
_binding?.animeRefresh?.isRefreshing = false _binding?.animeRefresh?.isRefreshing = false
running = false
} }
} }
} }

View file

@ -48,9 +48,9 @@ import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.statusBarHeight import ani.dantotsu.statusBarHeight
import ani.dantotsu.util.Logger import ani.dantotsu.util.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -379,7 +379,8 @@ class HomeFragment : Fragment() {
model.getHidden().observe(viewLifecycleOwner) { model.getHidden().observe(viewLifecycleOwner) {
if (it != null) { if (it != null) {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
binding.homeHiddenItemsRecyclerView.adapter = MediaAdaptor(0, it, requireActivity()) binding.homeHiddenItemsRecyclerView.adapter =
MediaAdaptor(0, it, requireActivity())
binding.homeHiddenItemsRecyclerView.layoutManager = LinearLayoutManager( binding.homeHiddenItemsRecyclerView.layoutManager = LinearLayoutManager(
requireContext(), requireContext(),
LinearLayoutManager.HORIZONTAL, LinearLayoutManager.HORIZONTAL,
@ -393,7 +394,8 @@ class HomeFragment : Fragment() {
} }
binding.homeHiddenItemsMore.setSafeOnClickListener { _ -> binding.homeHiddenItemsMore.setSafeOnClickListener { _ ->
ContextCompat.startActivity( ContextCompat.startActivity(
requireActivity(), Intent(requireActivity(), MediaListViewActivity::class.java) requireActivity(),
Intent(requireActivity(), MediaListViewActivity::class.java)
.putExtra("title", getString(R.string.hidden)) .putExtra("title", getString(R.string.hidden))
.putExtra("media", it), .putExtra("media", it),
null null
@ -403,8 +405,7 @@ class HomeFragment : Fragment() {
binding.homeHiddenItemsContainer.visibility = View.GONE binding.homeHiddenItemsContainer.visibility = View.GONE
true true
} }
} } else {
else {
binding.homeContinueWatch.setOnLongClickListener { binding.homeContinueWatch.setOnLongClickListener {
snackString(getString(R.string.no_hidden_items)) snackString(getString(R.string.no_hidden_items))
true true
@ -457,17 +458,29 @@ class HomeFragment : Fragment() {
val live = Refresh.activity.getOrPut(1) { MutableLiveData(true) } val live = Refresh.activity.getOrPut(1) { MutableLiveData(true) }
live.observe(viewLifecycleOwner) live.observe(viewLifecycleOwner)
{ {
if (it && !running) { if (!running && it) {
running = true running = true
scope.launch { scope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
//Get userData First //Get userData First
Anilist.userid =
PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
?.toIntOrNull()
if (Anilist.userid == null) {
getUserId(requireContext()) { getUserId(requireContext()) {
load() load()
} }
} else {
CoroutineScope(Dispatchers.IO).launch {
getUserId(requireContext()) {
load()
}
}
}
model.loaded = true model.loaded = true
CoroutineScope(Dispatchers.IO).launch {
model.setListImages() model.setListImages()
Logger.log("HomeFragment: Refreshing") }
var empty = true var empty = true
val homeLayoutShow: List<Boolean> = val homeLayoutShow: List<Boolean> =
PrefManager.getVal(PrefName.HomeLayout) PrefManager.getVal(PrefName.HomeLayout)
@ -483,9 +496,9 @@ class HomeFragment : Fragment() {
} }
live.postValue(false) live.postValue(false)
_binding?.homeRefresh?.isRefreshing = false _binding?.homeRefresh?.isRefreshing = false
running = false
} }
binding.homeHiddenItemsContainer.visibility = View.GONE binding.homeHiddenItemsContainer.visibility = View.GONE
running = false
} }
} }

View file

@ -33,6 +33,7 @@ import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString import ani.dantotsu.snackString
import ani.dantotsu.statusBarHeight import ani.dantotsu.statusBarHeight
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -253,14 +254,26 @@ class MangaFragment : Fragment() {
mangaPageAdapter.updateAvatar() mangaPageAdapter.updateAvatar()
} }
val live = Refresh.activity.getOrPut(this.hashCode()) { MutableLiveData(false) } var running = false
val live = Refresh.activity.getOrPut(this.hashCode()) { MutableLiveData(true) }
live.observe(viewLifecycleOwner) { live.observe(viewLifecycleOwner) {
if (it) { if (!running && it) {
running = true
scope.launch { scope.launch {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
Anilist.userid = PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
?.toIntOrNull()
if (Anilist.userid == null) {
getUserId(requireContext()) { getUserId(requireContext()) {
load() load()
} }
} else {
CoroutineScope(Dispatchers.IO).launch {
getUserId(requireContext()) {
load()
}
}
}
model.loaded = true model.loaded = true
model.loadTrending() model.loadTrending()
model.loadAll() model.loadAll()
@ -272,6 +285,7 @@ class MangaFragment : Fragment() {
} }
live.postValue(false) live.postValue(false)
_binding?.mangaRefresh?.isRefreshing = false _binding?.mangaRefresh?.isRefreshing = false
running = false
} }
} }
} }

View file

@ -59,6 +59,20 @@ object Logger {
} }
} }
fun log(level: Int, message: String, tag: String = "Internal Logger") {
val trace = Thread.currentThread().stackTrace[3]
loggerExecutor.execute {
if (file == null) Log.println(level, tag, message)
else {
val className = trace.className
val methodName = trace.methodName
val lineNumber = trace.lineNumber
file?.appendText("date/time: ${Date()} | $className.$methodName($lineNumber)\n")
file?.appendText("message: $message\n-\n")
}
}
}
fun log(e: Exception) { fun log(e: Exception) {
loggerExecutor.execute { loggerExecutor.execute {
if (file == null) e.printStackTrace() else { if (file == null) e.printStackTrace() else {