2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.os.Bundle;
21 import android.util.TypedValue;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
26 import androidx.appcompat.widget.Toolbar;
27 import androidx.lifecycle.ViewModelProvider;
28 import androidx.navigation.NavController;
29 import androidx.navigation.Navigation;
31 import net.ktnx.mobileledger.BuildConfig;
32 import net.ktnx.mobileledger.R;
33 import net.ktnx.mobileledger.async.SendTransactionTask;
34 import net.ktnx.mobileledger.async.TaskCallback;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import java.util.Objects;
40 import static net.ktnx.mobileledger.utils.Logger.debug;
42 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
43 NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
44 private NavController navController;
45 private NewTransactionModel model;
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
50 setContentView(R.layout.activity_new_transaction);
51 Toolbar toolbar = findViewById(R.id.toolbar);
52 setSupportActionBar(toolbar);
53 Data.profile.observe(this,
54 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
56 navController = Navigation.findNavController(this, R.id.new_transaction_nav);
58 Objects.requireNonNull(getSupportActionBar())
59 .setDisplayHomeAsUpEnabled(true);
61 model = new ViewModelProvider(this).get(NewTransactionModel.class);
64 protected void initProfile() {
65 String profileUUID = getIntent().getStringExtra("profile_uuid");
67 if (profileUUID != null) {
68 mProfile = Data.getProfile(profileUUID);
71 Data.setCurrentProfile(mProfile);
77 public void finish() {
79 overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
82 public boolean onOptionsItemSelected(MenuItem item) {
83 if (item.getItemId() == android.R.id.home) {
87 return super.onOptionsItemSelected(item);
89 public void onTransactionSave(LedgerTransaction tr) {
90 navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
93 SendTransactionTask saver =
94 new SendTransactionTask(this, mProfile, model.getSimulateSave());
98 debug("new-transaction", "Unknown error", e);
100 Bundle b = new Bundle();
101 b.putString("error", "unknown error");
102 navController.navigate(R.id.newTransactionFragment, b);
105 public void simulateCrash(MenuItem item) {
106 debug("crash", "Will crash intentionally");
107 new AsyncCrasher().execute();
109 public boolean onCreateOptionsMenu(Menu menu) {
110 // Inflate the menu; this adds items to the action bar if it is present.
111 getMenuInflater().inflate(R.menu.new_transaction, menu);
113 if (BuildConfig.DEBUG) {
114 menu.findItem(R.id.action_simulate_crash)
116 menu.findItem(R.id.action_simulate_save)
120 model.observeSimulateSave(this, state -> {
121 menu.findItem(R.id.action_simulate_save)
123 findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
130 public int dp2px(float dp) {
131 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
132 getResources().getDisplayMetrics()));
135 public void done(String error) {
136 Bundle b = new Bundle();
138 b.putString("error", error);
139 navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
142 navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
144 public void toggleSimulateSave(MenuItem item) {
145 model.toggleSimulateSave();