fix: optimization # 2
This commit is contained in:
parent
14f29d09df
commit
b187cf06be
10 changed files with 123 additions and 70 deletions
|
@ -51,7 +51,7 @@ android {
|
|||
}
|
||||
debug {
|
||||
applicationIdSuffix ".beta"
|
||||
versionNameSuffix "-beta02"
|
||||
versionNameSuffix "-beta03"
|
||||
manifestPlaceholders.icon_placeholder = "@mipmap/ic_launcher_beta"
|
||||
manifestPlaceholders.icon_placeholder_round = "@mipmap/ic_launcher_beta_round"
|
||||
debuggable false
|
||||
|
|
|
@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
|||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.multidex.MultiDex
|
||||
import androidx.multidex.MultiDexApplication
|
||||
import ani.dantotsu.addons.download.DownloadAddonManager
|
||||
|
@ -93,7 +94,7 @@ class App : MultiDexApplication() {
|
|||
|
||||
Logger.init(this)
|
||||
Thread.setDefaultUncaughtExceptionHandler(FinalExceptionHandler())
|
||||
Logger.log("App: Logging started")
|
||||
Logger.log(Log.WARN, "App: Logging started")
|
||||
|
||||
initializeNetwork()
|
||||
|
||||
|
|
|
@ -33,8 +33,8 @@ import androidx.lifecycle.Lifecycle
|
|||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.media3.common.util.UnstableApi
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import ani.dantotsu.addons.torrent.TorrentServerService
|
||||
import ani.dantotsu.addons.torrent.TorrentAddonManager
|
||||
import ani.dantotsu.addons.torrent.TorrentServerService
|
||||
import ani.dantotsu.connections.anilist.Anilist
|
||||
import ani.dantotsu.connections.anilist.AnilistHomeViewModel
|
||||
import ani.dantotsu.databinding.ActivityMainBinding
|
||||
|
@ -336,9 +336,6 @@ class MainActivity : AppCompatActivity() {
|
|||
startActivity(Intent(this, NoInternet::class.java))
|
||||
} else {
|
||||
val model: AnilistHomeViewModel by viewModels()
|
||||
model.genres.observe(this) {
|
||||
if (it != null) {
|
||||
if (it) {
|
||||
val navbar = binding.includedNavbar.navbar
|
||||
bottomBar = navbar
|
||||
navbar.visibility = View.VISIBLE
|
||||
|
@ -370,11 +367,6 @@ class MainActivity : AppCompatActivity() {
|
|||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
binding.mainProgressBar.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
//Load Data
|
||||
if (!load && !launched) {
|
||||
scope.launch(Dispatchers.IO) {
|
||||
|
|
|
@ -35,6 +35,8 @@ object Anilist {
|
|||
|
||||
var rateLimitReset: Long = 0
|
||||
|
||||
var initialized = false
|
||||
|
||||
val sortBy = listOf(
|
||||
"SCORE_DESC",
|
||||
"POPULARITY_DESC",
|
||||
|
|
|
@ -57,6 +57,7 @@ class AnilistQueries {
|
|||
Anilist.unreadNotificationCount = user.unreadNotificationCount ?: 0
|
||||
val unread = PrefManager.getVal<Int>(PrefName.UnreadCommentNotifications)
|
||||
Anilist.unreadNotificationCount += unread
|
||||
Anilist.initialized = true
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import androidx.fragment.app.FragmentActivity
|
|||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import ani.dantotsu.BuildConfig
|
||||
import ani.dantotsu.R
|
||||
import ani.dantotsu.connections.discord.Discord
|
||||
|
@ -12,31 +13,26 @@ import ani.dantotsu.connections.mal.MAL
|
|||
import ani.dantotsu.media.Media
|
||||
import ani.dantotsu.others.AppUpdater
|
||||
import ani.dantotsu.profile.User
|
||||
import ani.dantotsu.profile.activity.ActivityItemBuilder
|
||||
import ani.dantotsu.settings.saving.PrefManager
|
||||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.snackString
|
||||
import ani.dantotsu.tryWithSuspend
|
||||
import ani.dantotsu.util.Logger
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
suspend fun getUserId(context: Context, block: () -> Unit) {
|
||||
val anilist = if (Anilist.userid == null && Anilist.token != null) {
|
||||
if (!Anilist.initialized) {
|
||||
if (Anilist.query.getUserData()) {
|
||||
tryWithSuspend {
|
||||
if (MAL.token != null && !MAL.query.getUserData())
|
||||
snackString(context.getString(R.string.error_loading_mal_user_data))
|
||||
}
|
||||
true
|
||||
} else {
|
||||
snackString(context.getString(R.string.error_loading_anilist_user_data))
|
||||
false
|
||||
}
|
||||
} else true
|
||||
|
||||
if (anilist) block.invoke()
|
||||
}
|
||||
block.invoke()
|
||||
}
|
||||
|
||||
class AnilistHomeViewModel : ViewModel() {
|
||||
|
@ -90,6 +86,7 @@ class AnilistHomeViewModel : ViewModel() {
|
|||
MutableLiveData<ArrayList<Media>>(null)
|
||||
|
||||
fun getHidden(): LiveData<ArrayList<Media>> = hidden
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
suspend fun initHomePage() {
|
||||
val res = Anilist.query.initHomePage()
|
||||
|
@ -104,15 +101,20 @@ class AnilistHomeViewModel : ViewModel() {
|
|||
res["status"]?.let { userStatus.postValue(it as ArrayList<User>?) }
|
||||
}
|
||||
|
||||
|
||||
suspend fun loadMain(context: FragmentActivity) {
|
||||
Anilist.getSavedToken()
|
||||
MAL.getSavedToken()
|
||||
Discord.getSavedToken()
|
||||
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)
|
||||
|
|
|
@ -36,6 +36,7 @@ import ani.dantotsu.settings.saving.PrefManager
|
|||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.snackString
|
||||
import ani.dantotsu.statusBarHeight
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -268,14 +269,26 @@ class AnimeFragment : Fragment() {
|
|||
true
|
||||
}
|
||||
|
||||
var running = false
|
||||
val live = Refresh.activity.getOrPut(this.hashCode()) { MutableLiveData(false) }
|
||||
live.observe(viewLifecycleOwner) {
|
||||
if (it) {
|
||||
if (it && !running) {
|
||||
running = true
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
Anilist.userid = PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
|
||||
?.toIntOrNull()
|
||||
if (Anilist.userid == null) {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
} else {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
}
|
||||
}
|
||||
model.loaded = true
|
||||
model.loadTrending(1)
|
||||
model.loadAll()
|
||||
|
@ -287,6 +300,7 @@ class AnimeFragment : Fragment() {
|
|||
}
|
||||
live.postValue(false)
|
||||
_binding?.animeRefresh?.isRefreshing = false
|
||||
running = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,9 +48,9 @@ import ani.dantotsu.settings.saving.PrefName
|
|||
import ani.dantotsu.snackString
|
||||
import ani.dantotsu.statusBarHeight
|
||||
import ani.dantotsu.util.Logger
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
@ -379,7 +379,8 @@ class HomeFragment : Fragment() {
|
|||
model.getHidden().observe(viewLifecycleOwner) {
|
||||
if (it != null) {
|
||||
if (it.isNotEmpty()) {
|
||||
binding.homeHiddenItemsRecyclerView.adapter = MediaAdaptor(0, it, requireActivity())
|
||||
binding.homeHiddenItemsRecyclerView.adapter =
|
||||
MediaAdaptor(0, it, requireActivity())
|
||||
binding.homeHiddenItemsRecyclerView.layoutManager = LinearLayoutManager(
|
||||
requireContext(),
|
||||
LinearLayoutManager.HORIZONTAL,
|
||||
|
@ -393,7 +394,8 @@ class HomeFragment : Fragment() {
|
|||
}
|
||||
binding.homeHiddenItemsMore.setSafeOnClickListener { _ ->
|
||||
ContextCompat.startActivity(
|
||||
requireActivity(), Intent(requireActivity(), MediaListViewActivity::class.java)
|
||||
requireActivity(),
|
||||
Intent(requireActivity(), MediaListViewActivity::class.java)
|
||||
.putExtra("title", getString(R.string.hidden))
|
||||
.putExtra("media", it),
|
||||
null
|
||||
|
@ -403,8 +405,7 @@ class HomeFragment : Fragment() {
|
|||
binding.homeHiddenItemsContainer.visibility = View.GONE
|
||||
true
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
binding.homeContinueWatch.setOnLongClickListener {
|
||||
snackString(getString(R.string.no_hidden_items))
|
||||
true
|
||||
|
@ -457,17 +458,29 @@ class HomeFragment : Fragment() {
|
|||
val live = Refresh.activity.getOrPut(1) { MutableLiveData(true) }
|
||||
live.observe(viewLifecycleOwner)
|
||||
{
|
||||
if (it && !running) {
|
||||
if (!running && it) {
|
||||
running = true
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
//Get userData First
|
||||
Anilist.userid =
|
||||
PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
|
||||
?.toIntOrNull()
|
||||
if (Anilist.userid == null) {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
} else {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
}
|
||||
}
|
||||
model.loaded = true
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
model.setListImages()
|
||||
Logger.log("HomeFragment: Refreshing")
|
||||
}
|
||||
var empty = true
|
||||
val homeLayoutShow: List<Boolean> =
|
||||
PrefManager.getVal(PrefName.HomeLayout)
|
||||
|
@ -483,9 +496,9 @@ class HomeFragment : Fragment() {
|
|||
}
|
||||
live.postValue(false)
|
||||
_binding?.homeRefresh?.isRefreshing = false
|
||||
running = false
|
||||
}
|
||||
binding.homeHiddenItemsContainer.visibility = View.GONE
|
||||
running = false
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import ani.dantotsu.settings.saving.PrefManager
|
|||
import ani.dantotsu.settings.saving.PrefName
|
||||
import ani.dantotsu.snackString
|
||||
import ani.dantotsu.statusBarHeight
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -253,14 +254,26 @@ class MangaFragment : Fragment() {
|
|||
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) {
|
||||
if (it) {
|
||||
if (!running && it) {
|
||||
running = true
|
||||
scope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
Anilist.userid = PrefManager.getNullableVal<String>(PrefName.AnilistUserId, null)
|
||||
?.toIntOrNull()
|
||||
if (Anilist.userid == null) {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
} else {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
getUserId(requireContext()) {
|
||||
load()
|
||||
}
|
||||
}
|
||||
}
|
||||
model.loaded = true
|
||||
model.loadTrending()
|
||||
model.loadAll()
|
||||
|
@ -272,6 +285,7 @@ class MangaFragment : Fragment() {
|
|||
}
|
||||
live.postValue(false)
|
||||
_binding?.mangaRefresh?.isRefreshing = false
|
||||
running = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
loggerExecutor.execute {
|
||||
if (file == null) e.printStackTrace() else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue