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.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;
27 import androidx.appcompat.widget.Toolbar;
28 import androidx.lifecycle.ViewModelProviders;
29 import androidx.navigation.NavController;
30 import androidx.navigation.Navigation;
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;
39 import java.util.Objects;
41 import static net.ktnx.mobileledger.utils.Logger.debug;
44 * TODO: nicer progress while transaction is submitted
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)
50 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
51 NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
52 private NavController navController;
53 private NewTransactionModel model;
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
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()));
64 navController = Navigation.findNavController(this, R.id.new_transaction_nav);
66 Objects.requireNonNull(getSupportActionBar())
67 .setDisplayHomeAsUpEnabled(true);
69 model = ViewModelProviders.of(this)
70 .get(NewTransactionModel.class);
73 protected void initProfile() {
74 String profileUUID = getIntent().getStringExtra("profile_uuid");
76 if (profileUUID != null) {
77 mProfile = Data.getProfile(profileUUID);
80 Data.setCurrentProfile(mProfile);
86 public void finish() {
88 overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
91 public boolean onOptionsItemSelected(MenuItem item) {
92 if (item.getItemId() == android.R.id.home) {
96 return super.onOptionsItemSelected(item);
100 protected void onStart() {
102 // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
104 public void onTransactionSave(LedgerTransaction tr) {
105 navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
108 SendTransactionTask saver =
109 new SendTransactionTask(this, mProfile, model.getSimulateSave());
112 catch (Exception e) {
113 debug("new-transaction", "Unknown error", e);
115 Bundle b = new Bundle();
116 b.putString("error", "unknown error");
117 navController.navigate(R.id.newTransactionFragment, b);
120 public void simulateCrash(MenuItem item) {
121 debug("crash", "Will crash intentionally");
122 new AsyncCrasher().execute();
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);
128 if (BuildConfig.DEBUG) {
129 menu.findItem(R.id.action_simulate_crash)
131 menu.findItem(R.id.action_simulate_save)
135 model.observeSimulateSave(this, state -> {
136 menu.findItem(R.id.action_simulate_save)
138 findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
145 public int dp2px(float dp) {
146 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
147 getResources().getDisplayMetrics()));
150 public void done(String error) {
151 Bundle b = new Bundle();
153 b.putString("error", error);
154 navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
157 navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
159 public void toggleSimulateSave(MenuItem item) {
160 model.toggleSimulateSave();
163 private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
165 protected Void doInBackground(Void... voids) {
166 throw new RuntimeException("Simulated crash");