]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
drop unused code
[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     public void onTransactionSave(LedgerTransaction tr) {
90         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
91         try {
92
93             SendTransactionTask saver =
94                     new SendTransactionTask(this, mProfile, model.getSimulateSave());
95             saver.execute(tr);
96         }
97         catch (Exception e) {
98             debug("new-transaction", "Unknown error", e);
99
100             Bundle b = new Bundle();
101             b.putString("error", "unknown error");
102             navController.navigate(R.id.newTransactionFragment, b);
103         }
104     }
105     public void simulateCrash(MenuItem item) {
106         debug("crash", "Will crash intentionally");
107         new AsyncCrasher().execute();
108     }
109     public boolean onCreateOptionsMenu(Menu menu) {
110         // Inflate the menu; this adds items to the action bar if it is present.
111         getMenuInflater().inflate(R.menu.new_transaction, menu);
112
113         if (BuildConfig.DEBUG) {
114             menu.findItem(R.id.action_simulate_crash)
115                 .setVisible(true);
116             menu.findItem(R.id.action_simulate_save)
117                 .setVisible(true);
118         }
119
120         model.observeSimulateSave(this, state -> {
121             menu.findItem(R.id.action_simulate_save)
122                 .setChecked(state);
123             findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
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, b);
140         }
141         else
142             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
143     }
144     public void toggleSimulateSave(MenuItem item) {
145         model.toggleSimulateSave();
146     }
147
148 }