From 4322700964460691ed8e4ce60dae2b056ccf3e4b Mon Sep 17 00:00:00 2001 From: Yaroslav Berezanskyi Date: Wed, 9 Sep 2015 11:23:14 +0300 Subject: [PATCH] Remove profile functionality --- app/src/main/java/io/syng/entity/Profile.java | 10 ++++- .../profile/ProfileDialogFragment.java | 39 ++++++++++++++----- .../java/io/syng/util/ProfileManager.java | 11 ++++++ app/src/main/res/menu/profile_menu.xml | 6 +++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/syng/entity/Profile.java b/app/src/main/java/io/syng/entity/Profile.java index 2b2b908..31b695a 100644 --- a/app/src/main/java/io/syng/entity/Profile.java +++ b/app/src/main/java/io/syng/entity/Profile.java @@ -86,7 +86,7 @@ public class Profile implements Serializable { public List getAddresses() { List addresses = new ArrayList<>(); - for (String privateKey: privateKeys) { + for (String privateKey : privateKeys) { ECKey key = ECKey.fromPrivate(Hex.decode(privateKey)); addresses.add(Hex.toHexString(key.getAddress())); } @@ -241,4 +241,12 @@ public class Profile implements Serializable { privateKeys.add(decryptPrivateKey(privateKey, password)); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Profile)) return false; + Profile object = (Profile) o; + return object.getId().equalsIgnoreCase(id); + } + } diff --git a/app/src/main/java/io/syng/fragment/profile/ProfileDialogFragment.java b/app/src/main/java/io/syng/fragment/profile/ProfileDialogFragment.java index 3afd330..d61ee4d 100644 --- a/app/src/main/java/io/syng/fragment/profile/ProfileDialogFragment.java +++ b/app/src/main/java/io/syng/fragment/profile/ProfileDialogFragment.java @@ -18,29 +18,29 @@ import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.support.v7.widget.Toolbar; +import android.support.v7.widget.Toolbar.OnMenuItemClickListener; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; +import android.widget.Toast; import io.syng.R; import io.syng.adapter.ProfileViewPagerAdapter; import io.syng.entity.Profile; import io.syng.util.GeneralUtil; +import io.syng.util.ProfileManager; public class ProfileDialogFragment extends DialogFragment implements OnPageChangeListener, - OnClickListener { + OnClickListener, OnMenuItemClickListener { private static final String ARG_PROFILE_ID = "profile"; private ViewPager mViewPager; - private TabLayout mTabLayout; private Toolbar mToolbar; private String mProfileId; - private MenuItem importProfile; - private MenuItem exportProfile; public static ProfileDialogFragment newInstance(final Profile profile) { Bundle bundle = new Bundle(); @@ -68,7 +68,7 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.profile_edit_dialog, container); - mTabLayout = (TabLayout) view.findViewById(R.id.profile_tabs); + TabLayout tabLayout = (TabLayout) view.findViewById(R.id.profile_tabs); mViewPager = (ViewPager) view.findViewById(R.id.profile_view_pager); mViewPager.addOnPageChangeListener(this); @@ -76,16 +76,15 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang mToolbar.setTitle("Edit Profile"); mToolbar.inflateMenu(R.menu.profile_menu); - exportProfile = mToolbar.getMenu().findItem(R.id.action_key_export); + MenuItem exportProfile = mToolbar.getMenu().findItem(R.id.action_key_export); exportProfile.setVisible(false); exportProfile.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem menuItem) { - return false; } }); - importProfile = mToolbar.getMenu().findItem(R.id.action_key_import); + MenuItem importProfile = mToolbar.getMenu().findItem(R.id.action_key_import); importProfile.setVisible(false); importProfile.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override @@ -97,10 +96,11 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp); mToolbar.setNavigationOnClickListener(this); + mToolbar.setOnMenuItemClickListener(this); tintMenuItem(); mViewPager.setAdapter(new ProfileViewPagerAdapter(getChildFragmentManager(), getActivity(), mProfileId)); - mTabLayout.setupWithViewPager(mViewPager); + tabLayout.setupWithViewPager(mViewPager); return view; } @@ -115,6 +115,11 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang drawable2 = DrawableCompat.wrap(drawable2); DrawableCompat.setTint(drawable2, getResources().getColor(R.color.drawer_icon_color)); mToolbar.getMenu().findItem(R.id.action_key_import).setIcon(drawable2); + + Drawable drawable3 = mToolbar.getMenu().findItem(R.id.action_remove).getIcon(); + drawable3 = DrawableCompat.wrap(drawable3); + DrawableCompat.setTint(drawable3, getResources().getColor(R.color.drawer_icon_color)); + mToolbar.getMenu().findItem(R.id.action_remove).setIcon(drawable3); } @Override @@ -138,6 +143,22 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang imp.setVisible(showExport); } + @Override + public boolean onMenuItemClick(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_remove: + Profile profile = ProfileManager.getProfileById(mProfileId); + if (ProfileManager.removeProfile(profile)) { + ProfileDialogFragment.this.dismiss(); + } else { + Toast.makeText(getActivity(), "Can't remove current account", Toast.LENGTH_SHORT).show(); + } + + return true; + } + return false; + } + @Override public void onClick(View v) { dismiss(); diff --git a/app/src/main/java/io/syng/util/ProfileManager.java b/app/src/main/java/io/syng/util/ProfileManager.java index 5b238bd..f1a782f 100644 --- a/app/src/main/java/io/syng/util/ProfileManager.java +++ b/app/src/main/java/io/syng/util/ProfileManager.java @@ -55,6 +55,17 @@ public final class ProfileManager { notifyListener(); } + public static boolean removeProfile(Profile profile) { + boolean removed = !profile.equals(ProfileManager.getCurrentProfile()); + if (removed) { + ArrayList profiles = PrefsUtil.getProfiles(); + profiles.remove(profile); + PrefsUtil.saveProfiles(profiles); + notifyListener(); + } + return removed; + } + public static void updateProfile(Profile profile) { ArrayList profiles = getProfiles(); for (Profile item : profiles) { diff --git a/app/src/main/res/menu/profile_menu.xml b/app/src/main/res/menu/profile_menu.xml index 4c0112f..4287ac7 100644 --- a/app/src/main/res/menu/profile_menu.xml +++ b/app/src/main/res/menu/profile_menu.xml @@ -1,6 +1,12 @@ + Hello world! Settings + Remove profile