Skip to content

Commit a899c07

Browse files
committed
Only enable save menu when title is non-empty
1 parent bd35edc commit a899c07

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

app/res/menu/issue_edit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
1818

1919
<item
20-
android:id="@+id/m_edit"
20+
android:id="@+id/m_apply"
21+
android:enabled="false"
2122
android:icon="@drawable/action_save"
2223
android:showAsAction="ifRoom"
2324
android:title="@string/save"/>

app/src/main/java/com/github/mobile/ui/issue/EditIssueActivity.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import static com.github.mobile.RequestCodes.ISSUE_MILESTONE_UPDATE;
2929
import android.content.Intent;
3030
import android.os.Bundle;
31+
import android.text.Editable;
32+
import android.text.TextUtils;
3133
import android.view.View;
3234
import android.view.View.OnClickListener;
3335
import android.widget.EditText;
@@ -45,6 +47,7 @@
4547
import com.github.mobile.R.string;
4648
import com.github.mobile.ui.DialogFragmentActivity;
4749
import com.github.mobile.ui.StyledText;
50+
import com.github.mobile.ui.TextWatcherAdapter;
4851
import com.github.mobile.util.AvatarLoader;
4952
import com.google.inject.Inject;
5053

@@ -149,6 +152,8 @@ public static Intent createIntent(final Issue issue,
149152

150153
private RepositoryId repository;
151154

155+
private MenuItem saveItem;
156+
152157
@Override
153158
protected void onCreate(Bundle savedInstanceState) {
154159
super.onCreate(savedInstanceState);
@@ -202,6 +207,15 @@ public void onClick(View v) {
202207
}
203208
});
204209

210+
titleText.addTextChangedListener(new TextWatcherAdapter() {
211+
212+
@Override
213+
public void afterTextChanged(Editable s) {
214+
updateSaveMenu(s);
215+
}
216+
});
217+
218+
updateSaveMenu();
205219
updateView();
206220
}
207221

@@ -277,16 +291,28 @@ protected void onSaveInstanceState(Bundle outState) {
277291
outState.putSerializable(EXTRA_ISSUE, issue);
278292
}
279293

294+
private void updateSaveMenu() {
295+
if (titleText != null)
296+
updateSaveMenu(titleText.getText());
297+
}
298+
299+
private void updateSaveMenu(final CharSequence text) {
300+
if (saveItem != null)
301+
saveItem.setEnabled(!TextUtils.isEmpty(text));
302+
}
303+
280304
@Override
281305
public boolean onCreateOptionsMenu(Menu options) {
282306
getSupportMenuInflater().inflate(menu.issue_edit, options);
307+
saveItem = options.findItem(id.m_apply);
308+
updateSaveMenu();
283309
return true;
284310
}
285311

286312
@Override
287313
public boolean onOptionsItemSelected(MenuItem item) {
288314
switch (item.getItemId()) {
289-
case id.m_edit:
315+
case id.m_apply:
290316
issue.setTitle(titleText.getText().toString());
291317
issue.setBody(bodyText.getText().toString());
292318
if (issue.getNumber() > 0)

integration-tests/src/main/java/com/github/mobile/EditIssueActivityTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
package com.github.mobile;
1717

18+
import static android.view.KeyEvent.KEYCODE_DEL;
19+
import android.view.View;
20+
import android.widget.EditText;
21+
1822
import com.github.mobile.ui.issue.EditIssueActivity;
23+
import com.viewpagerindicator.R.id;
1924

2025
import org.eclipse.egit.github.core.Repository;
2126
import org.eclipse.egit.github.core.User;
@@ -41,4 +46,21 @@ protected void setUp() throws Exception {
4146
repo.setOwner(new User().setLogin("owner"));
4247
setActivityIntent(EditIssueActivity.createIntent(repo, "an issue"));
4348
}
49+
50+
/**
51+
* Verify save menu is properly enabled/disable depending on the issue have
52+
* a non-empty title
53+
*
54+
* @throws Throwable
55+
*/
56+
public void testSaveMenuEnabled() throws Throwable {
57+
View saveMenu = view(id.m_apply);
58+
assertFalse(saveMenu.isEnabled());
59+
EditText title = editText(id.et_issue_title);
60+
focus(title);
61+
send("a");
62+
assertTrue(saveMenu.isEnabled());
63+
sendKeys(KEYCODE_DEL);
64+
assertFalse(saveMenu.isEnabled());
65+
}
4466
}

0 commit comments

Comments
 (0)