Android Open Source - Mamytas Undo Bar Controller






From Project

Back to project page Mamytas.

License

The source code is released under:

GNU General Public License

If you think the Android project Mamytas listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mn.aug.restfulandroid.activity.base;
//from  w ww  .  j a  v  a 2  s .c om
/**
 * Created by Paul on 15/11/2014.
 */
/*
 * Copyright 2012 Roman Nurik
 *
 * 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.
 */

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import mn.aug.restfulandroid.R;
import mn.aug.restfulandroid.util.Logger;


public class UndoBarController {
    private final View actionBar;
    private View mBarView;
    private TextView mMessageView;
    private Handler mHideHandler = new Handler();

    private UndoListener mUndoListener;

    // State objects
    private Parcelable mUndoToken=null;
    private CharSequence mUndoMessage="";
    ValueAnimator animator;

    public interface UndoListener {
        void onUndo(Parcelable token);
        void undoDisabled(Parcelable token);
    }

    public UndoBarController(View actionBar, UndoListener undoListener) {
        this.actionBar = actionBar;
        mBarView = (RelativeLayout) actionBar.findViewById(R.id.undobar);

        mUndoListener = undoListener;

        mMessageView = (TextView) actionBar.findViewById(R.id.undobar_message);
        actionBar.findViewById(R.id.undobar_button)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mUndoListener.onUndo(mUndoToken);
                        hideUndoBar(false);
                    }
                });
        hideUndoBar(true);
    }

    public void showUndoBar(boolean immediate, CharSequence message, Parcelable undoToken) {
        mUndoToken = undoToken;
        mUndoMessage = message;
        mMessageView.setText(mUndoMessage);

        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable,
                actionBar.getResources().getInteger(R.integer.undobar_hide_delay));

        //mBarView.setVisibility(View.VISIBLE);

        if (immediate) {
            actionBar.setTranslationY(0);
        } else {
            animator = ValueAnimator.ofInt(mBarView.getMeasuredHeight(), 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    actionBar.setTranslationY((Integer) valueAnimator.getAnimatedValue());
                }
            });
            animator.setDuration(150);
            animator.start();
        }
    }

    public void hideUndoBar(boolean immediate) {
        Logger.debug("mBarView.getMeasuredHeight","height:"+mBarView.getMeasuredHeight());
        Logger.debug("mBarView.getMeasuredHeight","height:"+mBarView.getHeight());
        mHideHandler.removeCallbacks(mHideRunnable);
        if (immediate) {
            //mBarView.setVisibility(View.GONE);
            actionBar.setTranslationY(150);
            mUndoMessage = null;

        } else {
            animator = ValueAnimator.ofInt(0, mBarView.getMeasuredHeight());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    actionBar.setTranslationY((Integer) valueAnimator.getAnimatedValue());
                }
            });
            animator.setDuration(150);
            animator.start();
            mUndoMessage = null;

        }

    }

    public void onSaveInstanceState(Bundle outState) {
        outState.putCharSequence("undo_message", mUndoMessage);
        outState.putParcelable("undo_token", mUndoToken);
    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mUndoMessage = savedInstanceState.getCharSequence("undo_message");
            mUndoToken = savedInstanceState.getParcelable("undo_token");

            if (mUndoToken != null || !TextUtils.isEmpty(mUndoMessage)) {
                showUndoBar(true, mUndoMessage, mUndoToken);
            }
        }
    }

    private Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hideUndoBar(false);
            mUndoListener.undoDisabled(mUndoToken);
        }
    };

    public void clearUndoToken(){
        mUndoToken=null;
    }
}




Java Source Code List

mn.aug.restfulandroid.activity.AboutActivity.java
mn.aug.restfulandroid.activity.LoginActivity.java
mn.aug.restfulandroid.activity.ProjectEditor.java
mn.aug.restfulandroid.activity.ProjectsActivity.java
mn.aug.restfulandroid.activity.ProjectsArrayAdapter.java
mn.aug.restfulandroid.activity.TaskActivity.java
mn.aug.restfulandroid.activity.TaskEditor.java
mn.aug.restfulandroid.activity.TasksActivity.java
mn.aug.restfulandroid.activity.TasksArrayAdapter.java
mn.aug.restfulandroid.activity.TimerServiceHelper.java
mn.aug.restfulandroid.activity.TimerService.java
mn.aug.restfulandroid.activity.TimersArrayAdapter.java
mn.aug.restfulandroid.activity.base.RESTfulActivity.java
mn.aug.restfulandroid.activity.base.RESTfulListActivity.java
mn.aug.restfulandroid.activity.base.UndoBarController.java
mn.aug.restfulandroid.provider.CommentsDBAccess.java
mn.aug.restfulandroid.provider.ListsDBAccess.java
mn.aug.restfulandroid.provider.OwnershipDBAccess.java
mn.aug.restfulandroid.provider.ProviderDbHelper.java
mn.aug.restfulandroid.provider.RemindersDBAccess.java
mn.aug.restfulandroid.provider.TasksDBAccess.java
mn.aug.restfulandroid.provider.UsersDBAccess.java
mn.aug.restfulandroid.rest.AbstractRestMethod.java
mn.aug.restfulandroid.rest.DeleteListRestMethod.java
mn.aug.restfulandroid.rest.DeleteTaskRestMethod.java
mn.aug.restfulandroid.rest.GetListsRestMethod.java
mn.aug.restfulandroid.rest.GetTasksRestMethod.java
mn.aug.restfulandroid.rest.GetTimersRestMethod.java
mn.aug.restfulandroid.rest.LoginRestMethod.java
mn.aug.restfulandroid.rest.PostListRestMethod.java
mn.aug.restfulandroid.rest.PostTaskRestMethod.java
mn.aug.restfulandroid.rest.PostTimerRestMethod.java
mn.aug.restfulandroid.rest.PutListRestMethod.java
mn.aug.restfulandroid.rest.PutTaskRestMethod.java
mn.aug.restfulandroid.rest.PutTimerRestMethod.java
mn.aug.restfulandroid.rest.Request.java
mn.aug.restfulandroid.rest.Response.java
mn.aug.restfulandroid.rest.RestClient.java
mn.aug.restfulandroid.rest.RestMethodFactory.java
mn.aug.restfulandroid.rest.RestMethodResult.java
mn.aug.restfulandroid.rest.RestMethod.java
mn.aug.restfulandroid.rest.ShareListRestMethod.java
mn.aug.restfulandroid.rest.resource.Comment.java
mn.aug.restfulandroid.rest.resource.Lists.java
mn.aug.restfulandroid.rest.resource.Listw.java
mn.aug.restfulandroid.rest.resource.Login.java
mn.aug.restfulandroid.rest.resource.Reminder.java
mn.aug.restfulandroid.rest.resource.Resource.java
mn.aug.restfulandroid.rest.resource.TaskList.java
mn.aug.restfulandroid.rest.resource.Task.java
mn.aug.restfulandroid.rest.resource.Tasks.java
mn.aug.restfulandroid.rest.resource.Timer.java
mn.aug.restfulandroid.rest.resource.Timers.java
mn.aug.restfulandroid.security.AuthorizationManager.java
mn.aug.restfulandroid.security.RequestSigner.java
mn.aug.restfulandroid.service.ListProcessor.java
mn.aug.restfulandroid.service.ListsProcessor.java
mn.aug.restfulandroid.service.LoginProcessor.java
mn.aug.restfulandroid.service.ProcessorCallback.java
mn.aug.restfulandroid.service.ShareProcessor.java
mn.aug.restfulandroid.service.TaskProcessor.java
mn.aug.restfulandroid.service.TasksProcessor.java
mn.aug.restfulandroid.service.TimersProcessor.java
mn.aug.restfulandroid.service.WunderlistServiceHelper.java
mn.aug.restfulandroid.service.WunderlistService.java
mn.aug.restfulandroid.util.DateHelper.java
mn.aug.restfulandroid.util.DatePickerFragment.java
mn.aug.restfulandroid.util.Logger.java
mn.aug.restfulandroid.util.TimePickerFragment.java