Skip to content
This repository was archived by the owner on Jun 13, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
android:name="com.cocosw.undobar.example.UndoStyle"
android:label="@string/title_activity_undo_style" >
</activity>
<activity
android:name="com.cocosw.undobar.example.UndoCountdownStyle"
android:label="@string/title_activity_undo_countdown_style" >
</activity>
<activity
android:name="com.cocosw.undobar.example.RetryStyle"
android:label="RetryStyle" >
Expand Down
8 changes: 8 additions & 0 deletions example/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true"
android:text="Customize style" />
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:text="UndoBar countdown style" />

</RelativeLayout>
9 changes: 9 additions & 0 deletions example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
<resources>
<string name="app_name">UndoBarExample</string>
<string name="title_activity_undo_style">UndoStyle</string>
<string name="title_activity_undo_countdown_style">UndoCountdownStyle</string>
<string name="title_activity_customize">Customize Style</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>

<plurals name="countdown_seconds">
<item quantity="one">Dismissing in 1...</item>
<item quantity="other">Dismissing in %d...</item>
</plurals>

<string name="countdown_dismissing">Dismissing</string>

</resources>
9 changes: 6 additions & 3 deletions example/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<item name="android:padding">16dp</item>
</style>

<style name="UndoBar" parent="UndoBarKitKat"/>
<style name="UndoBarMessage" parent="UndoBarMessageKitKat"/>
<style name="UndoBarButton" parent="UndoBarButtonKitKat"/>
<style name="UndoBar" parent="UndoBarKitKat" />
<style name="UndoBarMessageContainer" parent="UndoBarMessageContainerKitKat" />
<style name="UndoBarMessage" parent="UndoBarMessageKitKat" />
<style name="UndoBarMessageCountdown" parent="UndoBarMessageCountdownKitKat" />
<style name="UndoBarButton" parent="UndoBarButtonKitKat" />

</resources>
4 changes: 4 additions & 0 deletions example/src/com/cocosw/undobar/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected void onCreate(final Bundle savedInstanceState) {
findViewById(id.button2).setOnClickListener(this);
findViewById(id.button3).setOnClickListener(this);
findViewById(id.button4).setOnClickListener(this);
findViewById(id.button5).setOnClickListener(this);
}

@Override
Expand All @@ -36,6 +37,9 @@ public void onClick(final View v) {
case id.button4:
startActivity(new Intent(this, Customize.class));
break;
case id.button5:
startActivity(new Intent(this, UndoCountdownStyle.class));
break;
default:
break;
}
Expand Down
65 changes: 65 additions & 0 deletions example/src/com/cocosw/undobar/example/UndoCountdownStyle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.cocosw.undobar.example;

import java.util.Arrays;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.cocosw.undobar.UndoBarController;
import com.cocosw.undobar.UndoBarController.CountDownFormatter;
import com.cocosw.undobar.UndoBarController.UndoListener;
import com.cocosw.undobar.UndoBarStyle;

public class UndoCountdownStyle extends ListActivity implements UndoListener
{

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
Arrays.asList(new String[] { "Item 1", "Item 2", "Item 3",
"Item 4", "Item 5", "Item 6", "Item 7", "Item 8",
"Item 9", "Item 10", "Item 11", "Item 12", "Item 13",
"Item 14", "Item 15", })));
}

@Override
protected void onListItemClick(final ListView l, final View v,
final int position, final long id) {
final Bundle b = new Bundle();
b.putInt("index", position);
UndoBarStyle undoBarStyle = new UndoBarStyle(com.cocosw.undobar.R.drawable.ic_undobar_undo,
R.string.undo, 5000).setCountDownFormatter(new MyFormatCountDownCallback());
UndoBarController.show(this, getListAdapter().getItem(position) + " was selected", this, b,
false, undoBarStyle);
}

@Override
public void onUndo(final Parcelable token) {
if (token != null) {
final int position = ((Bundle) token).getInt("index");
Toast.makeText(this, "undo clicked, index " + position,
Toast.LENGTH_SHORT).show();
}
}

private class MyFormatCountDownCallback implements CountDownFormatter {

@Override
public String getCountDownString(final long millisUntilFinished) {
int seconds = (int) Math.ceil(millisUntilFinished / 1000.0);

if (seconds > 0) {
return getResources().getQuantityString(R.plurals.countdown_seconds, seconds,
seconds);
}
return getString(R.string.countdown_dismissing);
}
}
}
19 changes: 15 additions & 4 deletions library/res/layout/undobar.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/_undobar"
style="@style/UndoBar">
style="@style/UndoBar" >

<TextView
android:id="@+id/undobar_message"
style="@style/UndoBarMessage" />
<LinearLayout
style="@style/UndoBarMessageContainer"
android:orientation="vertical" >

<TextView
android:id="@+id/undobar_message"
style="@style/UndoBarMessage" />

<TextView
android:id="@+id/undobar_message_countdown"
style="@style/UndoBarMessageCountdown"
android:visibility="gone" />
</LinearLayout>

<View
android:id="@+id/undobar_divider"
Expand All @@ -18,4 +28,5 @@
<Button
android:id="@+id/undobar_button"
style="@style/UndoBarButton" />

</LinearLayout>
32 changes: 26 additions & 6 deletions library/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
-->
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">


<style name="UndoBar" parent="UndoBarClassic" />

<style name="UndoBarMessageContainer" parent="UndoBarMessageContainerClassic" />

<style name="UndoBarMessage" parent="UndoBarMessageClassic" />

<style name="UndoBarMessageCountdown" parent="UndoBarMessageCountdownClassic" />

<style name="UndoBarButton" parent="UndoBarButtonClassic" />

<style name="UndoBarClassic">
Expand All @@ -36,17 +39,29 @@
<item name="android:clickable">true</item>
</style>

<style name="UndoBarMessageClassic">
<style name="UndoBarMessageContainerClassic">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">16dp</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_marginRight">16dp</item>
<item name="android:layout_marginTop">8dp</item>
<item name="android:layout_marginBottom">8dp</item>
</style>

<style name="UndoBarMessageClassic">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:textAppearance">?android:textAppearanceMedium</item>
<item name="android:textColor">#fff</item>
</style>

<style name="UndoBarMessageCountdownClassic" parent="UndoBarMessageClassic">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
</style>

<style name="UndoBarButtonClassic">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">match_parent</item>
Expand All @@ -68,22 +83,28 @@
<item name="android:padding">0.0dip</item>
</style>


<style name="UndoBarButtonKitKat" parent="UndoBarButtonClassic">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:background">@drawable/toast_frame_holo_button</item>
<item name="android:paddingRight">25dp</item>
</style>

<style name="UndoBarMessageContainerKitKat" parent="UndoBarMessageContainerClassic">
<item name="android:layout_weight">1.0</item>
<item name="android:layout_gravity">start|center</item>
<item name="android:layout_marginLeft">25dp</item>
</style>

<style name="UndoBarMessageKitKat" parent="UndoBarMessageClassic">
<item name="android:shadowColor">#bb000000</item>
<item name="android:shadowRadius">2.75</item>
<item name="android:fontFamily" tools:ignore="NewApi">sans-serif-condensed</item>
<item name="android:freezesText">true</item>
<item name="android:layout_weight">1.0</item>
<item name="android:layout_gravity">start|center</item>
<item name="android:layout_marginLeft">25dp</item>
</style>

<style name="UndoBarMessageCountdownKitKat" parent="UndoBarMessageKitKat">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
</style>

<style name="UndoBarDivider">
Expand All @@ -94,5 +115,4 @@
<item name="android:background">@drawable/undobar_divider</item>
</style>


</resources>
53 changes: 47 additions & 6 deletions library/src/com/cocosw/undobar/UndoBarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,31 @@ public class UndoBarController extends LinearLayout {
private static Animation inAnimation = inFromBottomAnimation(null);
private static Animation outAnimation = outToBottomAnimation(null);
private final TextView mMessageView;
private final TextView mMessageCountdownView;
private final TextView mButton;
private final Handler mHideHandler = new Handler();
private final Runnable mHideRunnable = new Runnable() {

private long mDismissStartMillis;
private final CountDownRunnable mCountDownRunnable;

private class CountDownRunnable implements Runnable {

@Override
public void run() {
hideUndoBar(false);
long millisRemaining = style.duration
- (System.currentTimeMillis() - mDismissStartMillis);
if (style.countDownFormatter != null) {
mMessageCountdownView.setText(style.countDownFormatter
.getCountDownString(millisRemaining));
}

if (millisRemaining <= 0) {
hideUndoBar(false);
} else {
mHideHandler.postDelayed(this, Math.min(millisRemaining, 1000));
}
}
};
}
private UndoListener mUndoListener;
// State objects
private Parcelable mUndoToken;
Expand All @@ -84,7 +101,9 @@ public void run() {
private UndoBarController(final Context context, final AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.undobar, this, true);
mCountDownRunnable = new CountDownRunnable();
mMessageView = (TextView) findViewById(R.id.undobar_message);
mMessageCountdownView = (TextView) findViewById(R.id.undobar_message_countdown);
mButton = (TextView) findViewById(id.undobar_button);
mButton.setOnClickListener(
new View.OnClickListener() {
Expand Down Expand Up @@ -357,7 +376,7 @@ private void setUndoListener(final UndoListener mUndoListener) {
}

private void hideUndoBar(final boolean immediate) {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.removeCallbacks(mCountDownRunnable);
mUndoToken = null;
if (immediate) {
setVisibility(View.GONE);
Expand Down Expand Up @@ -418,9 +437,17 @@ private void showUndoBar(final boolean immediate,
if (style.bgRes > 0)
findViewById(id._undobar).setBackgroundResource(style.bgRes);

mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.removeCallbacks(mCountDownRunnable);

if (style.countDownFormatter != null) {
mMessageCountdownView.setVisibility(View.VISIBLE);
mMessageCountdownView.setText(style.countDownFormatter
.getCountDownString(style.duration));
}

mDismissStartMillis = System.currentTimeMillis();
if (style.duration > 0) {
mHideHandler.postDelayed(mHideRunnable, style.duration);
mHideHandler.postDelayed(mCountDownRunnable, Math.min(1000, style.duration));
}
if (!immediate) {
clearAnimation();
Expand Down Expand Up @@ -452,6 +479,20 @@ public interface UndoListener {
// ensureView(act);
// }

/**
* A callback interface which is used to provide the text to display when
* counting down.
*/
public interface CountDownFormatter {
/**
* Called each tick of the CountDownTimer
*
* @param millisLeft time in milliseconds remaining before the item is
* automatically removed
*/
public String getCountDownString(final long millisLeft);
}

/**
* UndoBar Builder
*/
Expand Down
Loading