]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
NT: add simulation watermark when simulation is enabled
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionActivity.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.util.TypedValue;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.View;
26
27 import androidx.appcompat.widget.Toolbar;
28 import androidx.lifecycle.ViewModelProviders;
29 import androidx.navigation.NavController;
30 import androidx.navigation.Navigation;
31
32 import net.ktnx.mobileledger.BuildConfig;
33 import net.ktnx.mobileledger.R;
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
39 import java.util.Objects;
40
41 import static net.ktnx.mobileledger.utils.Logger.debug;
42
43 /*
44  * TODO: nicer progress while transaction is submitted
45  * TODO: reports
46  * TODO: get rid of the custom session/cookie and auth code?
47  *         (the last problem with the POST was the missing content-length header)
48  *  */
49
50 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
51         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
52     private NavController navController;
53     private NewTransactionModel model;
54     @Override
55     protected void onCreate(Bundle savedInstanceState) {
56         super.onCreate(savedInstanceState);
57
58         setContentView(R.layout.activity_new_transaction);
59         Toolbar toolbar = findViewById(R.id.toolbar);
60         setSupportActionBar(toolbar);
61         Data.profile.observe(this,
62                 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
63
64         navController = Navigation.findNavController(this, R.id.new_transaction_nav);
65
66         Objects.requireNonNull(getSupportActionBar())
67                .setDisplayHomeAsUpEnabled(true);
68
69         model = ViewModelProviders.of(this)
70                                   .get(NewTransactionModel.class);
71     }
72     @Override
73     protected void initProfile() {
74         String profileUUID = getIntent().getStringExtra("profile_uuid");
75
76         if (profileUUID != null) {
77             mProfile = Data.getProfile(profileUUID);
78             if (mProfile == null)
79                 finish();
80             Data.setCurrentProfile(mProfile);
81         }
82         else
83             super.initProfile();
84     }
85     @Override
86     public void finish() {
87         super.finish();
88         overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
89     }
90     @Override
91     public boolean onOptionsItemSelected(MenuItem item) {
92         if (item.getItemId() == android.R.id.home) {
93             finish();
94             return true;
95         }
96         return super.onOptionsItemSelected(item);
97     }
98
99     @Override
100     protected void onStart() {
101         super.onStart();
102         // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
103     }
104     public void onTransactionSave(LedgerTransaction tr) {
105         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
106         try {
107
108             SendTransactionTask saver =
109                     new SendTransactionTask(this, mProfile, model.getSimulateSave());
110             saver.execute(tr);
111         }
112         catch (Exception e) {
113             debug("new-transaction", "Unknown error", e);
114
115             Bundle b = new Bundle();
116             b.putString("error", "unknown error");
117             navController.navigate(R.id.newTransactionFragment, b);
118         }
119     }
120     public void simulateCrash(MenuItem item) {
121         debug("crash", "Will crash intentionally");
122         new AsyncCrasher().execute();
123     }
124     public boolean onCreateOptionsMenu(Menu menu) {
125         // Inflate the menu; this adds items to the action bar if it is present.
126         getMenuInflater().inflate(R.menu.new_transaction, menu);
127
128         if (BuildConfig.DEBUG) {
129             menu.findItem(R.id.action_simulate_crash)
130                 .setVisible(true);
131             menu.findItem(R.id.action_simulate_save)
132                 .setVisible(true);
133         }
134
135         model.observeSimulateSave(this, state -> {
136             menu.findItem(R.id.action_simulate_save)
137                 .setChecked(state);
138             findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
139         });
140
141         return true;
142     }
143
144
145     public int dp2px(float dp) {
146         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
147                 getResources().getDisplayMetrics()));
148     }
149     @Override
150     public void done(String error) {
151         Bundle b = new Bundle();
152         if (error != null) {
153             b.putString("error", error);
154             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
155         }
156         else
157             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
158     }
159     public void toggleSimulateSave(MenuItem item) {
160         model.toggleSimulateSave();
161     }
162
163     private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
164         @Override
165         protected Void doInBackground(Void... voids) {
166             throw new RuntimeException("Simulated crash");
167         }
168     }
169
170 }