From: Damyan Ivanov Date: Wed, 9 Jan 2019 05:25:02 +0000 (+0000) Subject: dynamic list of profiles, add profile removal X-Git-Tag: v0.3~96 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=94aa0aa57db3dce40f31fc0321c718998d9cb48b;p=mobile-ledger.git dynamic list of profiles, add profile removal --- diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index f32c3bc6..13998c72 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -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 lastUpdateDate = new ObservableValue<>(); public static ObservableValue profile = new ObservableValue<>(); + public static ObservableList profiles = + new ObservableList<>(new ArrayList<>()); } diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 72097a3f..038515bf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -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}); + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 7386de0c..931f7835 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -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 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"); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java index 3cd61712..857e7c39 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileListActivity.java @@ -111,15 +111,13 @@ public class ProfileListActivity extends AppCompatActivity { } private void setupRecyclerView(@NonNull RecyclerView recyclerView) { - List list = MobileLedgerProfile.loadAllFromDB(); - recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, list, mTwoPane)); + recyclerView.setAdapter(new ProfilesRecyclerViewAdapter(this, mTwoPane)); } public static class ProfilesRecyclerViewAdapter extends RecyclerView.Adapter { private final ProfileListActivity mParentActivity; - private final List 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 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; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java index 09099732..591d147a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailActivity.java @@ -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) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index ff1b5e5b..f2111393 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -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 index 00000000..02e7af04 --- /dev/null +++ b/app/src/main/res/drawable/ic_delete_white_24dp.xml @@ -0,0 +1,21 @@ + + + + + diff --git a/app/src/main/res/menu/profile_details.xml b/app/src/main/res/menu/profile_details.xml new file mode 100644 index 00000000..9d62e06a --- /dev/null +++ b/app/src/main/res/menu/profile_details.xml @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index c81a8f08..1a7c9482 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -64,4 +64,5 @@ Трансакции Грешка в HTTP Нов профил + Изтриване на профила \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8c1682ab..e4e23b43 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -106,4 +106,5 @@ Profile Details Profiles New profile + Delete profile