Refactor star icon handling
Extracted all favorite handling to two functions. Makes it a little bit more readable.
This commit is contained in:
parent
4859b77ac0
commit
4a55bc0f1d
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user