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