From 93d99192b3e66f7e75478db6e88c7823e367a7db Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 11 Mar 2025 15:56:10 +0100 Subject: [PATCH 1/2] Don't restart activity changing alignment When changing alignment in the reader we were restarting the reader activity to reload. Doing this led to reloading the article which was initially opened every time you changed alignment. Now, when changing the alignment we retain all existing fragments but command all of them to update their alignment setting. --- .../android/ReaderActivity.kt | 28 ++++++++----------- .../android/fragments/ArticleFragment.kt | 4 ++- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index 55f42ac..013432c 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -209,28 +209,24 @@ class ReaderActivity : } } - R.id.align_left -> { - switchAlignmentSetting(AppSettingsService.ALIGN_LEFT) - refreshFragment() - } + R.id.align_left -> switchAlignmentSetting(AppSettingsService.ALIGN_LEFT) - R.id.align_justify -> { - switchAlignmentSetting(AppSettingsService.JUSTIFY) - refreshFragment() - } + R.id.align_justify -> switchAlignmentSetting(AppSettingsService.JUSTIFY) } return super.onOptionsItemSelected(item) } - private fun switchAlignmentSetting(allignment: Int) { - appSettingsService.changeAllignment(allignment) + private fun switchAlignmentSetting(alignment: Int) { + appSettingsService.changeAllignment(alignment) alignmentMenu() - } - private fun refreshFragment() { - finish() - overridePendingTransition(0, 0) - startActivity(intent) - overridePendingTransition(0, 0) + val fragmentManager = supportFragmentManager + val fragments = fragmentManager.fragments + + for (fragment in fragments) { + if (fragment is ArticleFragment) { + fragment.refreshAlignment() + } + } } } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index 671c20b..b7e2f95 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -263,13 +263,15 @@ class ArticleFragment : ) } - private fun refreshAlignment() { + fun refreshAlignment() { textAlignment = when (appSettingsService.getActiveAllignment()) { 1 -> "justify" 2 -> "left" else -> "justify" } + + htmlToWebview() } @Suppress("detekt:SwallowedException") -- 2.34.1 From c3ee07dd85c29a8904a26ee08d6962e3a6e1618b Mon Sep 17 00:00:00 2001 From: davidoskky Date: Tue, 11 Mar 2025 17:01:45 +0100 Subject: [PATCH 2/2] Refactor star icon handling Extracted all favorite handling to two functions. Makes it a little bit more readable. --- .../android/ReaderActivity.kt | 119 ++++++------------ 1 file changed, 41 insertions(+), 78 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index 013432c..35677d2 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -37,22 +37,6 @@ class ReaderActivity : private val repository: Repository by instance() private val appSettingsService: AppSettingsService by instance() - private fun showMenuItem(willAddToFavorite: Boolean) { - if (willAddToFavorite) { - toolbarMenu.findItem(R.id.star).icon?.setTint(Color.WHITE) - } else { - toolbarMenu.findItem(R.id.star).icon?.setTint(Color.RED) - } - } - - private fun canFavorite() { - showMenuItem(true) - } - - private fun canRemoveFromFavorite() { - showMenuItem(false) - } - @Suppress("detekt:SwallowedException") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -73,14 +57,21 @@ class ReaderActivity : finish() } - try { - readItem(allItems[currentItem]) - } catch (e: IndexOutOfBoundsException) { - finish() - } + readItem() binding.pager.adapter = ScreenSlidePagerAdapter(this) binding.pager.setCurrentItem(currentItem, false) + + binding.pager.registerOnPageChangeCallback( + object : ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + currentItem = position + updateStarIcon() + readItem() + } + }, + ) } override fun onResume() { @@ -89,14 +80,20 @@ class ReaderActivity : binding.indicator.setViewPager(binding.pager) } - private fun readItem(item: SelfossModel.Item) { - if (appSettingsService.isMarkOnScrollEnabled() && !appSettingsService.getPublicAccess()) { + private fun readItem() { + val item = allItems.getOrNull(currentItem) + if (appSettingsService.isMarkOnScrollEnabled() && !appSettingsService.getPublicAccess() && item != null) { CoroutineScope(Dispatchers.IO).launch { repository.markAsRead(item) } } } + private fun updateStarIcon() { + val isStarred = allItems.getOrNull(currentItem)?.starred ?: false + toolbarMenu.findItem(R.id.star)?.icon?.setTint(if (isStarred) Color.RED else Color.WHITE) + } + override fun onSaveInstanceState(oldInstanceState: Bundle) { super.onSaveInstanceState(oldInstanceState) oldInstanceState.clear() @@ -141,8 +138,7 @@ class ReaderActivity : } override fun onCreateOptionsMenu(menu: Menu): Boolean { - val inflater = menuInflater - inflater.inflate(R.menu.reader_menu, menu) + menuInflater.inflate(R.menu.reader_menu, menu) toolbarMenu = menu alignmentMenu() @@ -150,72 +146,39 @@ class ReaderActivity : if (appSettingsService.getPublicAccess()) { menu.removeItem(R.id.star) } else { - if (allItems.isNotEmpty() && allItems[currentItem].starred) { - canRemoveFromFavorite() - } else { - canFavorite() - } - - binding.pager.registerOnPageChangeCallback( - object : ViewPager2.OnPageChangeCallback() { - override fun onPageSelected(position: Int) { - super.onPageSelected(position) - - if (!allItems.isNullOrEmpty() && allItems.size >= position) { - if (allItems[position].starred) { - canRemoveFromFavorite() - } else { - canFavorite() - } - readItem(allItems[position]) - } - } - }, - ) + updateStarIcon() } return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { - fun afterSave() { - allItems[binding.pager.currentItem] = - allItems[binding.pager.currentItem].toggleStar() - canRemoveFromFavorite() - } - - fun afterUnsave() { - allItems[binding.pager.currentItem] = allItems[binding.pager.currentItem].toggleStar() - canFavorite() - } - when (item.itemId) { - android.R.id.home -> { - onBackPressedDispatcher.onBackPressed() - return true - } - - R.id.star -> { - if (allItems[binding.pager.currentItem].starred) { - CoroutineScope(Dispatchers.IO).launch { - repository.unstarr(allItems[binding.pager.currentItem]) - } - afterUnsave() - } else { - CoroutineScope(Dispatchers.IO).launch { - repository.starr(allItems[binding.pager.currentItem]) - } - afterSave() - } - } - + android.R.id.home -> onBackPressedDispatcher.onBackPressed() + R.id.star -> toggleFavorite() R.id.align_left -> switchAlignmentSetting(AppSettingsService.ALIGN_LEFT) - R.id.align_justify -> switchAlignmentSetting(AppSettingsService.JUSTIFY) } return super.onOptionsItemSelected(item) } + private fun toggleFavorite() { + val item = allItems.getOrNull(currentItem) ?: return + + val starred = item.starred + + CoroutineScope(Dispatchers.IO).launch { + if (starred) { + repository.unstarr(item) + } else { + repository.starr(item) + } + } + + item.toggleStar() + updateStarIcon() + } + private fun switchAlignmentSetting(alignment: Int) { appSettingsService.changeAllignment(alignment) alignmentMenu() -- 2.34.1