Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
- Rename variable for clarity
- Override the MenuProvider functions, and call super.onCreateMenu() so
  other menu items (like "Send error report") are shown
- Document the onPrepareMenu behaviour and link to relevant Google bug
  • Loading branch information
nikclayton committed Mar 13, 2024
1 parent 59edcaa commit df5d66e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions app/src/main/java/app/pachli/ViewMediaActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.os.Bundle
import android.os.Environment
import android.transition.Transition
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.webkit.MimeTypeMap
Expand Down Expand Up @@ -91,12 +92,13 @@ class ViewMediaActivity : BaseActivity(), MediaActionsListener {
/** True if a download to share media is in progress */
private var isDownloading: Boolean = false

/** True if the Toolbar options menu is being created */
private var isOptionsMenuBeingCreated = false
/** True if a call to [onPrepareMenu] represents a user-initiated action */
private var respondToPrepareMenu = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
addMenuProvider(this)

supportPostponeEnterTransition()

Expand Down Expand Up @@ -161,25 +163,26 @@ class ViewMediaActivity : BaseActivity(), MediaActionsListener {
)
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
isOptionsMenuBeingCreated = true
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
super.onCreateMenu(menu, menuInflater)

menuInflater.inflate(R.menu.view_media_toolbar, menu)
// We don't support 'open status' from single image views
menu.findItem(R.id.action_open_status)?.isVisible = (attachmentViewData != null)
return true
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
if (isOptionsMenuBeingCreated) {
isOptionsMenuBeingCreated = false
} else {
// The overflow menu is being opened
override fun onPrepareMenu(menu: Menu) {
menu.findItem(R.id.action_share_media)?.isEnabled = !isDownloading

// onPrepareMenu is called immediately after onCreateMenu when the activity
// is created (https://issuetracker.google.com/issues/329322653), and this is
// not in response to user action. Ignore the first call, respond to
// subsequent calls.
if (respondToPrepareMenu) {
viewModel.onToolbarMenuInteraction()
} else {
respondToPrepareMenu = true
}

menu?.findItem(R.id.action_share_media)?.isEnabled = !isDownloading
return true
}

override fun onMediaReady() {
Expand Down

0 comments on commit df5d66e

Please sign in to comment.