diff --git a/app/src/main/java/com/wallet/crypto/trust/controller/Controller.java b/app/src/main/java/com/wallet/crypto/trust/controller/Controller.java index e30c74c23..5be5d3222 100644 --- a/app/src/main/java/com/wallet/crypto/trust/controller/Controller.java +++ b/app/src/main/java/com/wallet/crypto/trust/controller/Controller.java @@ -149,7 +149,7 @@ public class Controller { // Get transactions new GetTransactionsTask(mAccounts, new OnTaskCompleted() { @Override - public void onTaskCompleted() { + public void onTaskCompleted(TaskResult result) { Log.d(TAG, "Finished loading transactions"); // ... and then get balances @@ -182,6 +182,11 @@ public class Controller { context.startActivity(intent); } + public void navigateToAccountList() { + Intent intent = new Intent(mAppContext, AccountListActivity.class); + mAppContext.startActivity(intent); + } + public void navigateToAccountList(Context context) { Intent intent = new Intent(context, AccountListActivity.class); context.startActivity(intent); @@ -209,12 +214,13 @@ public class Controller { public void clickCreateAccount(Activity activity, String name, String password) throws Exception { Log.d(TAG, String.format("Create account '%s' with pwd '%s", name, password)); + boolean firstAccount = mAccounts.size() == 0; VMAccount account = createAccount(password); mAccounts.add(account); mTransactions.put(account.getAddress(), new ArrayList()); - if (mEtherStore.getAccounts().size() == 0) { + if (firstAccount) { setCurrentAddress(account.getAddress()); } @@ -368,7 +374,7 @@ public class Controller { for (VMAccount a: mAccounts) { getBalance(a); } - mListener.onTaskCompleted(); + mListener.onTaskCompleted(new TaskResult(TaskStatus.SUCCESS, "")); return null; } } @@ -390,10 +396,11 @@ public class Controller { Account account = mEtherStore.importKeyStore(keystoreJson, password); loadAccounts(); Log.d("INFO", "Imported account: " + account.getAddress().getHex()); + listener.onTaskCompleted(new TaskResult(TaskStatus.SUCCESS, "Imported wallet.")); } catch (Exception e) { Log.d("ERROR", e.toString()); + listener.onTaskCompleted(new TaskResult(TaskStatus.FAILURE, "Failed to import wallet: '%s'".format(e.getMessage()))); } - listener.onTaskCompleted(); return null; } } @@ -536,7 +543,7 @@ public class Controller { for (VMAccount a : mAccounts) { fetchTransactionsForAddress(a); } - mListener.onTaskCompleted(); + mListener.onTaskCompleted(new TaskResult(TaskStatus.SUCCESS, "Fetched transactions for all accounts.")); return null; } } diff --git a/app/src/main/java/com/wallet/crypto/trust/controller/OnTaskCompleted.java b/app/src/main/java/com/wallet/crypto/trust/controller/OnTaskCompleted.java index ca8e7d0c5..c905dae98 100644 --- a/app/src/main/java/com/wallet/crypto/trust/controller/OnTaskCompleted.java +++ b/app/src/main/java/com/wallet/crypto/trust/controller/OnTaskCompleted.java @@ -5,6 +5,6 @@ package com.wallet.crypto.trust.controller; */ public interface OnTaskCompleted { - void onTaskCompleted(); + void onTaskCompleted(TaskResult result); } diff --git a/app/src/main/java/com/wallet/crypto/trust/controller/TaskResult.java b/app/src/main/java/com/wallet/crypto/trust/controller/TaskResult.java new file mode 100644 index 000000000..be7dd5e8d --- /dev/null +++ b/app/src/main/java/com/wallet/crypto/trust/controller/TaskResult.java @@ -0,0 +1,23 @@ +package com.wallet.crypto.trust.controller; + +/** + * Created by marat on 10/15/17. + */ + +public class TaskResult { + private TaskStatus status; + private String message; + + public TaskResult(TaskStatus status, String message) { + this.status = status; + this.message = message; + } + + public TaskStatus getStatus() { + return status; + } + + public String getMessage() { + return message; + } +} diff --git a/app/src/main/java/com/wallet/crypto/trust/controller/TaskStatus.java b/app/src/main/java/com/wallet/crypto/trust/controller/TaskStatus.java new file mode 100644 index 000000000..6d1b883d7 --- /dev/null +++ b/app/src/main/java/com/wallet/crypto/trust/controller/TaskStatus.java @@ -0,0 +1,7 @@ +package com.wallet.crypto.trust.controller; + +/** + * Created by marat on 10/15/17. + */ + +public enum TaskStatus { SUCCESS, FAILURE } diff --git a/app/src/main/java/com/wallet/crypto/trust/views/AccountListActivity.java b/app/src/main/java/com/wallet/crypto/trust/views/AccountListActivity.java index 45f7afeec..47a9bbfd0 100644 --- a/app/src/main/java/com/wallet/crypto/trust/views/AccountListActivity.java +++ b/app/src/main/java/com/wallet/crypto/trust/views/AccountListActivity.java @@ -2,6 +2,7 @@ package com.wallet.crypto.trust.views; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.design.widget.BottomNavigationView; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; @@ -38,6 +39,24 @@ public class AccountListActivity extends AppCompatActivity implements DeleteAcco */ private boolean mTwoPane; + private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener + = new BottomNavigationView.OnNavigationItemSelectedListener() { + + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.action_import: + mController.navigateToImportAccount(AccountListActivity.this); + break; + case R.id.action_create: + mController.navigateToCreateAccount(AccountListActivity.this); + break; + } + return false; + } + + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -54,6 +73,9 @@ public class AccountListActivity extends AppCompatActivity implements DeleteAcco mController = Controller.get(); + BottomNavigationView navigation = findViewById(R.id.navigation); + navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); + mRecyclerView = findViewById(R.id.account_list); assert mRecyclerView != null; setupRecyclerView((RecyclerView) mRecyclerView); @@ -78,6 +100,10 @@ public class AccountListActivity extends AppCompatActivity implements DeleteAcco Toast.makeText(AccountListActivity.this, "Delete dialog callback " + password, Toast.LENGTH_SHORT).show(); try { mController.deleteAccount(address, password); + if (mController.getCurrentAccount() == null) { + finish(); // Don't show account list if there are no accounts, + // go to main view which will ask to create a new account + } setupRecyclerView((RecyclerView) mRecyclerView); } catch (Exception e) { Log.e(TAG, e.toString()); @@ -88,12 +114,6 @@ public class AccountListActivity extends AppCompatActivity implements DeleteAcco @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { - case R.id.action_import: - mController.navigateToImportAccount(this); - break; - case R.id.action_create: - mController.navigateToCreateAccount(this); - break; case android.R.id.home: finish(); break; diff --git a/app/src/main/java/com/wallet/crypto/trust/views/CreateAccountActivity.java b/app/src/main/java/com/wallet/crypto/trust/views/CreateAccountActivity.java index d817e0632..999e442f6 100644 --- a/app/src/main/java/com/wallet/crypto/trust/views/CreateAccountActivity.java +++ b/app/src/main/java/com/wallet/crypto/trust/views/CreateAccountActivity.java @@ -3,6 +3,7 @@ package com.wallet.crypto.trust.views; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.annotation.TargetApi; +import android.content.Context; import android.content.pm.PackageManager; import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; @@ -66,6 +67,7 @@ public class CreateAccountActivity extends AppCompatActivity implements LoaderCa private EditText mConfirmPassword; private View mProgressView; private View mLoginFormView; + private Button mImportButton; @Override protected void onCreate(Bundle savedInstanceState) { @@ -113,6 +115,14 @@ public class CreateAccountActivity extends AppCompatActivity implements LoaderCa mLoginFormView = findViewById(R.id.login_form); mProgressView = findViewById(R.id.login_progress); + + mImportButton = findViewById(R.id.import_account_button); + mImportButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + mController.navigateToImportAccount(CreateAccountActivity.this); + } + }); } private void populateAutoComplete() { diff --git a/app/src/main/java/com/wallet/crypto/trust/views/ImportAccountActivity.java b/app/src/main/java/com/wallet/crypto/trust/views/ImportAccountActivity.java index 3bf0171df..b1c7a7ea6 100644 --- a/app/src/main/java/com/wallet/crypto/trust/views/ImportAccountActivity.java +++ b/app/src/main/java/com/wallet/crypto/trust/views/ImportAccountActivity.java @@ -8,10 +8,13 @@ import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; import com.wallet.crypto.trust.R; import com.wallet.crypto.trust.controller.Controller; import com.wallet.crypto.trust.controller.OnTaskCompleted; +import com.wallet.crypto.trust.controller.TaskResult; +import com.wallet.crypto.trust.controller.TaskStatus; public class ImportAccountActivity extends AppCompatActivity { @@ -46,11 +49,16 @@ public class ImportAccountActivity extends AppCompatActivity { mPassword.getText().toString(), new OnTaskCompleted() { @Override - public void onTaskCompleted() { + public void onTaskCompleted(final TaskResult result) { runOnUiThread(new Runnable() { @Override public void run() { - ImportAccountActivity.this.finish(); + if (result.getStatus() == TaskStatus.SUCCESS) { + ImportAccountActivity.this.finish(); + mController.navigateToAccountList(); + } else { + Toast.makeText(ImportAccountActivity.this, result.getMessage(), Toast.LENGTH_SHORT).show(); + } } }); } diff --git a/app/src/main/java/com/wallet/crypto/trust/views/TransactionListActivity.java b/app/src/main/java/com/wallet/crypto/trust/views/TransactionListActivity.java index 236324abb..f673113ff 100644 --- a/app/src/main/java/com/wallet/crypto/trust/views/TransactionListActivity.java +++ b/app/src/main/java/com/wallet/crypto/trust/views/TransactionListActivity.java @@ -7,6 +7,7 @@ import android.graphics.Color; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.NonNull; +import android.support.design.widget.BottomNavigationView; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.RecyclerView; @@ -24,6 +25,7 @@ import android.widget.TextView; import com.wallet.crypto.trust.R; import com.wallet.crypto.trust.controller.Controller; import com.wallet.crypto.trust.controller.OnTaskCompleted; +import com.wallet.crypto.trust.controller.TaskResult; import com.wallet.crypto.trust.model.ESTransaction; import com.wallet.crypto.trust.model.VMAccount; @@ -37,7 +39,7 @@ import java.util.List; * item details. On tablets, the activity presents the list of items and * item details side-by-side using two vertical panes. */ -public class TransactionListActivity extends AppCompatActivity implements OnTaskCompleted { +public class TransactionListActivity extends AppCompatActivity { /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet @@ -49,6 +51,24 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask private SwipeRefreshLayout mSwipeRefreshLayout; private RecyclerView mRecyclerView; + private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener + = new BottomNavigationView.OnNavigationItemSelectedListener() { + + @Override + public boolean onNavigationItemSelected(@NonNull MenuItem item) { + switch (item.getItemId()) { + case R.id.navigation_receive: + mController.navigateToReceive(TransactionListActivity.this); + break; + case R.id.navigation_send: + mController.navigateToSend(TransactionListActivity.this); + break; + } + return false; + } + + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -59,6 +79,9 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask mController = Controller.get(); mController.init(this); + BottomNavigationView navigation = findViewById(R.id.navigation); + navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); + mRecyclerView = (RecyclerView) findViewById(R.id.item_list); assert mRecyclerView != null; setupRecyclerView(mRecyclerView); @@ -81,7 +104,7 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask private void fetchModelsAndReinit() { mController.loadViewModels(new OnTaskCompleted() { @Override - public void onTaskCompleted() { + public void onTaskCompleted(TaskResult result) { asyncInit(); } }); @@ -93,9 +116,6 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask mAddress = account.getAddress(); Log.d(TAG, "Address: %s, Balance: %s".format(mAddress, account.getBalance().toString())); } else { - //Intent intent = new Intent(this, CreateAccountActivity.class); - //this.startActivity(intent); - mAddress ="0xDEADBEEF"; account = new VMAccount(mAddress, "0"); } @@ -136,7 +156,7 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask SharedPreferences.Editor e = getPrefs.edit(); // Edit preference to make it false because we don't want this to run again - e.putBoolean("firstStart", true); + e.putBoolean("firstStart", false); // Apply changes e.apply(); @@ -165,7 +185,12 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask @Override public void onResume() { super.onResume(); + init(); + + if (mController.getCurrentAccount() == null) { + mController.navigateToCreateAccount(this); + } } @Override @@ -202,11 +227,6 @@ public class TransactionListActivity extends AppCompatActivity implements OnTask recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(txns)); } - @Override - public void onTaskCompleted() { - // Populate transactions - } - public class SimpleItemRecyclerViewAdapter extends RecyclerView.Adapter { diff --git a/app/src/main/res/layout/activity_create_account.xml b/app/src/main/res/layout/activity_create_account.xml index fd761067d..3f60ea265 100644 --- a/app/src/main/res/layout/activity_create_account.xml +++ b/app/src/main/res/layout/activity_create_account.xml @@ -75,6 +75,22 @@ android:text="@string/action_sign_in" android:textStyle="bold" /> + + +