Android Open Source - RealtimeStorage-Android Storage Handler






From Project

Back to project page RealtimeStorage-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeStorage-Android 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 handlers;
//from www  .  j  a  v a  2 s.c om
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import co.realtime.sample.MainActivity;

import java.util.LinkedHashMap;

import co.realtime.storage.ItemAttribute;
import co.realtime.storage.ItemSnapshot;
import co.realtime.storage.StorageRef;
import co.realtime.storage.TableRef;
import co.realtime.storage.ext.OnError;
import co.realtime.storage.ext.OnItemSnapshot;
import co.realtime.storage.ext.OnReconnected;
import co.realtime.storage.ext.OnReconnecting;
import co.realtime.storage.ext.StorageException;
import config.Config;

public class StorageHandler {
    private static String TAG = "StorageHandler";
    private static StorageHandler instance = null;
    private StorageRef storage;
    private Context context;
    private String listName;

    private StorageHandler(final Context context) {
        try {
            this.context = context;
            storage = new StorageRef(Config.APPKEY, Config.TOKEN, true, false,Config.CLUSTERURL);
            storage.onReconnected(new OnReconnected(){
                @Override
                public void run(StorageRef sender) {
                    ((Activity)context).runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            ((MainActivity)context).clearItems();
                            getItems();
                        }});
                }
            });

            storage.onReconnecting(new OnReconnecting() {
                @Override
                public void run(StorageRef storageRef) {
                    Log.i(TAG, "Reconnecting to storage");
                }
            });
        } catch (StorageException e) {
            e.printStackTrace();
        }
    }

    public static StorageHandler getInstance(Context context) {
        if (instance == null) {
           instance = new StorageHandler(context);
        }
        return instance;
    }

    private OnError onError = new OnError(){
        @Override
        public void run(Integer code, String errorMessage) {
            Log.e(TAG,String.format("%d (%s)", code, errorMessage));
        }
    };

    private OnItemSnapshot onPutUpdate = new OnItemSnapshot(){
        @Override
        public void run(final ItemSnapshot itemSnapshot) {
            if(itemSnapshot != null) {
                ((MainActivity)context).updateItems(itemSnapshot);
            }
        }
    };

    private OnItemSnapshot onDelete = new OnItemSnapshot(){
        @Override
        public void run(final ItemSnapshot itemSnapshot) {
            if(itemSnapshot != null) {
                ((MainActivity) context).deleteItems(itemSnapshot);
            }
        }
    };

    public void getItems(){
        TableRef tr = storage.table(Config.TABLE_NAME).equals(Config.PRIMARY_KEY, new ItemAttribute(listName));
        tr.getItems(new OnItemSnapshot(){
            @Override
            public void run(ItemSnapshot item) {
                ((MainActivity)context).getItems(item);
            }}, onError);
    }

    public void storageOn(){
        storage.table(Config.TABLE_NAME).on(StorageRef.StorageEvent.PUT, new ItemAttribute(listName), onPutUpdate);
        storage.table(Config.TABLE_NAME).on(StorageRef.StorageEvent.UPDATE, new ItemAttribute(listName), onPutUpdate);
        storage.table(Config.TABLE_NAME).on(StorageRef.StorageEvent.DELETE, new ItemAttribute(listName), onDelete);
    }

    public void storageOff(){
        storage.table(Config.TABLE_NAME).off(StorageRef.StorageEvent.PUT, new ItemAttribute(listName), onPutUpdate);
        storage.table(Config.TABLE_NAME).off(StorageRef.StorageEvent.UPDATE, new ItemAttribute(listName), onPutUpdate);
        storage.table(Config.TABLE_NAME).off(StorageRef.StorageEvent.DELETE, new ItemAttribute(listName), onDelete);
    }

    public void storagePush(LinkedHashMap<String, ItemAttribute> itemToPut){
        storage.table(Config.TABLE_NAME).push(itemToPut, null, onError);
    }

    public void storageDel(LinkedHashMap<String, ItemAttribute> item){
        storage.table(Config.TABLE_NAME).item(item.get(Config.PRIMARY_KEY), item.get(Config.SECONDARY_KEY)).del(null, onError);
    }

    public void storageSet(LinkedHashMap<String, ItemAttribute> item){
        storage.table(Config.TABLE_NAME).item(item.get(Config.PRIMARY_KEY), item.get(Config.SECONDARY_KEY)).set(item, null, onError);
    }

    public String getListName() {
        return listName;
    }

    public void setListName(String listName) {
        this.listName = listName;
    }

}




Java Source Code List

adapters.TodoCustomAdapter.java
co.realtime.sample.ApplicationTest.java
co.realtime.sample.MainActivity.java
co.realtime.storage.ApplicationTest.java
co.realtime.storage.EventCollection.java
co.realtime.storage.Event.java
co.realtime.storage.Filter.java
co.realtime.storage.ItemAttribute.java
co.realtime.storage.ItemRef.java
co.realtime.storage.ItemSnapshot.java
co.realtime.storage.LHMItemsComparator.java
co.realtime.storage.OnRestCompleted.java
co.realtime.storage.PostBodyBuilder.java
co.realtime.storage.ProcessRestResponse.java
co.realtime.storage.RestWebservice.java
co.realtime.storage.Rest.java
co.realtime.storage.StorageContext.java
co.realtime.storage.StorageRef.java
co.realtime.storage.TableRef.java
co.realtime.storage.TableSnapshot.java
co.realtime.storage.entities.Heartbeat.java
co.realtime.storage.entities.IORMapping.java
co.realtime.storage.entities.KeySchema.java
co.realtime.storage.entities.Key.java
co.realtime.storage.entities.TableMetadata.java
co.realtime.storage.entities.Throughput.java
co.realtime.storage.ext.OnBooleanResponse.java
co.realtime.storage.ext.OnError.java
co.realtime.storage.ext.OnHeartbeat.java
co.realtime.storage.ext.OnItemSnapshot.java
co.realtime.storage.ext.OnPresence.java
co.realtime.storage.ext.OnReconnected.java
co.realtime.storage.ext.OnReconnecting.java
co.realtime.storage.ext.OnTableCreation.java
co.realtime.storage.ext.OnTableMetadata.java
co.realtime.storage.ext.OnTableSnapshot.java
co.realtime.storage.ext.OnTableUpdate.java
co.realtime.storage.ext.StorageException.java
config.Config.java
handlers.StorageHandler.java
helpers.ListNameHelper.java
listeners.ClickListener.java
listeners.EditorListener.java
ui.MyViewPager.java