Add some keyboard shortcuts

This commit is contained in:
William Brawner 2020-08-23 05:02:36 -07:00
parent a1e114b162
commit c16ed3cbbe
2 changed files with 64 additions and 52 deletions

View file

@ -9,12 +9,11 @@ import android.content.res.Configuration
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.*
import android.view.View
import android.view.ViewGroup
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -56,6 +55,13 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_edit, menu)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
menu.findItem(R.id.action_save_as)?.setAlphabeticShortcut('S', KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON)
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater.inflate(R.layout.fragment_main, container, false) inflater.inflate(R.layout.fragment_main, container, false)
@ -63,53 +69,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
with(findNavController()) { with(findNavController()) {
appBarConfiguration = AppBarConfiguration(graph, drawerLayout) appBarConfiguration = AppBarConfiguration(graph, drawerLayout)
toolbar.setupWithNavController(this, appBarConfiguration!!) toolbar.setupWithNavController(this, appBarConfiguration!!)
toolbar.inflateMenu(R.menu.menu_edit) (activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
toolbar.setOnMenuItemClickListener { item ->
return@setOnMenuItemClickListener when (item.itemId) {
R.id.action_save -> {
launch {
if (!viewModel.save(requireContext())) {
requestFileOp(REQUEST_SAVE_FILE)
} else {
Toast.makeText(
requireContext(),
getString(R.string.file_saved, viewModel.fileName.value),
Toast.LENGTH_SHORT
).show()
}
}
true
}
R.id.action_save_as -> {
requestFileOp(REQUEST_SAVE_FILE)
true
}
R.id.action_share -> {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_TEXT, viewModel.markdownUpdates.value)
shareIntent.type = "text/plain"
startActivity(Intent.createChooser(
shareIntent,
getString(R.string.share_file)
))
true
}
R.id.action_load -> {
requestFileOp(REQUEST_OPEN_FILE)
true
}
R.id.action_new -> {
promptSaveOrDiscardChanges()
true
}
R.id.action_lock_swipe -> {
item.isChecked = !item.isChecked
pager!!.setSwipeLocked(item.isChecked)
true
}
else -> item.onNavDestinationSelected(findNavController())
}
}
navigationView.setupWithNavController(this) navigationView.setupWithNavController(this)
} }
val adapter = EditPagerAdapter(childFragmentManager, view.context) val adapter = EditPagerAdapter(childFragmentManager, view.context)
@ -127,6 +87,53 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba
}) })
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_save -> {
launch {
if (!viewModel.save(requireContext())) {
requestFileOp(REQUEST_SAVE_FILE)
} else {
Toast.makeText(
requireContext(),
getString(R.string.file_saved, viewModel.fileName.value),
Toast.LENGTH_SHORT
).show()
}
}
true
}
R.id.action_save_as -> {
requestFileOp(REQUEST_SAVE_FILE)
true
}
R.id.action_share -> {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.putExtra(Intent.EXTRA_TEXT, viewModel.markdownUpdates.value)
shareIntent.type = "text/plain"
startActivity(Intent.createChooser(
shareIntent,
getString(R.string.share_file)
))
true
}
R.id.action_load -> {
requestFileOp(REQUEST_OPEN_FILE)
true
}
R.id.action_new -> {
promptSaveOrDiscardChanges()
true
}
R.id.action_lock_swipe -> {
item.isChecked = !item.isChecked
pager!!.setSwipeLocked(item.isChecked)
true
}
else -> item.onNavDestinationSelected(findNavController())
}
}
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
launch { launch {

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/action_share" android:id="@+id/action_share"
@ -9,17 +10,21 @@
<item <item
android:id="@+id/action_new" android:id="@+id/action_new"
android:title="@string/action_new" android:title="@string/action_new"
android:alphabeticShortcut="N"
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@+id/action_load" android:id="@+id/action_load"
android:title="@string/action_open" android:title="@string/action_open"
android:alphabeticShortcut="O"
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@+id/action_save" android:id="@+id/action_save"
android:alphabeticShortcut="S"
android:title="@string/action_save" /> android:title="@string/action_save" />
<item <item
android:id="@+id/action_save_as" android:id="@+id/action_save_as"
android:title="@string/action_save_as" /> android:title="@string/action_save_as"
tools:ignore="UnusedAttribute" />
<item <item
android:id="@+id/action_lock_swipe" android:id="@+id/action_lock_swipe"
android:checkable="true" android:checkable="true"