]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
avoid deprecated usage of viewModelProviders
[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 /*
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 = new ViewModelProvider(this).get(NewTransactionModel.class);
69     }
70     @Override
71     protected void initProfile() {
72         String profileUUID = getIntent().getStringExtra("profile_uuid");
73
74         if (profileUUID != null) {
75             mProfile = Data.getProfile(profileUUID);
76             if (mProfile == null)
77                 finish();
78             Data.setCurrentProfile(mProfile);
79         }
80         else
81             super.initProfile();
82     }
83     @Override
84     public void finish() {
85         super.finish();
86         overridePendingTransition(R.anim.dummy, R.anim.slide_out_down);
87     }
88     @Override
89     public boolean onOptionsItemSelected(MenuItem item) {
90         if (item.getItemId() == android.R.id.home) {
91             finish();
92             return true;
93         }
94         return super.onOptionsItemSelected(item);
95     }
96
97     @Override
98     protected void onStart() {
99         super.onStart();
100         // FIXME if (tvDescription.getText().toString().isEmpty()) tvDescription.requestFocus();
101     }
102     public void onTransactionSave(LedgerTransaction tr) {
103         navController.navigate(R.id.action_newTransactionFragment_to_newTransactionSavingFragment);
104         try {
105
106             SendTransactionTask saver =
107                     new SendTransactionTask(this, mProfile, model.getSimulateSave());
108             saver.execute(tr);
109         }
110         catch (Exception e) {
111             debug("new-transaction", "Unknown error", e);
112
113             Bundle b = new Bundle();
114             b.putString("error", "unknown error");
115             navController.navigate(R.id.newTransactionFragment, b);
116         }
117     }
118     public void simulateCrash(MenuItem item) {
119         debug("crash", "Will crash intentionally");
120         new AsyncCrasher().execute();
121     }
122     public boolean onCreateOptionsMenu(Menu menu) {
123         // Inflate the menu; this adds items to the action bar if it is present.
124         getMenuInflater().inflate(R.menu.new_transaction, menu);
125
126         if (BuildConfig.DEBUG) {
127             menu.findItem(R.id.action_simulate_crash)
128                 .setVisible(true);
129             menu.findItem(R.id.action_simulate_save)
130                 .setVisible(true);
131         }
132
133         model.observeSimulateSave(this, state -> {
134             menu.findItem(R.id.action_simulate_save)
135                 .setChecked(state);
136             findViewById(R.id.simulationLabel).setVisibility(state ? View.VISIBLE : View.GONE);
137         });
138
139         return true;
140     }
141
142
143     public int dp2px(float dp) {
144         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
145                 getResources().getDisplayMetrics()));
146     }
147     @Override
148     public void done(String error) {
149         Bundle b = new Bundle();
150         if (error != null) {
151             b.putString("error", error);
152             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
153         }
154         else
155             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
156     }
157     public void toggleSimulateSave(MenuItem item) {
158         model.toggleSimulateSave();
159     }
160
161 }