dynamic list of profiles, add profile removal
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 05:25:02 +0000 (05:25 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 05:25:02 +0000 (05:25 +0000)
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java
app/src/main/res/drawable/ic_delete_white_24dp.xml [new file with mode: 0644]
app/src/main/res/menu/profile_details.xml [new file with mode: 0644]
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index f32c3bc67049b8f66b578668d923415d2ce9ce37..13998c7205210368854e41aaa5ca6f32abf0e598 100644 (file)
@@ -18,6 +18,7 @@
 package net.ktnx.mobileledger.model;
 
 import net.ktnx.mobileledger.utils.ObservableAtomicInteger;
+import net.ktnx.mobileledger.utils.ObservableList;
 import net.ktnx.mobileledger.utils.ObservableValue;
 
 import java.util.ArrayList;
@@ -32,4 +33,6 @@ public final class Data {
     public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
     public static ObservableValue<Date> lastUpdateDate = new ObservableValue<>();
     public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
+    public static ObservableList<MobileLedgerProfile> profiles =
+            new ObservableList<>(new ArrayList<>());
 }
index 72097a3faa6112e89475fb95b1601a394be1ab68..038515bfb02e4ec32e01293476c179c878276f22 100644 (file)
@@ -254,4 +254,9 @@ public final class MobileLedgerProfile {
     public void set_option_value(String name, long value) {
         set_option_value(name, String.valueOf(value));
     }
+    public void removeFromDB() {
+        SQLiteDatabase db = MLDB.getWritableDatabase();
+        Log.d("db", String.format("removinf progile %s from DB", uuid));
+        db.execSQL("delete from profiles where uuid=?", new Object[]{uuid});
+    }
 }
index 7386de0c5aca0482cd233fdaa72e0bf71a5bde2e..931f783582bd6b95ba1ab62965ab0640033b5dff 100644 (file)
@@ -54,7 +54,6 @@ import java.lang.ref.WeakReference;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.Date;
-import java.util.List;
 import java.util.Observable;
 import java.util.Observer;
 
@@ -184,14 +183,14 @@ public class MainActivity extends AppCompatActivity {
         });
     }
     private void setupProfile() {
-        List<MobileLedgerProfile> profiles = MobileLedgerProfile.loadAllFromDB();
+        Data.profiles.setList(MobileLedgerProfile.loadAllFromDB());
         MobileLedgerProfile profile = null;
 
         String profileUUID = MLDB.get_option_value(MLDB.OPT_PROFILE_UUID, null);
         if (profileUUID == null) {
-            if (profiles.isEmpty()) {
-                profiles = MobileLedgerProfile.createInitialProfileList();
-                profile = profiles.get(0);
+            if (Data.profiles.isEmpty()) {
+                Data.profiles.setList(MobileLedgerProfile.createInitialProfileList());
+                profile = Data.profiles.get(0);
 
                 SharedPreferences backend = getSharedPreferences("backend", MODE_PRIVATE);
                 Log.d("profiles", "Migrating from preferences to profiles");
@@ -215,7 +214,7 @@ public class MainActivity extends AppCompatActivity {
             profile = MobileLedgerProfile.loadUUIDFromDB(profileUUID);
         }
 
-        if (profile == null) profile = profiles.get(0);
+        if (profile == null) profile = Data.profiles.get(0);
 
         if (profile == null) throw new AssertionError("profile must have a value");
 
index 3cd61712ff96dcfc6f31a487209747bd62b0ab6f..857e7c394a65830d60beb0f4a0480ef7855e877d 100644 (file)
@@ -111,15 +111,13 @@ public class ProfileListActivity extends AppCompatActivity {
     }
 
     private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
-        List<MobileLedgerProfile> list = MobileLedgerProfile.loadAllFromDB();
-        recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, list, mTwoPane));
+        recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, mTwoPane));
     }
 
     public static class ProfilesRecyclerViewAdapter
             extends RecyclerView.Adapter<ProfilesRecyclerViewAdapter.ProfileListViewHolder> {
 
         private final ProfileListActivity mParentActivity;
-        private final List<MobileLedgerProfile> mValues;
         private final boolean mTwoPane;
         private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
             @Override
@@ -128,11 +126,13 @@ public class ProfileListActivity extends AppCompatActivity {
                 editProfile(view, item);
             }
         };
-        ProfilesRecyclerViewAdapter(ProfileListActivity parent, List<MobileLedgerProfile> items,
-                                    boolean twoPane) {
-            mValues = items;
+        ProfilesRecyclerViewAdapter(ProfileListActivity parent, boolean twoPane) {
             mParentActivity = parent;
             mTwoPane = twoPane;
+            Data.profiles.addObserver((o, arg) ->{
+                Log.d("profiles", "profile list changed");
+                notifyDataSetChanged();
+            });
         }
         private void editProfile(View view, MobileLedgerProfile item) {
             if (mTwoPane) {
@@ -171,7 +171,7 @@ public class ProfileListActivity extends AppCompatActivity {
         }
         @Override
         public void onBindViewHolder(@NonNull final ProfileListViewHolder holder, int position) {
-            final MobileLedgerProfile profile = mValues.get(position);
+            final MobileLedgerProfile profile = Data.profiles.get(position);
             final MobileLedgerProfile currentProfile = Data.profile.get();
             Log.d("profiles", String.format("pos %d: %s, current: %s", position, profile.getUuid(),
                     currentProfile.getUuid()));
@@ -193,7 +193,7 @@ public class ProfileListActivity extends AppCompatActivity {
         }
         @Override
         public int getItemCount() {
-            return mValues.size();
+            return Data.profiles.size();
         }
         class ProfileListViewHolder extends RecyclerView.ViewHolder {
             final RadioButton mRadioView;
index 09099732f89f7325ef3dd61f0d87f9e637224a32..591d147a777b037a919a9f6d2a9d359c2a04d18e 100644 (file)
@@ -21,13 +21,17 @@ import android.content.Intent;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
-import android.support.v7.widget.Toolbar;
-import android.view.View;
-import android.support.v7.app.AppCompatActivity;
 import android.support.v7.app.ActionBar;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.view.Menu;
 import android.view.MenuItem;
+import android.view.View;
 
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
 
 /**
@@ -37,7 +41,7 @@ import net.ktnx.mobileledger.ui.activity.ProfileListActivity;
  * in a {@link ProfileListActivity}.
  */
 public class ProfileDetailActivity extends AppCompatActivity {
-
+    private MobileLedgerProfile profile;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -70,17 +74,50 @@ public class ProfileDetailActivity extends AppCompatActivity {
         // http://developer.android.com/guide/components/fragments.html
         //
         if (savedInstanceState == null) {
+            final String profileUUID =
+                    getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID);
+
+            if (profileUUID != null) {
+                int i = 0;
+                for (MobileLedgerProfile p : Data.profiles.getList()) {
+                    if (p.getUuid().equals(profileUUID)) {
+                        Log.d("profiles", String.format("found profile %s at %d", profileUUID, i));
+                        profile = p;
+                        break;
+                    }
+                    i++;
+                }
+                if (profile == null) throw new AssertionError(
+                        String.format("Can't get profile " + "(uuid:%s) from the " + "global list",
+                                profileUUID));
+            }
+
             // Create the detail fragment and add it to the activity
             // using a fragment transaction.
             Bundle arguments = new Bundle();
-            arguments.putString(ProfileDetailFragment.ARG_ITEM_ID,
-                    getIntent().getStringExtra(ProfileDetailFragment.ARG_ITEM_ID));
+            arguments.putString(ProfileDetailFragment.ARG_ITEM_ID, profileUUID);
             ProfileDetailFragment fragment = new ProfileDetailFragment();
             fragment.setArguments(arguments);
             getSupportFragmentManager().beginTransaction()
                     .add(R.id.profile_detail_container, fragment).commit();
         }
     }
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        super.onCreateOptionsMenu(menu);
+        Log.d("profiles", "[activity] Creating profile details options menu");
+        getMenuInflater().inflate(R.menu.profile_details, menu);
+        menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(item -> {
+            Log.d("profiles", String.format("deleting profile %s", profile.getUuid()));
+            profile.removeFromDB();
+            Data.profiles.remove(profile);
+            Data.profile.set(Data.profiles.get(0));
+            finish();
+            return true;
+        });
+
+        return true;
+    }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
index ff1b5e5b7c21aebe6405e4a6130480bfad5ad18e..f211139375b9cd21847e3d2c0e7e111ecd7af18b 100644 (file)
@@ -26,6 +26,9 @@ import android.support.design.widget.FloatingActionButton;
 import android.support.v4.app.Fragment;
 import android.util.Log;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
@@ -68,7 +71,24 @@ public class ProfileDetailFragment extends Fragment {
      */
     public ProfileDetailFragment() {
     }
-
+    @Override
+    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        Log.d("profiles", "[fragment] Creating profile details options menu");
+        super.onCreateOptionsMenu(menu, inflater);
+        inflater.inflate(R.menu.profile_details, menu);
+        menu.findItem(R.id.menuDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+            @Override
+            public boolean onMenuItemClick(MenuItem item) {
+                Log.d("profiles", String.format("[fragment] removing profile %s", mItem.getUuid()));
+                mItem.removeFromDB();
+                Data.profiles.remove(mItem);
+                if (Data.profile.get().getUuid().equals(mItem.getUuid())) {
+                    Data.profile.set(Data.profiles.get(0));
+                }
+                return false;
+            }
+        });
+    }
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -113,6 +133,7 @@ public class ProfileDetailFragment extends Fragment {
                 mItem = new MobileLedgerProfile(profileName.getText(), url.getText(),
                         useAuthentication.isChecked(), userName.getText(), password.getText());
                 mItem.storeInDB();
+                Data.profiles.add(mItem);
             }
 
             Activity activity = getActivity();
diff --git a/app/src/main/res/drawable/ic_delete_white_24dp.xml b/app/src/main/res/drawable/ic_delete_white_24dp.xml
new file mode 100644 (file)
index 0000000..02e7af0
--- /dev/null
@@ -0,0 +1,21 @@
+<!--
+  ~ Copyright Google Inc.
+  ~
+  ~ 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:
+  ~
+  ~   https://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distribution 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 permissionsand
+  ~ limitations under the License.
+  -->
+
+<vector android:height="24dp" android:tint="#EEEEEE"
+    android:viewportHeight="24.0" android:viewportWidth="24.0"
+    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="#FF000000" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
+</vector>
diff --git a/app/src/main/res/menu/profile_details.xml b/app/src/main/res/menu/profile_details.xml
new file mode 100644 (file)
index 0000000..9d62e06
--- /dev/null
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright © 2019 Damyan Ivanov.
+  ~ This file is part of Mobile-Ledger.
+  ~ Mobile-Ledger is free software: you can distribute it and/or modify it
+  ~ under the term of the GNU General Public License as published by
+  ~ the Free Software Foundation, either version 3 of the License, or
+  ~ (at your opinion), any later version.
+  ~
+  ~ Mobile-Ledger is distributed in the hope that it will be useful,
+  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
+  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+  ~ GNU General Public License terms for details.
+  ~
+  ~ You should have received a copy of the GNU General Public License
+  ~ along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
+  -->
+
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <item
+        android:id="@+id/menuDelete"
+        android:icon="@drawable/ic_delete_white_24dp"
+        android:title="@string/delete" />
+</menu>
\ No newline at end of file
index c81a8f0875deac96bd08d97d9806c1122cc6d403..1a7c94824bf30ba57e51e494d9d23c430bda54e1 100644 (file)
@@ -64,4 +64,5 @@
     <string name="title_activity_transaction_list">Трансакции</string>
     <string name="err_http_error">Грешка в HTTP</string>
     <string name="new_profile_title">Нов профил</string>
+    <string name="delete">Изтриване на профила</string>
 </resources>
\ No newline at end of file
index 8c1682ab75e952f9efd109701680be8ab5180e65..e4e23b43b75799c2a37057fe5db886d85ab8c12a 100644 (file)
     <string name="title_profile_details">Profile Details</string>
     <string name="profiles">Profiles</string>
     <string name="new_profile_title" type="id">New profile</string>
+    <string name="delete">Delete profile</string>
 </resources>