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 {
applicationIdSuffix ".beta"
versionNameSuffix "-beta02"
versionNameSuffix "-beta03"
manifestPlaceholders.icon_placeholder = "@mipmap/ic_launcher_beta"
manifestPlaceholders.icon_placeholder_round = "@mipmap/ic_launcher_beta_round"
debuggable false

View file

@ -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()

View file

@ -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,43 +336,35 @@ 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
binding.mainProgressBar.visibility = View.GONE
val mainViewPager = binding.viewpager
mainViewPager.isUserInputEnabled = false
mainViewPager.adapter =
ViewPagerAdapter(supportFragmentManager, lifecycle)
mainViewPager.setPageTransformer(ZoomOutPageTransformer())
navbar.setOnTabSelectListener(object :
AnimatedBottomBar.OnTabSelectListener {
override fun onTabSelected(
lastIndex: Int,
lastTab: AnimatedBottomBar.Tab?,
newIndex: Int,
newTab: AnimatedBottomBar.Tab
) {
navbar.animate().translationZ(12f).setDuration(200).start()
selectedOption = newIndex
mainViewPager.setCurrentItem(newIndex, false)
}
})
if (mainViewPager.currentItem != selectedOption) {
navbar.selectTabAt(selectedOption)
mainViewPager.post {
mainViewPager.setCurrentItem(
selectedOption,
false
)
}
}
} else {
binding.mainProgressBar.visibility = View.GONE
}
val navbar = binding.includedNavbar.navbar
bottomBar = navbar
navbar.visibility = View.VISIBLE
binding.mainProgressBar.visibility = View.GONE
val mainViewPager = binding.viewpager
mainViewPager.isUserInputEnabled = false
mainViewPager.adapter =
ViewPagerAdapter(supportFragmentManager, lifecycle)
mainViewPager.setPageTransformer(ZoomOutPageTransformer())
navbar.setOnTabSelectListener(object :
AnimatedBottomBar.OnTabSelectListener {
override fun onTabSelected(
lastIndex: Int,
lastTab: AnimatedBottomBar.Tab?,
newIndex: Int,
newTab: AnimatedBottomBar.Tab
) {
navbar.animate().translationZ(12f).setDuration(200).start()
selectedOption = newIndex
mainViewPager.setCurrentItem(newIndex, false)
}
})
if (mainViewPager.currentItem != selectedOption) {
navbar.selectTabAt(selectedOption)
mainViewPager.post {
mainViewPager.setCurrentItem(
selectedOption,
false
)
}
}
//Load Data

View file

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

View file

@ -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
}

View file

@ -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)

View file

@ -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,13 +269,25 @@ 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) {
getUserId(requireContext()) {
load()
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)
@ -287,6 +300,7 @@ class AnimeFragment : Fragment() {
}
live.postValue(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.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
getUserId(requireContext()) {
load()
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.setListImages()
Logger.log("HomeFragment: Refreshing")
CoroutineScope(Dispatchers.IO).launch {
model.setListImages()
}
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
}
}

View file

@ -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,13 +254,25 @@ 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) {
getUserId(requireContext()) {
load()
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()
@ -272,6 +285,7 @@ class MangaFragment : Fragment() {
}
live.postValue(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) {
loggerExecutor.execute {
if (file == null) e.printStackTrace() else {