2 * Copyright © 2021 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.new_transaction;
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.fragment.NavHostFragment;
31 import net.ktnx.mobileledger.BuildConfig;
32 import net.ktnx.mobileledger.R;
33 import net.ktnx.mobileledger.async.AsyncCrasher;
34 import net.ktnx.mobileledger.async.SendTransactionTask;
35 import net.ktnx.mobileledger.async.TaskCallback;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.ui.activity.ProfileThemedActivity;
40 import java.util.Objects;
42 import static net.ktnx.mobileledger.utils.Logger.debug;
44 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
45 NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
46 private NavController navController;
47 private NewTransactionModel model;
49 protected void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
52 setContentView(R.layout.activity_new_transaction);
53 Toolbar toolbar = findViewById(R.id.toolbar);
54 setSupportActionBar(toolbar);
55 Data.observeProfile(this,
56 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
58 NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
59 getSupportFragmentManager().findFragmentById(R.id.new_transaction_nav));
60 navController = navHostFragment.getNavController();
62 Objects.requireNonNull(getSupportActionBar())
63 .setDisplayHomeAsUpEnabled(true);
65 model = new ViewModelProvider(this).get(NewTransactionModel.class);
68 protected void initProfile() {
69 String profileUUID = getIntent().getStringExtra("profile_uuid");
71 if (profileUUID != null) {
72 mProfile = Data.getProfile(profileUUID);
75 Data.setCurrentProfile(mProfile);
81 public void finish() {
83 overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
86 public boolean onOptionsItemSelected(MenuItem item) {
87 if (item.getItemId() == android.R.id.home) {
91 return super.onOptionsItemSelected(item);
93 public void onTransactionSave(LedgerTransaction tr) {
94 navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
97 SendTransactionTask saver =
98 new SendTransactionTask(this, mProfile, model.getSimulateSave());
101 catch (Exception e) {
102 debug("new-transaction", "Unknown error", e);
104 Bundle b = new Bundle();
105 b.putString("error", "unknown error");
106 navController.navigate(R.id.newTransactionFragment, b);
109 public void simulateCrash(MenuItem item) {
110 debug("crash", "Will crash intentionally");
111 new AsyncCrasher().execute();
113 public boolean onCreateOptionsMenu(Menu menu) {
114 // Inflate the menu; this adds items to the action bar if it is present.
115 getMenuInflater().inflate(R.menu.new_transaction, menu);
117 if (BuildConfig.DEBUG) {
118 menu.findItem(R.id.action_simulate_crash)
120 menu.findItem(R.id.action_simulate_save)
124 model.observeSimulateSave(this, state -> {
125 menu.findItem(R.id.action_simulate_save)
127 findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
134 public int dp2px(float dp) {
135 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
136 getResources().getDisplayMetrics()));
139 public void done(String error) {
140 Bundle b = new Bundle();
142 b.putString("error", error);
143 navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
146 navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
148 public void toggleSimulateSave(MenuItem item) {
149 model.toggleSimulateSave();