Skip to content

Commit

Permalink
Add action to edit issue name in ArgumentsFragment
Browse files Browse the repository at this point in the history
So we don't need to go back to give the issue a name.
  • Loading branch information
markusfisch committed Nov 7, 2019
1 parent 1ecd667 commit 7bd8513
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ class ArgumentsFragment : Fragment() {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.edit_issue -> {
askForIssueName(
context,
issueId,
db.getIssueName(issueId)
) { title ->
activity?.title = title
}
true
}
R.id.sort_arguments -> {
sortArguments()
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ class IssuesFragment : Fragment() {
): Boolean {
return when (item.itemId) {
R.id.edit_issue -> {
askForIssueName(issue.id, getItemText(issue.position))
askForIssueName(
context,
issue.id,
getItemText(issue.position)
) {
updateList()
}
closeActionMode()
true
}
Expand Down Expand Up @@ -150,35 +156,33 @@ class IssuesFragment : Fragment() {
)
}

// dialogs don't have a parent layout
@SuppressLint("InflateParams")
private fun askForIssueName(issueId: Long, text: String?) {
val context = activity
val view = LayoutInflater.from(context).inflate(
R.layout.dialog_enter_name, null
)
val nameView = view.findViewById<EditText>(R.id.name)
nameView.setText(text)
AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ ->
updateIssueName(
issueId,
nameView.text.toString()
)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}

private fun updateIssueName(id: Long, name: String) {
db.updateIssueName(id, name)
updateList()
}

private fun updateList() {
adapter.changeCursor(db.getIssues())
}

private data class Issue(var id: Long, var position: Int)
}

// dialogs don't have a parent layout
@SuppressLint("InflateParams")
fun askForIssueName(
context: Context,
issueId: Long,
text: String?,
update: (name: String) -> Unit
) {
val view = LayoutInflater.from(context).inflate(
R.layout.dialog_enter_name, null
)
val nameView = view.findViewById<EditText>(R.id.name)
nameView.setText(text)
AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ ->
val name = nameView.text.toString()
db.updateIssueName(issueId, name)
update(name)
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
5 changes: 5 additions & 0 deletions app/src/main/res/menu/fragment_arguments.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:material="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/edit_issue"
android:title="@string/edit_issue"
android:icon="@drawable/ic_action_edit"
material:showAsAction="ifRoom"/>
<item
android:id="@+id/sort_arguments"
android:title="@string/sort_arguments"
Expand Down

0 comments on commit 7bd8513

Please sign in to comment.