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