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