]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java
shuffle some classes under proper packages
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionActivity.java
1 /*
2  * Copyright © 2021 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.new_transaction;
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.AsyncCrasher;
34 import net.ktnx.mobileledger.async.SendTransactionTask;
35 import net.ktnx.mobileledger.async.TaskCallback;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.ui.activity.ProfileThemedActivity;
39
40 import java.util.Objects;
41
42 import static net.ktnx.mobileledger.utils.Logger.debug;
43
44 public class NewTransactionActivity extends ProfileThemedActivity implements TaskCallback,
45         NewTransactionFragment.OnNewTransactionFragmentInteractionListener {
46     private NavController navController;
47     private NewTransactionModel model;
48     @Override
49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51
52         setContentView(R.layout.activity_new_transaction);
53         Toolbar toolbar = findViewById(R.id.toolbar);
54         setSupportActionBar(toolbar);
55         Data.observeProfile(this,
56                 mobileLedgerProfile -> toolbar.setSubtitle(mobileLedgerProfile.getName()));
57
58         NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull(
59                 getSupportFragmentManager().findFragmentById(R.id.new_transaction_nav));
60         navController = navHostFragment.getNavController();
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     public void onTransactionSave(LedgerTransaction tr) {
94         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
95         try {
96
97             SendTransactionTask saver =
98                     new SendTransactionTask(this, mProfile, model.getSimulateSave());
99             saver.execute(tr);
100         }
101         catch (Exception e) {
102             debug("new-transaction", "Unknown error", e);
103
104             Bundle b = new Bundle();
105             b.putString("error", "unknown error");
106             navController.navigate(R.id.newTransactionFragment, b);
107         }
108     }
109     public void simulateCrash(MenuItem item) {
110         debug("crash", "Will crash intentionally");
111         new AsyncCrasher().execute();
112     }
113     public boolean onCreateOptionsMenu(Menu menu) {
114         // Inflate the menu; this adds items to the action bar if it is present.
115         getMenuInflater().inflate(R.menu.new_transaction, menu);
116
117         if (BuildConfig.DEBUG) {
118             menu.findItem(R.id.action_simulate_crash)
119                 .setVisible(true);
120             menu.findItem(R.id.action_simulate_save)
121                 .setVisible(true);
122         }
123
124         model.observeSimulateSave(this, state -> {
125             menu.findItem(R.id.action_simulate_save)
126                 .setChecked(state);
127             findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
128         });
129
130         return true;
131     }
132
133
134     public int dp2px(float dp) {
135         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
136                 getResources().getDisplayMetrics()));
137     }
138     @Override
139     public void done(String error) {
140         Bundle b = new Bundle();
141         if (error != null) {
142             b.putString("error", error);
143             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
144         }
145         else
146             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
147     }
148     public void toggleSimulateSave(MenuItem item) {
149         model.toggleSimulateSave();
150     }
151
152 }