reader/android: add swipe left to delete action

Change-Id: Ic303c0d2507fbdabb04e9864be9e0ed238d7648a
diff --git a/android/app/build.gradle b/android/app/build.gradle
index f3f079b..905069f 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -34,8 +34,8 @@
 }
 
 android {
-    compileSdkVersion 21
-    buildToolsVersion "21.1.2"
+    compileSdkVersion 22
+    buildToolsVersion "22.0.1"
 
     compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_7
@@ -44,8 +44,8 @@
 
     defaultConfig {
         applicationId "io.v.android.apps.reader"
-        minSdkVersion 21
-        targetSdkVersion 21
+        minSdkVersion 22
+        targetSdkVersion 22
         versionCode 1
         versionName "1.0"
         multiDexEnabled true
@@ -60,8 +60,8 @@
 
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
-    compile 'com.android.support:cardview-v7:21+'
-    compile 'com.android.support:recyclerview-v7:21+'
+    compile 'com.android.support:cardview-v7:22+'
+    compile 'com.android.support:recyclerview-v7:22+'
     compile 'com.joanzapata.pdfview:android-pdfview:1.0.4@aar'
     compile project(':android-lib')
 }
diff --git a/android/app/src/main/java/io/v/android/apps/reader/DeviceSetChooserActivity.java b/android/app/src/main/java/io/v/android/apps/reader/DeviceSetChooserActivity.java
index d52a796..b6953a1 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/DeviceSetChooserActivity.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/DeviceSetChooserActivity.java
@@ -9,6 +9,7 @@
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
+import android.support.v7.widget.helper.ItemTouchHelper;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
@@ -24,7 +25,7 @@
 
 /**
  * Activity that displays all the active device sets of this user.
- *
+ * <p/>
  * When the user clicks on one of the device sets, it starts the PdfViewerActivity with the file
  * associated with the device set.
  */
@@ -48,9 +49,10 @@
         mRecyclerView.setHasFixedSize(true);
 
         // Use the linear layout manager for the recycler view
-        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this);
+        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
         mRecyclerView.setLayoutManager(layoutManager);
 
+        // "Add Device Set" button initialization
         mButtonAddDeviceSet = (Button) findViewById(R.id.button_add_device_set);
         mButtonAddDeviceSet.setOnClickListener(new View.OnClickListener() {
             @Override
@@ -85,6 +87,27 @@
         });
 
         mRecyclerView.setAdapter(mAdapter);
+
+        // ItemTouchHelper for handling the swipe action.
+        ItemTouchHelper.SimpleCallback touchCallback;
+        touchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
+            @Override
+            public boolean onMove(RecyclerView recyclerView,
+                                  RecyclerView.ViewHolder viewHolder,
+                                  RecyclerView.ViewHolder target) {
+                return false;
+            }
+
+            @Override
+            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
+                // Delete the device set on left swipe.
+                if (direction == ItemTouchHelper.LEFT) {
+                    mDB.deleteDeviceSet(
+                            mAdapter.getDeviceSetId(viewHolder.getLayoutPosition()));
+                }
+            }
+        };
+        new ItemTouchHelper(touchCallback).attachToRecyclerView(mRecyclerView);
     }
 
     @Override
diff --git a/android/app/src/main/java/io/v/android/apps/reader/DeviceSetListAdapter.java b/android/app/src/main/java/io/v/android/apps/reader/DeviceSetListAdapter.java
index a43b65a..1900343 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/DeviceSetListAdapter.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/DeviceSetListAdapter.java
@@ -44,7 +44,8 @@
                 @Override
                 public void onClick(View v) {
                     if (mClickListener != null) {
-                        mClickListener.onDeviceSetClick(DeviceSetListAdapter.this, v, getPosition());
+                        mClickListener.onDeviceSetClick(
+                                DeviceSetListAdapter.this, v, getLayoutPosition());
                     }
                 }
             });
@@ -77,6 +78,10 @@
         holder.mTextViewId.setText("Id: " + ds.getId());
     }
 
+    public String getDeviceSetId(int position) {
+        return mDeviceSets.getItem(position).getId();
+    }
+
     public String getItemTitle(int position) {
         return getItemTitle(mDeviceSets.getItem(position));
     }
diff --git a/android/app/src/main/java/io/v/android/apps/reader/db/DB.java b/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
index abfd8e1..c8e0bc3 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/db/DB.java
@@ -91,26 +91,37 @@
 
     /**
      * Gets the list of available PDF files.
+     *
      * @return a list of PDF files.
      */
     DBList<File> getFileList();
 
     /**
      * Gets the list of devices of this user.
+     *
      * @return a list of devices.
      */
     DBList<Device> getDeviceList();
 
     /**
      * Gets the list of currently active device sets.
+     *
      * @return a list of device sets.
      */
     DBList<DeviceSet> getDeviceSetList();
 
     /**
      * Adds a new device set to the db.
+     *
      * @param ds the device set to be added.
      */
     void addDeviceSet(DeviceSet ds);
 
+    /**
+     * Deletes a device set with the given id.
+     *
+     * @param id the id of the device set.
+     */
+    void deleteDeviceSet(String id);
+
 }
diff --git a/android/app/src/main/java/io/v/android/apps/reader/db/FakeDB.java b/android/app/src/main/java/io/v/android/apps/reader/db/FakeDB.java
index 008ffed..5af039d 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/db/FakeDB.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/db/FakeDB.java
@@ -181,6 +181,15 @@
                 mListener.notifyItemInserted(mDeviceSets.size() - 1);
             }
         }
+
+        public void removeItemById(String id) {
+            for (int i = 0; i < mDeviceSets.size(); ++i) {
+                if (mDeviceSets.get(i).getId().equals(id)) {
+                    mDeviceSets.remove(i);
+                    return;
+                }
+            }
+        }
     }
 
     public void init(Activity activity) {
@@ -212,4 +221,9 @@
     public void addDeviceSet(DeviceSet ds) {
         mDeviceSetList.addItem(ds);
     }
+
+    @Override
+    public void deleteDeviceSet(String id) {
+        mDeviceSetList.removeItemById(id);
+    }
 }
diff --git a/android/app/src/main/java/io/v/android/apps/reader/db/SyncbaseDB.java b/android/app/src/main/java/io/v/android/apps/reader/db/SyncbaseDB.java
index 9ece043..48b7712 100644
--- a/android/app/src/main/java/io/v/android/apps/reader/db/SyncbaseDB.java
+++ b/android/app/src/main/java/io/v/android/apps/reader/db/SyncbaseDB.java
@@ -445,6 +445,15 @@
         }
     }
 
+    @Override
+    public void deleteDeviceSet(String id) {
+        try {
+            mLocalSB.deviceSets.delete(mVContext, id);
+        } catch (VException e) {
+            handleError("Failed to delete the device set with id " + id + ": " + e.getMessage());
+        }
+    }
+
     private void handleError(String msg) {
         Log.e(TAG, msg);
         Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();