]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
move new transaction UI into a fragment, have a clean saving progress
[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
26 import androidx.appcompat.widget.Toolbar;
27 import androidx.navigation.NavController;
28 import androidx.navigation.Navigation;
29
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;
36
37 import java.util.Objects;
38
39 import static net.ktnx.mobileledger.utils.Logger.debug;
40
41 /*
42  * TODO: nicer progress while transaction is submitted
43  * TODO: reports
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)
46  *  */
47
48 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
49         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
50     private NavController navController;
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54
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()));
60
61         navController = Navigation.findNavController(this, R.id.new_transaction_nav);
62
63         Objects.requireNonNull(getSupportActionBar())
64                .setDisplayHomeAsUpEnabled(true);
65     }
66     @Override
67     protected void initProfile() {
68         String profileUUID = getIntent().getStringExtra("profile_uuid");
69
70         if (profileUUID != null) {
71             mProfile = Data.getProfile(profileUUID);
72             if (mProfile == null)
73                 finish();
74             Data.setCurrentProfile(mProfile);
75         }
76         else
77             super.initProfile();
78     }
79     @Override
80     public void finish() {
81         super.finish();
82         overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
83     }
84     @Override
85     public boolean onOptionsItemSelected(MenuItem item) {
86         switch (item.getItemId()) {
87             case android.R.id.home:
88                 finish();
89                 return true;
90         }
91         return super.onOptionsItemSelected(item);
92     }
93     @Override
94     protected void onStart() {
95         super.onStart();
96         // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
97     }
98     public void onTransactionSave(LedgerTransaction tr) {
99         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
100         try {
101
102             SendTransactionTask saver = new SendTransactionTask(this, mProfile);
103             saver.execute(tr);
104         }
105         catch (Exception e) {
106             debug("new-transaction", "Unknown error", e);
107
108             Bundle b = new Bundle();
109             b.putString("error", "unknown error");
110             navController.navigate(R.id.newTransactionFragment, b);
111         }
112     }
113     public void simulateCrash(MenuItem item) {
114         debug("crash", "Will crash intentionally");
115         new AsyncCrasher().execute();
116     }
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);
120
121         if (BuildConfig.DEBUG) {
122             menu.findItem(R.id.action_simulate_crash)
123                 .setVisible(true);
124         }
125
126         return true;
127     }
128
129
130     public int dp2px(float dp) {
131         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
132                 getResources().getDisplayMetrics()));
133     }
134     @Override
135     public void done(String error) {
136         Bundle b = new Bundle();
137         if (error != null) {
138             b.putString("error", error);
139             navController.navigate(R.id.action_newTransactionSavingFragment_Failure);
140         }
141         else
142             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
143     }
144
145     private class AsyncCrasher extends AsyncTask<Void, Void, Void> {
146         @Override
147         protected Void doInBackground(Void... voids) {
148             throw new RuntimeException("Simulated crash");
149         }
150     }
151
152 }