Skip to content

Commit

Permalink
Allow to press "open" immediately in the "article added dialog"
Browse files Browse the repository at this point in the history
The article will be opened as soon as it becomes available (if the dialog is not closed).
  • Loading branch information
di72nn committed Mar 18, 2020
1 parent f95fac0 commit f979b46
Showing 1 changed file with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -16,6 +14,7 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.CircularProgressDrawable;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
Expand Down Expand Up @@ -68,6 +67,8 @@ public class EditAddedArticleActivity extends AppCompatActivity {
private boolean archived;
private boolean favorite;

private boolean openPending;

@Override
protected void onCreate(Bundle savedInstanceState) {
Themes.applyDialogTheme(this);
Expand Down Expand Up @@ -121,6 +122,8 @@ public void onArticlesChangedEvent(ArticlesChangedEvent event) {

if (event.isChangedAny(articleId, CHANGE_SET_FOR_REINIT)) {
init();

openIfPending();
}
}

Expand All @@ -132,6 +135,8 @@ public void onLocalArticleReplacedEvent(LocalArticleReplacedEvent event) {
articleId = event.getArticleId();

init();

openIfPending();
}
}

Expand Down Expand Up @@ -169,13 +174,55 @@ public void onTagClick(View view) {
}

public void onOpenClick(View view) {
if (canOpen()) {
openArticle();
} else {
openLater();
}
}

private boolean canOpen() {
return articleId != -1 && article != null;
}

private void openArticle() {
Intent intent = new Intent(this, ReadArticleActivity.class);
intent.putExtra(ReadArticleActivity.EXTRA_ID, article.getId());
startActivity(intent);

finish();
}

private void openLater() {
cancelAutoclose();

openPending = true;

Drawable oldImage = openButton.getDrawable();
CircularProgressDrawable progressDrawable = new CircularProgressDrawable(this) {
@Override
public int getIntrinsicWidth() {
return oldImage.getIntrinsicWidth();
}

@Override
public int getIntrinsicHeight() {
return oldImage.getIntrinsicHeight();
}
};
progressDrawable.setStyle(CircularProgressDrawable.DEFAULT);

openButton.setImageDrawable(progressDrawable);
progressDrawable.start();

openButton.setEnabled(false);
openButton.setClickable(false);
}

private void openIfPending() {
if (openPending && canOpen()) openArticle();
}

private void init() {
loadArticle();

Expand Down Expand Up @@ -219,8 +266,6 @@ private void updateViews() {
archived ? R.attr.icon_read_undo : R.attr.icon_read));
archiveButton.setContentDescription(getString(
archived ? R.string.btnMarkUnread : R.string.btnMarkRead));

setEnabled(openButton, articleId != -1 && article != null);
}

private int resolveResource(int resId) {
Expand All @@ -230,18 +275,6 @@ private int resolveResource(int resId) {
return value.resourceId;
}

private void setEnabled(ImageButton button, boolean enabled) {
button.setEnabled(enabled);
button.setClickable(enabled);

Drawable background = button.getBackground();
if (enabled) {
background.setColorFilter(null);
} else {
background.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
}
}

private void scheduleAutoclose() {
handler = new Handler();
handler.postDelayed(autocloseRunnable = this::finish, 7000);
Expand Down

0 comments on commit f979b46

Please sign in to comment.