From e82c9dcd2a63faf5fd706fd56a9c9b09aa232821 Mon Sep 17 00:00:00 2001 From: Ivan Kupalov Date: Tue, 6 Mar 2018 00:43:13 +0300 Subject: [PATCH] Compose close dialog + focus fix (#539) --- .../keylesspalace/tusky/ComposeActivity.java | 75 ++++++++++++------- app/src/main/res/layout/activity_compose.xml | 21 +++--- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 3f517f36..05b9c911 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -207,11 +207,11 @@ public final class ComposeActivity extends BaseActivity // setup the account image AccountEntity activeAccount = TuskyApplication.getAccountManager().getActiveAccount(); - if(activeAccount != null) { + if (activeAccount != null) { ImageView composeAvatar = findViewById(R.id.composeAvatar); - if(TextUtils.isEmpty(activeAccount.getProfilePictureUrl())) { + if (TextUtils.isEmpty(activeAccount.getProfilePictureUrl())) { composeAvatar.setImageResource(R.drawable.avatar_default); } else { Picasso.with(this).load(activeAccount.getProfilePictureUrl()) @@ -476,6 +476,7 @@ public final class ComposeActivity extends BaseActivity } } + textEditor.requestFocus(); } @Override @@ -1058,7 +1059,7 @@ public final class ComposeActivity extends BaseActivity private void onMediaPick() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { + != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); @@ -1239,9 +1240,30 @@ public final class ComposeActivity extends BaseActivity input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); input.setText(item.description); + DialogInterface.OnClickListener okListener = (dialog, which) -> { + mastodonApi.updateMedia(item.id, input.getText().toString()) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + Attachment attachment = response.body(); + if (response.isSuccessful() && attachment != null) { + item.description = attachment.getDescription(); + dialog.dismiss(); + } else { + showFailedCaptionMessage(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showFailedCaptionMessage(); + } + }); + }; + AlertDialog dialog = new AlertDialog.Builder(this) .setView(dialogLayout) - .setPositiveButton(android.R.string.ok, null) + .setPositiveButton(android.R.string.ok, okListener) .setNegativeButton(android.R.string.cancel, null) .create(); @@ -1252,26 +1274,6 @@ public final class ComposeActivity extends BaseActivity WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); } - dialog.setOnShowListener(dialogInterface -> { - dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> mastodonApi.updateMedia(item.id, - input.getText().toString()).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - Attachment attachment = response.body(); - if (response.isSuccessful() && attachment != null) { - item.description = attachment.getDescription(); - dialog.dismiss(); - } else { - showFailedCaptionMessage(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - showFailedCaptionMessage(); - } - })); - }); dialog.show(); } @@ -1547,15 +1549,34 @@ public final class ComposeActivity extends BaseActivity @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case android.R.id.home: { - onBackPressed(); + case android.R.id.home: + handleCloseButton(); return true; - } } return super.onOptionsItemSelected(item); } + @Override + public void onBackPressed() { + // Acting like a teen: deliberately ignoring parent. + handleCloseButton(); + } + + private void handleCloseButton() { + if (!TextUtils.isEmpty(textEditor.getText()) + || !TextUtils.isEmpty(contentWarningEditor.getText()) + || !mediaQueued.isEmpty()) { + new AlertDialog.Builder(this) + .setTitle("Close the toot without saving?") + .setPositiveButton(android.R.string.yes, (d, w) -> finish()) + .setNegativeButton(android.R.string.no, null) + .show(); + } else { + finish(); + } + } + @Override public List searchAccounts(String mention) { ArrayList resultList = new ArrayList<>(); diff --git a/app/src/main/res/layout/activity_compose.xml b/app/src/main/res/layout/activity_compose.xml index 4d19f6b3..67454268 100644 --- a/app/src/main/res/layout/activity_compose.xml +++ b/app/src/main/res/layout/activity_compose.xml @@ -17,31 +17,31 @@ android:layout_height="?attr/actionBarSize" android:layout_marginBottom="8dp" android:background="@android:color/transparent"> + @@ -63,7 +64,6 @@ android:orientation="vertical"> + android:paddingRight="16dp" + android:textSize="?attr/status_text_medium" /> + android:textColor="?android:textColorPrimary" + android:textSize="?attr/status_text_medium" />