Replace toast with snackbar and optional undo action

This commit is contained in:
Martin Fietz 2016-10-12 17:51:14 +02:00
parent 21799ab22c
commit fdc7d41824
6 changed files with 35 additions and 24 deletions

View File

@ -10,6 +10,7 @@ import android.database.DataSetObserver;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
@ -26,7 +27,6 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
@ -737,7 +737,14 @@ public class MainActivity extends CastEnabledActivity implements NavDrawerActivi
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show();
View parentLayout = findViewById(R.id.drawer_layout);
Snackbar snackbar = Snackbar.make(parentLayout, event.message, Snackbar.LENGTH_SHORT);
if(event.action != null) {
snackbar.setAction(getString(R.string.undo), v -> {
event.action.run();
});
}
snackbar.show();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -31,7 +31,6 @@ import com.joanzapata.iconify.fonts.FontAwesomeIcons;
import java.util.Locale;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
@ -222,11 +221,6 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show();
}
/**
* Should be used to switch to another player activity if the mime type is
* not the correct one for the current activity.

View File

@ -8,6 +8,7 @@ import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
@ -36,6 +37,7 @@ import de.danoeh.antennapod.adapter.ChaptersListAdapter;
import de.danoeh.antennapod.adapter.NavListAdapter;
import de.danoeh.antennapod.core.asynctask.FeedRemover;
import de.danoeh.antennapod.core.dialog.ConfirmationDialog;
import de.danoeh.antennapod.core.event.MessageEvent;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedMedia;
@ -464,7 +466,17 @@ public abstract class MediaplayerInfoActivity extends MediaplayerActivity implem
}, error -> Log.e(TAG, Log.getStackTraceString(error)));
}
public void onEventMainThread(MessageEvent event) {
Log.d(TAG, "onEvent(" + event + ")");
View parentLayout = findViewById(R.id.drawer_layout);
Snackbar snackbar = Snackbar.make(parentLayout, event.message, Snackbar.LENGTH_SHORT);
if (event.action != null) {
snackbar.setAction(getString(R.string.undo), v -> {
event.action.run();
});
}
snackbar.show();
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.1'
classpath "me.tatarka:gradle-retrolambda:3.3.0"
classpath "me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2"
classpath 'com.github.triplet.gradle:play-publisher:1.1.4'

View File

@ -1,24 +1,21 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.danoeh.antennapod.core.event;
import android.support.annotation.Nullable;
public class MessageEvent {
public final String message;
@Nullable
public final Runnable action;
public MessageEvent(String message) {
this(message, null);
}
public MessageEvent(String message, Runnable action) {
this.message = message;
this.action = action;
}
}

View File

@ -854,7 +854,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
Log.d(TAG, "Setting sleep timer to " + Long.toString(waitingTime) + " milliseconds");
taskManager.setSleepTimer(waitingTime, shakeToReset, vibrate);
sendNotificationBroadcast(NOTIFICATION_TYPE_SLEEPTIMER_UPDATE, 0);
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label)));
EventBus.getDefault().post(new MessageEvent(getString(R.string.sleep_timer_enabled_label),
() -> disableSleepTimer()));
}
public void disableSleepTimer() {