Added import account test.

This commit is contained in:
Adrian Tiberius 2015-08-03 21:39:55 +02:00
parent 5d6a820c4f
commit b52a951fcf
5 changed files with 202 additions and 33 deletions

View File

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

View File

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

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_new_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/accent"
android:text="@string/create_new_account"/>
<RadioButton android:id="@+id/radio_import_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/accent"
android:text="@string/import_account_from_file"/>
<RadioButton android:id="@+id/radio_import_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/accent"
android:text="@string/import_account_from_string"/>
</RadioGroup>
<EditText
android:id="@+id/accout_import_source"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/accent"
android:inputType="none"/>
</LinearLayout>

View File

@ -19,6 +19,12 @@
android:textColor="@color/accent"
android:hint="Name" />
</android.support.design.widget.TextInputLayout>
<TextView
android:id="@+id/account_mode"
android:text="@string/account_mode_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"

View File

@ -26,4 +26,10 @@
<string name="manage_profiles">Manage Profiles</string>
<string name="title_activity_spash_screen">Syng</string>
</resources>
<string name="account_mode_title">Account</string>
<string name="ok">OK</string>
<string name="create_new_account">Create new Account</string>
<string name="import_account_from_file">Import account from file</string>
<string name="import_account_from_string">Import account from string</string>
</resources>