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.ViewModelProviders;
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;
43 * TODO: nicer progress while transaction is submitted
45 * TODO: get rid of the custom session/cookie and auth code?
46 * (the last problem with the POST was the missing content-length header)
49 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
50 NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
51 private NavController navController;
52 private NewTransactionModel model;
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
57 setContentView(R.layout.activity_new_transaction);
58 Toolbar toolbar = findViewById(R.id.toolbar);
59 setSupportActionBar(toolbar);
60 Data.profile.observe(this,
61 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
63 navController = Navigation.findNavController(this, R.id.new_transaction_nav);
65 Objects.requireNonNull(getSupportActionBar())
66 .setDisplayHomeAsUpEnabled(true);
68 model = ViewModelProviders.of(this)
69 .get(NewTransactionModel.class);
72 protected void initProfile() {
73 String profileUUID = getIntent().getStringExtra("profile_uuid");
75 if (profileUUID != null) {
76 mProfile = Data.getProfile(profileUUID);
79 Data.setCurrentProfile(mProfile);
85 public void finish() {
87 overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
90 public boolean onOptionsItemSelected(MenuItem item) {
91 if (item.getItemId() == android.R.id.home) {
95 return super.onOptionsItemSelected(item);
99 protected void onStart() {
101 // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
103 public void onTransactionSave(LedgerTransaction tr) {
104 navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
107 SendTransactionTask saver =
108 new SendTransactionTask(this, mProfile, model.getSimulateSave());
111 catch (Exception e) {
112 debug("new-transaction", "Unknown error", e);
114 Bundle b = new Bundle();
115 b.putString("error", "unknown error");
116 navController.navigate(R.id.newTransactionFragment, b);
119 public void simulateCrash(MenuItem item) {
120 debug("crash", "Will crash intentionally");
121 new AsyncCrasher().execute();
123 public boolean onCreateOptionsMenu(Menu menu) {
124 // Inflate the menu; this adds items to the action bar if it is present.
125 getMenuInflater().inflate(R.menu.new_transaction, menu);
127 if (BuildConfig.DEBUG) {
128 menu.findItem(R.id.action_simulate_crash)
130 menu.findItem(R.id.action_simulate_save)
134 model.observeSimulateSave(this, state -> {
135 menu.findItem(R.id.action_simulate_save)
137 findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
144 public int dp2px(float dp) {
145 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
146 getResources().getDisplayMetrics()));
149 public void done(String error) {
150 Bundle b = new Bundle();
152 b.putString("error", error);
153 navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
156 navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
158 public void toggleSimulateSave(MenuItem item) {
159 model.toggleSimulateSave();