]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
d506ac35ab994f65397c3317779bd2a8e481ff9a
[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.ViewModelProviders;
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: nicer progress while transaction is submitted
44  * TODO: reports
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)
47  *  */
48
49 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
50         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
51     private NavController navController;
52     private NewTransactionModel model;
53     @Override
54     protected void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56
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()));
62
63         navController = Navigation.findNavController(this, R.id.new_transaction_nav);
64
65         Objects.requireNonNull(getSupportActionBar())
66                .setDisplayHomeAsUpEnabled(true);
67
68         model = ViewModelProviders.of(this)
69                                   .get(NewTransactionModel.class);
70     }
71     @Override
72     protected void initProfile() {
73         String profileUUID = getIntent().getStringExtra("profile_uuid");
74
75         if (profileUUID != null) {
76             mProfile = Data.getProfile(profileUUID);
77             if (mProfile == null)
78                 finish();
79             Data.setCurrentProfile(mProfile);
80         }
81         else
82             super.initProfile();
83     }
84     @Override
85     public void finish() {
86         super.finish();
87         overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
88     }
89     @Override
90     public boolean onOptionsItemSelected(MenuItem item) {
91         if (item.getItemId() == android.R.id.home) {
92             finish();
93             return true;
94         }
95         return super.onOptionsItemSelected(item);
96     }
97
98     @Override
99     protected void onStart() {
100         super.onStart();
101         // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
102     }
103     public void onTransactionSave(LedgerTransaction tr) {
104         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
105         try {
106
107             SendTransactionTask saver =
108                     new SendTransactionTask(this, mProfile, model.getSimulateSave());
109             saver.execute(tr);
110         }
111         catch (Exception e) {
112             debug("new-transaction", "Unknown error", e);
113
114             Bundle b = new Bundle();
115             b.putString("error", "unknown error");
116             navController.navigate(R.id.newTransactionFragment, b);
117         }
118     }
119     public void simulateCrash(MenuItem item) {
120         debug("crash", "Will crash intentionally");
121         new AsyncCrasher().execute();
122     }
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);
126
127         if (BuildConfig.DEBUG) {
128             menu.findItem(R.id.action_simulate_crash)
129                 .setVisible(true);
130             menu.findItem(R.id.action_simulate_save)
131                 .setVisible(true);
132         }
133
134         model.observeSimulateSave(this, state -> {
135             menu.findItem(R.id.action_simulate_save)
136                 .setChecked(state);
137             findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
138         });
139
140         return true;
141     }
142
143
144     public int dp2px(float dp) {
145         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
146                 getResources().getDisplayMetrics()));
147     }
148     @Override
149     public void done(String error) {
150         Bundle b = new Bundle();
151         if (error != null) {
152             b.putString("error", error);
153             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
154         }
155         else
156             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
157     }
158     public void toggleSimulateSave(MenuItem item) {
159         model.toggleSimulateSave();
160     }
161
162 }