General and Keys fragments in profile dialog. Added ProfilesChangeListener in ProfileManager

This commit is contained in:
Yaroslav Berezanskyi 2015-08-17 18:18:58 +03:00
parent 00dfe79091
commit b15a447c5e
17 changed files with 363 additions and 58 deletions

View File

@ -23,6 +23,7 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
@ -54,15 +55,16 @@ import io.syng.adapter.ProfileDrawerAdapter;
import io.syng.adapter.ProfileDrawerAdapter.OnProfileClickListener;
import io.syng.entity.Dapp;
import io.syng.entity.Profile;
import io.syng.fragment.ProfileDialogFragment;
import io.syng.fragment.profile.ProfileDialogFragment;
import io.syng.util.GeneralUtil;
import io.syng.util.PrefsUtil;
import io.syng.util.ProfileManager;
import io.syng.util.ProfileManager.ProfilesChangeListener;
import static android.view.View.VISIBLE;
public abstract class BaseActivity extends AppCompatActivity implements
OnClickListener, OnDAppClickListener, OnProfileClickListener, View.OnLongClickListener {
OnClickListener, OnDAppClickListener, OnProfileClickListener, OnLongClickListener, ProfilesChangeListener {
private static final Logger logger = LoggerFactory.getLogger("SyngApplication");
@ -144,6 +146,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
mProfilesRecyclerView.setLayoutManager(layoutManager2);
mProfileDrawerAdapter = new ProfileDrawerAdapter(this, new ArrayList<Profile>(), this);
mProfilesRecyclerView.setAdapter(mProfileDrawerAdapter);
updateCurrentProfileName();
populateProfiles();
mDAppsRecyclerView = (RecyclerView) findViewById(R.id.dapp_drawer_recycler_view);
@ -159,11 +162,12 @@ public abstract class BaseActivity extends AppCompatActivity implements
Glide.with(this).load(PrefsUtil.getBackgroundResourceId(currentProfileId)).into(mHeaderImageView);
GeneralUtil.showWarningDialogIfNeed(this);
ProfileManager.setProfilesChangeListener(this);
}
private void populateProfiles() {
mProfileDrawerAdapter.swapData(ProfileManager.getProfiles());
updateCurrentProfileName();
}
private void populateDApps() {
@ -191,7 +195,8 @@ public abstract class BaseActivity extends AppCompatActivity implements
View view = dialog.getCustomView();
EditText password = (EditText) view.findViewById(R.id.et_pass);
if (profile.checkPassword(password.getText().toString())) {
changeProfile(profile);
ProfileManager.setCurrentProfile(profile);
flipDrawer();
} else {
Toast.makeText(BaseActivity.this, "Password is not correct", Toast.LENGTH_SHORT).show();
}
@ -202,14 +207,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
GeneralUtil.showKeyBoard(name, this);
}
private void changeProfile(Profile profile) {
ProfileManager.setCurrentProfile(profile);
updateCurrentProfileName();
Glide.with(this).load(PrefsUtil.getBackgroundResourceId(profile.getId())).into(mHeaderImageView);
populateDApps();
flipDrawer();
}
private void updateCurrentProfileName() {
TextView textView = (TextView) findViewById(R.id.tv_name);
textView.setText(ProfileManager.getCurrentProfile().getName());
@ -277,6 +274,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacksAndMessages(null);
ProfileManager.removeProfilesChangeListener(this);
}
@Override
@ -339,7 +337,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
profile.setName(name.getText().toString());
profile.setPassword(pass1String);
ProfileManager.addProfile(profile);
mProfileDrawerAdapter.swapData(ProfileManager.getProfiles());
GeneralUtil.hideKeyBoard(name, BaseActivity.this);
GeneralUtil.hideKeyBoard(pass1, BaseActivity.this);
GeneralUtil.hideKeyBoard(pass2, BaseActivity.this);
@ -476,7 +473,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
dapp.setUrl(url);
System.out.println(url);
ProfileManager.updateDAppInProfile(ProfileManager.getCurrentProfile(), dapp);
populateDApps();
if (homeScreenIcon) {
GeneralUtil.createHomeScreenIcon(BaseActivity.this, name, url);
}
@ -503,23 +499,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
@SuppressWarnings("ConstantConditions")
@Override
public void onProfilePress(final Profile profile) {
// MaterialDialog dialog = new MaterialDialog.Builder(this)
// .title("Edit account")
// .content("Put your name to create new account")
// .inputType(InputType.TYPE_CLASS_TEXT)
// .input("Name", "", new MaterialDialog.InputCallback() {
// @Override
// public void onInput(MaterialDialog dialog, CharSequence input) {
// profile.setName(input.toString());
// ProfileManager.updateProfile(profile);
// mProfileDrawerAdapter.swapData(ProfileManager.getProfiles());
// updateCurrentProfileName();
// }
// }).show();
// dialog.getInputEditText().setSingleLine();
// dialog.getInputEditText().setText(profile.getName());
ProfileDialogFragment dialogFragment = ProfileDialogFragment.newInstance();
ProfileDialogFragment dialogFragment = ProfileDialogFragment.newInstance(profile);
dialogFragment.show(getSupportFragmentManager(), "profile_dialog");
}
@ -543,7 +523,6 @@ public abstract class BaseActivity extends AppCompatActivity implements
Dapp dapp = new Dapp(name);
dapp.setUrl(url);
ProfileManager.addDAppToProfile(ProfileManager.getCurrentProfile(), dapp);
populateDApps();
if (homeScreenIcon) {
GeneralUtil.createHomeScreenIcon(BaseActivity.this, name, url);
}
@ -601,4 +580,12 @@ public abstract class BaseActivity extends AppCompatActivity implements
}
return true;
}
@Override
public void onProfilesChange() {
updateCurrentProfileName();
populateDApps();
populateProfiles();
Glide.with(this).load(PrefsUtil.getBackgroundResourceId(ProfileManager.getCurrentProfile().getId())).into(mHeaderImageView);
}
}

View File

@ -0,0 +1,55 @@
package io.syng.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import io.syng.R;
public class ProfileKeyAdapter extends RecyclerView.Adapter<ProfileKeyAdapter.SimpleViewHolder> {
private List<String> mDataSet;
public ProfileKeyAdapter(List<String> data) {
this.mDataSet = data;
}
@Override
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.profile_key_list_item, parent, false);
return new SimpleViewHolder(v);
}
@Override
public void onBindViewHolder(SimpleViewHolder holder, int position) {
holder.keyTextView.setText(mDataSet.get(position));
holder.profileKeyItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return mDataSet.size();
}
static class SimpleViewHolder extends RecyclerView.ViewHolder {
private TextView keyTextView;
private View profileKeyItem;
public SimpleViewHolder(View v) {
super(v);
keyTextView = (TextView) v.findViewById(R.id.text);
profileKeyItem = v.findViewById(R.id.ll_profile_key_item);
}
}
}

View File

@ -7,7 +7,8 @@ import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
import io.syng.fragment.ConsoleFragment;
import io.syng.fragment.profile.ProfileGeneralFragment;
import io.syng.fragment.profile.ProfileKeysFragment;
public final class ProfileViewPagerAdapter extends FragmentPagerAdapter {
@ -16,15 +17,24 @@ public final class ProfileViewPagerAdapter extends FragmentPagerAdapter {
public static final String[] LABELS = new String[]{"General", "Keys"};
private final Context mContext;
private final String mProfileId;
public ProfileViewPagerAdapter(FragmentManager fragmentManager, Context context) {
public ProfileViewPagerAdapter(FragmentManager fragmentManager, Context context, String profileId) {
super(fragmentManager);
this.mContext = context;
mProfileId = profileId;
}
@Override
public Fragment getItem(int position) {
return new ConsoleFragment();
switch (position) {
case GENERAL_POSITION:
return ProfileGeneralFragment.newInstance(mProfileId);
case KEYS_POSITION:
return ProfileKeysFragment.newInstance(mProfileId);
default:
throw new UnsupportedOperationException();
}
}
@Override

View File

@ -1,4 +1,4 @@
package io.syng.fragment;
package io.syng.fragment.profile;
import android.app.Dialog;
import android.graphics.drawable.Drawable;
@ -13,26 +13,37 @@ import android.support.v7.widget.Toolbar;
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 io.syng.R;
import io.syng.adapter.ProfileViewPagerAdapter;
import io.syng.entity.Profile;
public class ProfileDialogFragment extends DialogFragment implements OnPageChangeListener {
public class ProfileDialogFragment extends DialogFragment implements OnPageChangeListener,
OnClickListener {
private static final String ARG_PROFILE_ID = "profile";
private ViewPager mViewPager;
private TabLayout mTabLayout;
private Toolbar mToolbar;
private String mProfileId;
public static ProfileDialogFragment newInstance() {
return new ProfileDialogFragment();
public static ProfileDialogFragment newInstance(final Profile profile) {
Bundle bundle = new Bundle();
bundle.putString(ARG_PROFILE_ID, profile.getId());
ProfileDialogFragment dialogFragment = new ProfileDialogFragment();
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mProfileId = getArguments().getString(ARG_PROFILE_ID);
}
@Override
@ -55,9 +66,11 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang
mToolbar.inflateMenu(R.menu.profile_menu);
mToolbar.getMenu().findItem(R.id.action_key_export).setVisible(false);
mToolbar.getMenu().findItem(R.id.action_key_import).setVisible(false);
mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
mToolbar.setNavigationOnClickListener(this);
tintMenuItem();
mViewPager.setAdapter(new ProfileViewPagerAdapter(getChildFragmentManager(), getActivity()));
mViewPager.setAdapter(new ProfileViewPagerAdapter(getChildFragmentManager(), getActivity(), mProfileId));
mTabLayout.setupWithViewPager(mViewPager);
return view;
@ -95,4 +108,9 @@ public class ProfileDialogFragment extends DialogFragment implements OnPageChang
exp.setVisible(showExport);
imp.setVisible(showExport);
}
@Override
public void onClick(View v) {
dismiss();
}
}

View File

@ -0,0 +1,68 @@
package io.syng.fragment.profile;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import io.syng.R;
import io.syng.entity.Profile;
import io.syng.util.ProfileManager;
public class ProfileGeneralFragment extends Fragment implements TextWatcher {
private static final String ARG_PROFILE_ID = "profile_id";
private String mProfileId;
public static ProfileGeneralFragment newInstance(String profileId) {
Bundle bundle = new Bundle();
bundle.putString(ARG_PROFILE_ID, profileId);
ProfileGeneralFragment dialogFragment = new ProfileGeneralFragment();
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mProfileId = getArguments().getString(ARG_PROFILE_ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_general, container, false);
EditText profileName = (EditText) view.findViewById(R.id.et_profile_name);
profileName.addTextChangedListener(this);
Profile profile = ProfileManager.getProfileById(mProfileId);
if (profile != null) {
profileName.setText(profile.getName());
}
return view;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Profile profile = ProfileManager.getProfileById(mProfileId);
if (profile != null) {
profile.setName(s.toString());
ProfileManager.updateProfile(profile);
}
}
@Override
public void afterTextChanged(Editable s) {
}
}

View File

@ -0,0 +1,59 @@
package io.syng.fragment.profile;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import io.syng.R;
import io.syng.adapter.ProfileKeyAdapter;
import io.syng.entity.Profile;
import io.syng.util.ProfileManager;
public class ProfileKeysFragment extends Fragment {
private static final String ARG_PROFILE_ID = "profile_id";
private String mProfileId;
private RecyclerView mRecyclerView;
private ProfileKeyAdapter mProfileDrawerAdapter;
public static ProfileKeysFragment newInstance(String profileId) {
Bundle bundle = new Bundle();
bundle.putString(ARG_PROFILE_ID, profileId);
ProfileKeysFragment dialogFragment = new ProfileKeysFragment();
dialogFragment.setArguments(bundle);
return dialogFragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mProfileId = getArguments().getString(ARG_PROFILE_ID);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_keys, container, false);
Profile profile = ProfileManager.getProfileById(mProfileId);
if (profile != null) {
profile.getPrivateKeys();
}
mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_profile_keys);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mProfileDrawerAdapter = new ProfileKeyAdapter(profile.getPrivateKeys());
mRecyclerView.setAdapter(mProfileDrawerAdapter);
return view;
}
}

View File

@ -90,11 +90,6 @@ public final class PrefsUtil {
public static boolean addProfile(Profile profile) {
ArrayList<Profile> profiles = PrefsUtil.getProfiles();
for (Profile item : profiles) {
if (item.getName().equals(profile.getName())) {
return false;
}
}
profiles.add(profile);
saveProfiles(profiles);
return true;

View File

@ -1,5 +1,7 @@
package io.syng.util;
import android.support.annotation.Nullable;
import org.ethereum.crypto.HashUtil;
import org.spongycastle.util.encoders.Hex;
@ -14,8 +16,14 @@ import static org.ethereum.config.SystemProperties.CONFIG;
public final class ProfileManager {
public interface ProfilesChangeListener {
void onProfilesChange();
}
private static ProfileManager sInstance;
private ProfilesChangeListener mProfilesChangeListener;
public static void initialize() {
if (sInstance != null) {
throw new IllegalStateException("ProfileManager have already been initialized");
@ -35,12 +43,14 @@ public final class ProfileManager {
return PrefsUtil.getProfiles();
}
public static boolean addProfile(Profile profile) {
return PrefsUtil.addProfile(profile);
public static void addProfile(Profile profile) {
PrefsUtil.addProfile(profile);
notifyListener();
}
public static void updateProfile(Profile profile) {
PrefsUtil.updateProfile(profile);
notifyListener();
}
public static Profile getCurrentProfile() {
@ -78,17 +88,44 @@ public final class ProfileManager {
List<String> privateKeys = profile.getPrivateKeys();
SyngApplication.sEthereumConnector.init(privateKeys);
PrefsUtil.setCurrentProfileId(profile.getId());
notifyListener();
}
public static void addDAppToProfile(Profile profile, Dapp dapp) {
profile.addDapp(dapp);
ProfileManager.updateProfile(profile);
notifyListener();
}
public static void updateDAppInProfile(Profile profile, Dapp dapp) {
profile.updateDapp(dapp);
ProfileManager.updateProfile(profile);
notifyListener();
}
@Nullable
public static Profile getProfileById(String profileId) {
List<Profile> profiles = ProfileManager.getProfiles();
for (Profile profile : profiles) {
if (profile.getId().equals(profileId)) {
return profile;
}
}
return null;
}
public static void setProfilesChangeListener(ProfilesChangeListener listener) {
getInstance().mProfilesChangeListener = listener;
}
public static void removeProfilesChangeListener(ProfilesChangeListener listener) {
getInstance().mProfilesChangeListener = null;
}
private static void notifyListener() {
if (getInstance().mProfilesChangeListener != null) {
getInstance().mProfilesChangeListener.onProfilesChange();
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 146 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

View File

@ -0,0 +1,21 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context="io.syng.fragment.profile.ProfileGeneralFragment">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint.profile.name"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>

View File

@ -0,0 +1,20 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.syng.fragment.profile.ProfileKeysFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="KEYS HERE"
android:visibility="gone"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_profile_keys"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>

View File

@ -6,19 +6,28 @@
android:background="@color/toolbar"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/profile_tabs"
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="scrollable"/>
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/profile_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="@color/toolbar"
app:tabMode="scrollable"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/profile_view_pager"

View File

@ -0,0 +1,26 @@
<LinearLayout
android:id="@+id/ll_profile_key_item"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightSmall"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
style="@style/Base.TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:textColor="@android:color/black"
tools:text="Test text"/>
</LinearLayout>