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;
26 import androidx.appcompat.widget.Toolbar;
27 import androidx.navigation.NavController;
28 import androidx.navigation.Navigation;
30 import net.ktnx.mobileledger.BuildConfig;
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.async.SendTransactionTask;
33 import net.ktnx.mobileledger.async.TaskCallback;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.model.LedgerTransaction;
37 import java.util.Objects;
39 import static net.ktnx.mobileledger.utils.Logger.debug;
42 * TODO: nicer progress while transaction is submitted
44 * TODO: get rid of the custom session/cookie and auth code?
45 * (the last problem with the POST was the missing content-length header)
48 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
49 NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
50 private NavController navController;
52 protected void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
55 setContentView(R.layout.activity_new_transaction);
56 Toolbar toolbar = findViewById(R.id.toolbar);
57 setSupportActionBar(toolbar);
58 Data.profile.observe(this,
59 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
61 navController = Navigation.findNavController(this, R.id.new_transaction_nav);
63 Objects.requireNonNull(getSupportActionBar())
64 .setDisplayHomeAsUpEnabled(true);
67 protected void initProfile() {
68 String profileUUID = getIntent().getStringExtra("profile_uuid");
70 if (profileUUID != null) {
71 mProfile = Data.getProfile(profileUUID);
74 Data.setCurrentProfile(mProfile);
80 public void finish() {
82 overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
85 public boolean onOptionsItemSelected(MenuItem item) {
86 if (item.getItemId() == android.R.id.home) {
90 return super.onOptionsItemSelected(item);
94 protected void onStart() {
96 // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
98 public void onTransactionSave(LedgerTransaction tr) {
99 navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
102 SendTransactionTask saver = new SendTransactionTask(this, mProfile);
105 catch (Exception e) {
106 debug("new-transaction", "Unknown error", e);
108 Bundle b = new Bundle();
109 b.putString("error", "unknown error");
110 navController.navigate(R.id.newTransactionFragment, b);
113 public void simulateCrash(MenuItem item) {
114 debug("crash", "Will crash intentionally");
115 new AsyncCrasher().execute();
117 public boolean onCreateOptionsMenu(Menu menu) {
118 // Inflate the menu; this adds items to the action bar if it is present.
119 getMenuInflater().inflate(R.menu.new_transaction, menu);
121 if (BuildConfig.DEBUG) {
122 menu.findItem(R.id.action_simulate_crash)
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);
142 navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
145 private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
147 protected Void doInBackground(Void... voids) {
148 throw new RuntimeException("Simulated crash");