Thursday, October 17, 2013

Android AutoSave For Saving Processes After Custom Delay

Hi there! Today i'm gonna share a very common task while developing android apps. An autosave function which allow me to monitor user actions and save something after a custom delay everytime the user interacts with the UI.

Defining the common Interface

This interface will be implemented by the interested clients. For example your Activity.
/**
 * Clients who are interested in having auto save capabilities functions implements this interface.

 * 

 * Steps on how to use it:

 * - Implement this interface

 * - In your UI, in the desired onClick, onTouch or whatever method, create a new instance of {@link AutoSave}

 * - Pass this implemented instance to the constructor of {@link AutoSave}

 * - With the method setSecondsToWait() of {@link AutoSave}, set the seconds you may want to delay the save process.

 * - Obs: If you do not use the call setSecondsToWait() on {@link AutoSave}, the default behavior will be 10 seconds delay.

 * - Important: Call the start() method on {@link AutoSave} to start monitoring user actions.

 * 
 * @author Ricardo Ferreira 17/10/2013
 */
public interface SaveAction {

 public void autosave();

}

Defining the AutoSave Thread

This is the class which monitors the user's interaction.
/**
 * Use this class to start a auto save process as a Thread with some delay before saving.

 * 

 * Steps on how to use it:

 * - Instantiate this instance in your desired onClick, onTouch or whatever method

 * - Call the method setSecondsToWait() and set the seconds you want to delay

 * - (If you do not call setSecondsToWait() on {@link AutoSave}, the default behavior will be 10 seconds delay.)

 * - Important: Call the start() method on {@link AutoSave} to start monitoring user actions.

 * 
 * @author Ricardo Ferreira  17/10/2013
 * 
 */
public class AutoSave extends Thread {

 private Activity context;
 private long startTime = 0;
 private long currentTime = 0;
 private final long teenSeconds = 10000;
 private final long oneSecond = 1000;
 private boolean recording = false;
 private SaveAction saveAction;
 private long secondsToWait = 0;

 public AutoSave(Activity context, SaveAction saveAction) {
  this.context = context;
  this.saveAction = saveAction;
 }

 @Override
 public void run() {

  while (!this.context.isDestroyed()) {
   try {
    if (isRecording()) {
     setCurrentTime(System.currentTimeMillis());
     if (getCurrentTime() - getStartTime() > (secondsToWait == 0 ? teenSeconds : secondsToWait)) {
      setRecording(false);
      setStartTime(0);
      setCurrentTime(0);
      autoSave();
     }
     sleep(oneSecond);
    } else {
     sleep(oneSecond);
    }
   } catch (InterruptedException e) {
    Log.i("TEST", e.getMessage());
    e.printStackTrace();
   }
  }

 }

 public void setSecondsToWait(long secondsToWait) {
  this.secondsToWait = secondsToWait;
 }

 public void saveAfterSecondsToWait() {
  if (!isRecording()) {
   setRecording(true);
   setStartTime(System.currentTimeMillis());
  }
 }

 private void autoSave() {
  this.saveAction.autosave();
 }

 private long getStartTime() {
  return startTime;
 }

 private void setStartTime(long startTime) {
  this.startTime = startTime;
 }

 private void setCurrentTime(long currentTime) {
  this.currentTime = currentTime;
 }

 private long getCurrentTime() {
  return currentTime;
 }

 private boolean isRecording() {
  return recording;
 }

 private void setRecording(boolean recording) {
  this.recording = recording;
 }

}

Initializing the AutoSave instance

Lets assume now, that you have an Activity which implements the interface SaveAction. Lets call it: "PaintActivity" and you are interested in monitoring user actions. Every time the user draws something on the screen, you'll wait teen seconds and save the drawing. To do it so, call this two lines bellow in your onTouchListener's constructor. Then when the action happens, call saveAfterSecondsToWait() from AutoSave. Done! If you want to customize the delay time, call setSecondsToWait() from AutoSave before calling start(). Done! ;) You can now implement your auto save behavior in your Activity.

this.autosave = new AutoSave(this.context, (PainterActivity) this.context);
//this.autosave.setSecondsToWait(3000);// three seconds delay
this.autosave.start();
// call saveAfterSecondsToWait() from autosave when the action actually happens. for example: onTouch() or onClick() whatever...
this.autosave.saveAfterSecondsToWait(),


That's all! Hope you like it! :)

😱👇 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘‡

Be sure to read, it will change your life!
Show your work by Austin Kleonhttps://amzn.to/34NVmwx

This book is a must read - it will put you in another level! (Expert)
Agile Software Development, Principles, Patterns, and Practiceshttps://amzn.to/30WQSm2

Write cleaner code and stand out!
Clean Code - A Handbook of Agile Software Craftsmanship: https://amzn.to/33RvaSv

This book is very practical, straightforward and to the point! Worth every penny!
Kotlin for Android App Development (Developer's Library): https://amzn.to/33VZ6gp

Needless to say, these are top right?
Apple AirPods Pro: https://amzn.to/2GOICxy

😱👆 PROMOTIONAL DISCOUNT: BOOKS AND IPODS PRO ðŸ˜±ðŸ‘†