diff --git a/app/src/main/java/io/syng/AddProfileFragment.java b/app/src/main/java/io/syng/AddProfileFragment.java index 2a43024..41e6602 100644 --- a/app/src/main/java/io/syng/AddProfileFragment.java +++ b/app/src/main/java/io/syng/AddProfileFragment.java @@ -6,15 +6,24 @@ import android.os.Bundle; import android.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.text.InputType; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; +import android.widget.RadioButton; import android.widget.Switch; import android.widget.TextView; +import android.widget.Toast; import com.afollestad.materialdialogs.MaterialDialog; +import org.w3c.dom.Text; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -57,10 +66,36 @@ public class AddProfileFragment extends Fragment { private EditText dappName; private EditText dappUrl; + private TextView accountModeButton; + private MaterialDialog accountModeDialog; + private RadioButton accountModeCreate; + private RadioButton accountModeImportFile; + private RadioButton accountModeImportString; + private EditText accountImportSource; + + private int accountMode; + private String accountPrivateKey = null; + private Dapp addDapp = new Dapp("new_app_id", "Add new dapp"); protected int dapEditPosition = -1; + protected View.OnClickListener accountModeListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + accountMode = view.getId(); + switch(accountMode) { + case R.id.radio_new_account: + accountImportSource.setInputType(InputType.TYPE_NULL); + break; + case R.id.radio_import_file: + case R.id.radio_import_string: + accountImportSource.setInputType(InputType.TYPE_CLASS_TEXT); + break; + } + } + }; + /** * Use this factory method to create a new instance of * this fragment using the provided parameters. @@ -105,7 +140,7 @@ public class AddProfileFragment extends Fragment { public Profile getProfile() { - Profile profile = new Profile(); + Profile profile = accountPrivateKey != null ? new Profile(accountPrivateKey) : new Profile(); profile.setName(profileName.getText().toString()); profile.setPasswordProtectedProfile(profilePasswordProtected.isChecked()); List dapps = adapter.getItems(); @@ -150,49 +185,127 @@ public class AddProfileFragment extends Fragment { profileName = (EditText)view.findViewById(R.id.profile_name); profilePasswordProtected = (Switch)view.findViewById(R.id.profile_password_protected); + accountModeButton = (TextView)view.findViewById(R.id.account_mode); dappsList = (RecyclerView) view.findViewById(R.id.profile_dapps_list); - // use this setting to improve performance if you know that changes - // in content do not change the layout size of the RecyclerView - dappsList.setHasFixedSize(true); + accountModeDialog = new MaterialDialog.Builder(getActivity()) + .title(R.string.account_mode_title) + .customView(R.layout.account_creation_mode, true) + .positiveText(R.string.ok) + .negativeText(R.string.cancel) + .contentColor(R.color.accent) // notice no 'res' postfix for literal color + .dividerColorRes(R.color.accent) + .backgroundColorRes(R.color.primary_dark) + .positiveColorRes(R.color.accent) + .negativeColorRes(R.color.accent) + .widgetColorRes(R.color.accent) + .autoDismiss(false) + .callback(new MaterialDialog.ButtonCallback() { - // use a linear layout manager - layoutManager = new LinearLayoutManager(getActivity()); - dappsList.setLayoutManager(layoutManager); + @Override + public void onPositive(MaterialDialog dialog) { - adapter = new DappAdapter(dapps); - resetDapps(); - dappsList.setAdapter(adapter); + boolean hideDialog = true; + accountPrivateKey = null; + switch (accountMode) { + case R.id.radio_new_account: - swipeToAction = new SwipeToAction(dappsList, new SwipeToAction.SwipeListener() { - @Override - public boolean swipeLeft(final Dapp itemData) { + break; + case R.id.radio_import_file: + File file = new File(accountImportSource.getText().toString()); + if (file.exists()) { + StringBuilder text = new StringBuilder(); - adapter.remove(itemData); - return false; //true will move the front view to its starting position - } + try { + BufferedReader buffer = new BufferedReader(new FileReader(file)); + String line; - @Override - public boolean swipeRight(Dapp itemData) { + while ((line = buffer.readLine()) != null) { + text.append(line); + } + buffer.close(); + accountPrivateKey = text.toString(); + } catch (IOException e) { - return true; - } + } + } else { + Toast.makeText(getActivity(), "File not found", Toast.LENGTH_LONG).show(); + hideDialog = false; + } + break; + case R.id.radio_import_string: + accountPrivateKey = accountImportSource.getText().toString(); + break; + } + if (hideDialog) { + dialog.hide(); + } - @Override - public void onClick(Dapp itemData) { - - if (itemData.getId() == "new_app_id") { - createDapp(); - } else { - editDapp(itemData); } - } - @Override - public void onLongClick(Dapp itemData) { + @Override + public void onNegative(MaterialDialog dialog) { + dialog.hide(); + } + }) + .build(); + accountModeButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + accountModeDialog.show(); + } + }); + View accountModeView = accountModeDialog.getCustomView(); + accountModeCreate = (RadioButton)accountModeView.findViewById(R.id.radio_new_account); + accountModeImportFile = (RadioButton)accountModeView.findViewById(R.id.radio_import_file); + accountModeImportString = (RadioButton)accountModeView.findViewById(R.id.radio_import_string); + accountImportSource = (EditText)accountModeView.findViewById(R.id.accout_import_source); + + accountModeCreate.setOnClickListener(accountModeListener); + accountModeImportFile.setOnClickListener(accountModeListener); + accountModeImportString.setOnClickListener(accountModeListener); + // use this setting to improve performance if you know that changes + // in content do not change the layout size of the RecyclerView + dappsList.setHasFixedSize(true); + + // use a linear layout manager + layoutManager = new LinearLayoutManager(getActivity()); + dappsList.setLayoutManager(layoutManager); + + adapter = new DappAdapter(dapps); + resetDapps(); + dappsList.setAdapter(adapter); + + swipeToAction = new SwipeToAction(dappsList, new SwipeToAction.SwipeListener() { + @Override + public boolean swipeLeft(final Dapp itemData) { + + adapter.remove(itemData); + return false; //true will move the front view to its starting position + } + + @Override + public boolean swipeRight(Dapp itemData) { + + return true; + } + + @Override + public void onClick(Dapp itemData) { + + if (itemData.getId() == "new_app_id") { + createDapp(); + } else { + editDapp(itemData); } - }); + } + + @Override + public void onLongClick(Dapp itemData) { + + } + }); boolean wrapInScrollView = true; diff --git a/app/src/main/java/io/syng/entities/Profile.java b/app/src/main/java/io/syng/entities/Profile.java index f5a59e8..a17f2d7 100644 --- a/app/src/main/java/io/syng/entities/Profile.java +++ b/app/src/main/java/io/syng/entities/Profile.java @@ -18,7 +18,17 @@ public class Profile implements Serializable { public Profile() { - this.privateKey = "new key"; + this.privateKey = createPrivateKey(); + } + + public Profile(String privateKey) { + + this.privateKey = privateKey; + } + + protected String createPrivateKey() { + + return ""; } public String getPrivateKey() { diff --git a/app/src/main/res/layout/account_creation_mode.xml b/app/src/main/res/layout/account_creation_mode.xml new file mode 100644 index 0000000..e2e2f18 --- /dev/null +++ b/app/src/main/res/layout/account_creation_mode.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_add_profile.xml b/app/src/main/res/layout/fragment_add_profile.xml index 51eecf1..c904b1d 100644 --- a/app/src/main/res/layout/fragment_add_profile.xml +++ b/app/src/main/res/layout/fragment_add_profile.xml @@ -19,6 +19,12 @@ android:textColor="@color/accent" android:hint="Name" /> + + Manage Profiles Syng - + + Account + OK + Create new Account + Import account from file + Import account from string + \ No newline at end of file