Remove profile functionality

This commit is contained in:
Yaroslav Berezanskyi 2015-09-09 11:23:14 +03:00
parent b529bc8179
commit 4322700964
5 changed files with 57 additions and 10 deletions

View File

@ -86,7 +86,7 @@ public class Profile implements Serializable {
public List<String> getAddresses() {
List<String> 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);
}
}

View File

@ -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();

View File

@ -55,6 +55,17 @@ public final class ProfileManager {
notifyListener();
}
public static boolean removeProfile(Profile profile) {
boolean removed = !profile.equals(ProfileManager.getCurrentProfile());
if (removed) {
ArrayList<Profile> profiles = PrefsUtil.getProfiles();
profiles.remove(profile);
PrefsUtil.saveProfiles(profiles);
notifyListener();
}
return removed;
}
public static void updateProfile(Profile profile) {
ArrayList<Profile> profiles = getProfiles();
for (Profile item : profiles) {

View File

@ -1,6 +1,12 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_remove"
android:icon="@drawable/ic_delete_black_24dp"
android:orderInCategory="100"
android:title="@string/menu.profile.remove"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_key_import"

View File

@ -41,5 +41,6 @@
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="menu.profile.remove">Remove profile</string>
</resources>