]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
two NPE fixed
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
2  * Copyright © 2019 Damyan Ivanov.
3  * This file is part of Mobile-Ledger.
4  * Mobile-Ledger 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  * Mobile-Ledger 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 Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.pm.PackageInfo;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.support.annotation.ColorInt;
26 import android.support.v4.app.Fragment;
27 import android.support.v4.app.FragmentManager;
28 import android.support.v4.app.FragmentPagerAdapter;
29 import android.support.v4.view.GravityCompat;
30 import android.support.v4.view.ViewPager;
31 import android.support.v4.widget.DrawerLayout;
32 import android.support.v7.app.ActionBarDrawerToggle;
33 import android.support.v7.app.AppCompatActivity;
34 import android.support.v7.widget.Toolbar;
35 import android.util.Log;
36 import android.view.View;
37 import android.widget.LinearLayout;
38 import android.widget.ProgressBar;
39 import android.widget.TextView;
40
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.async.RefreshDescriptionsTask;
43 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
44 import net.ktnx.mobileledger.model.Data;
45 import net.ktnx.mobileledger.model.LedgerAccount;
46 import net.ktnx.mobileledger.model.MobileLedgerProfile;
47 import net.ktnx.mobileledger.ui.MobileLedgerListFragment;
48 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
49 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
50 import net.ktnx.mobileledger.utils.MLDB;
51
52 import java.lang.ref.WeakReference;
53 import java.text.DateFormat;
54 import java.time.ZoneId;
55 import java.time.format.DateTimeFormatter;
56 import java.util.Date;
57 import java.util.Observable;
58 import java.util.Observer;
59
60 public class MainActivity extends AppCompatActivity {
61     public MobileLedgerListFragment currentFragment = null;
62     DrawerLayout drawer;
63     private AccountSummaryFragment accountSummaryFragment;
64     private TransactionListFragment transactionListFragment;
65     private FragmentManager fragmentManager;
66     private TextView tvLastUpdate;
67     private RetrieveTransactionsTask retrieveTransactionsTask;
68     private View bTransactionListCancelDownload;
69     private ProgressBar progressBar;
70     private LinearLayout progressLayout;
71     private SectionsPagerAdapter mSectionsPagerAdapter;
72     private ViewPager mViewPager;
73
74     @Override
75     protected void onStart() {
76         super.onStart();
77
78         Data.lastUpdateDate.set(null);
79         updateLastUpdateTextFromDB();
80         Date lastUpdate = Data.lastUpdateDate.get();
81
82         long now = new Date().getTime();
83         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
84             if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
85             else Log.d("db",
86                     String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching",
87                             lastUpdate.getTime() / 1000f, now / 1000f));
88
89             scheduleTransactionListRetrieval();
90         }
91     }
92     @Override
93     protected void onCreate(Bundle savedInstanceState) {
94         super.onCreate(savedInstanceState);
95         setContentView(R.layout.activity_main);
96         Toolbar toolbar = findViewById(R.id.toolbar);
97         setSupportActionBar(toolbar);
98
99         Data.profile.addObserver(new Observer() {
100             @Override
101             public void update(Observable o, Object arg) {
102                 MobileLedgerProfile profile = Data.profile.get();
103                 runOnUiThread(() -> {
104                     if (profile == null) toolbar.setSubtitle("");
105                     else toolbar.setSubtitle(profile.getName());
106                 });
107             }
108         });
109
110         setupProfile();
111
112         drawer = findViewById(R.id.drawer_layout);
113         ActionBarDrawerToggle toggle =
114                 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
115                         R.string.navigation_drawer_close);
116         drawer.addDrawerListener(toggle);
117         toggle.syncState();
118
119         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
120
121         try {
122             PackageInfo pi =
123                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
124             ver.setText(pi.versionName);
125         }
126         catch (Exception e) {
127             e.printStackTrace();
128         }
129
130         tvLastUpdate = findViewById(R.id.transactions_last_update);
131
132         bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download);
133         progressBar = findViewById(R.id.transaction_list_progress_bar);
134         if (progressBar == null)
135             throw new RuntimeException("Can't get hold on the transaction value progress bar");
136         progressLayout = findViewById(R.id.transaction_progress_layout);
137         if (progressLayout == null) throw new RuntimeException(
138                 "Can't get hold on the transaction value progress bar layout");
139
140         fragmentManager = getSupportFragmentManager();
141         mSectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager);
142
143         mViewPager = findViewById(R.id.root_frame);
144         mViewPager.setAdapter(mSectionsPagerAdapter);
145         mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
146             @Override
147             public void onPageSelected(int position) {
148                 switch (position) {
149                     case 0:
150                         markDrawerItemCurrent(R.id.nav_account_summary);
151                         break;
152                     case 1:
153                         markDrawerItemCurrent(R.id.nav_latest_transactions);
154                         break;
155                     default:
156                         Log.e("MainActivity", String.format("Unexpected page index %d", position));
157                 }
158
159                 super.onPageSelected(position);
160             }
161         });
162
163         Data.lastUpdateDate.addObserver((o, arg) -> {
164             Log.d("main", "lastUpdateDate changed");
165             runOnUiThread(() -> {
166                 Date date = Data.lastUpdateDate.get();
167                 if (date == null) {
168                     tvLastUpdate.setText(R.string.transaction_last_update_never);
169                 }
170                 else {
171                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
172                         tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
173                                 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
174                     }
175                     else {
176                         tvLastUpdate.setText(DateFormat.getDateTimeInstance().format(date));
177                     }
178                 }
179             });
180         });
181     }
182     private void setupProfile() {
183         Data.profiles.setList(MobileLedgerProfile.loadAllFromDB());
184         MobileLedgerProfile profile = null;
185
186         String profileUUID = MLDB.get_option_value(MLDB.OPT_PROFILE_UUID, null);
187         if (profileUUID == null) {
188             if (Data.profiles.isEmpty()) {
189                 Data.profiles.setList(MobileLedgerProfile.createInitialProfileList());
190                 profile = Data.profiles.get(0);
191
192                 SharedPreferences backend = getSharedPreferences("backend", MODE_PRIVATE);
193                 Log.d("profiles", "Migrating from preferences to profiles");
194                 // migration to multiple profiles
195                 if (profile.getUrl().isEmpty()) {
196                     // no legacy config
197                     Intent intent = new Intent(this, ProfileListActivity.class);
198                     startActivity(intent);
199                 }
200                 profile.setUrl(backend.getString("backend_url", ""));
201                 profile.setAuthEnabled(backend.getBoolean("backend_use_http_auth", false));
202                 profile.setAuthUserName(backend.getString("backend_auth_user", null));
203                 profile.setAuthPassword(backend.getString("backend_auth_password", null));
204                 profile.storeInDB();
205                 SharedPreferences.Editor editor = backend.edit();
206                 editor.clear();
207                 editor.apply();
208             }
209         }
210         else {
211             profile = MobileLedgerProfile.loadUUIDFromDB(profileUUID);
212         }
213
214         if (profile == null) profile = Data.profiles.get(0);
215
216         if (profile == null) throw new AssertionError("profile must have a value");
217
218         Data.profile.set(profile);
219         MLDB.set_option_value(MLDB.OPT_PROFILE_UUID, profile.getUuid());
220
221         if (profile.getUrl().isEmpty()) {
222             Intent intent = new Intent(this, ProfileListActivity.class);
223             Bundle args = new Bundle();
224             args.putInt(ProfileListActivity.ARG_ACTION, ProfileListActivity.ACTION_EDIT_PROFILE);
225             args.putInt(ProfileListActivity.ARG_PROFILE_INDEX, 0);
226             intent.putExtras(args);
227             startActivity(intent, args);
228         }
229     }
230     public void fab_new_transaction_clicked(View view) {
231         Intent intent = new Intent(this, NewTransactionActivity.class);
232         startActivity(intent);
233         overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
234     }
235
236     public void nav_exit_clicked(View view) {
237         Log.w("app", "exiting");
238         finish();
239     }
240
241     public void nav_settings_clicked(View view) {
242         Intent intent = new Intent(this, SettingsActivity.class);
243         startActivity(intent);
244     }
245     public void markDrawerItemCurrent(int id) {
246         TextView item = drawer.findViewById(id);
247         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
248             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
249         }
250         else {
251             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
252         }
253
254         setTitle(item.getText());
255
256         @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
257
258         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
259         for (int i = 0; i < actions.getChildCount(); i++) {
260             View view = actions.getChildAt(i);
261             if (view.getId() != id) {
262                 view.setBackgroundColor(transparent);
263             }
264         }
265     }
266     public void onViewClicked(View view) {
267         switch (view.getId()) {
268             case R.id.clearAccountNameFilter:
269                 if (transactionListFragment != null)
270                     transactionListFragment.onClearAccountNameClick(view);
271                 break;
272             default:
273                 Log.e("click", String.format("View %d click not handled", view.getId()));
274         }
275     }
276     public void onAccountSummaryClicked(View view) {
277         drawer.closeDrawers();
278
279         showAccountSummaryFragment();
280     }
281     private void showAccountSummaryFragment() {
282         mViewPager.setCurrentItem(0, true);
283 //        FragmentTransaction ft = fragmentManager.beginTransaction();
284 //        accountSummaryFragment = new AccountSummaryFragment();
285 //        ft.replace(R.id.root_frame, accountSummaryFragment);
286 //        ft.commit();
287 //        currentFragment = accountSummaryFragment;
288     }
289     public void onLatestTransactionsClicked(View view) {
290         drawer.closeDrawers();
291
292         showTransactionsFragment(null);
293     }
294     private void resetFragmentBackStack() {
295 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
296     }
297     private void showTransactionsFragment(LedgerAccount account) {
298         mViewPager.setCurrentItem(1, true);
299 //        FragmentTransaction ft = fragmentManager.beginTransaction();
300 //        if (transactionListFragment == null) {
301 //            Log.d("flow", "MainActivity creating TransactionListFragment");
302 //            transactionListFragment = new TransactionListFragment();
303 //        }
304 //        Bundle bundle = new Bundle();
305 //        if (account != null) {
306 //            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
307 //                    account.getName());
308 //        }
309 //        transactionListFragment.setArguments(bundle);
310 //        ft.replace(R.id.root_frame, transactionListFragment);
311 //        if (account != null)
312 //            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
313 //        ft.commit();
314 //
315 //        currentFragment = transactionListFragment;
316     }
317     public void showAccountTransactions(LedgerAccount account) {
318         showTransactionsFragment(account);
319     }
320     @Override
321     public void onBackPressed() {
322         DrawerLayout drawer = findViewById(R.id.drawer_layout);
323         if (drawer.isDrawerOpen(GravityCompat.START)) {
324             drawer.closeDrawer(GravityCompat.START);
325         }
326         else {
327             Log.d("fragments",
328                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
329
330             super.onBackPressed();
331         }
332     }
333     public void updateLastUpdateTextFromDB() {
334         {
335             long last_update = Data.profile.get().get_option_value(MLDB.OPT_LAST_SCRAPE, 0L);
336
337             Log.d("transactions", String.format("Last update = %d", last_update));
338             if (last_update == 0) {
339                 Data.lastUpdateDate.set(null);
340             }
341             else {
342                 Data.lastUpdateDate.set(new Date(last_update));
343             }
344         }
345     }
346     public void scheduleTransactionListRetrieval() {
347         retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this));
348
349         retrieveTransactionsTask.execute();
350         bTransactionListCancelDownload.setEnabled(true);
351     }
352     public void onStopTransactionRefreshClick(View view) {
353         Log.d("interactive", "Cancelling transactions refresh");
354         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
355         bTransactionListCancelDownload.setEnabled(false);
356     }
357     public void onRetrieveDone(boolean success) {
358         progressLayout.setVisibility(View.GONE);
359         updateLastUpdateTextFromDB();
360
361         new RefreshDescriptionsTask().execute();
362     }
363     public void onRetrieveStart() {
364         progressBar.setIndeterminate(true);
365         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) progressBar.setProgress(0, false);
366         else progressBar.setProgress(0);
367         progressLayout.setVisibility(View.VISIBLE);
368     }
369     public void onRetrieveProgress(RetrieveTransactionsTask.Progress progress) {
370         if ((progress.getTotal() == RetrieveTransactionsTask.Progress.INDETERMINATE) ||
371             (progress.getTotal() == 0))
372         {
373             progressBar.setIndeterminate(true);
374         }
375         else {
376             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
377                 progressBar.setMin(0);
378             }
379             progressBar.setMax(progress.getTotal());
380             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
381                 progressBar.setProgress(progress.getProgress(), true);
382             }
383             else progressBar.setProgress(progress.getProgress());
384             progressBar.setIndeterminate(false);
385         }
386     }
387     public void nav_profiles_clicked(View view) {
388         drawer.closeDrawers();
389         Intent intent = new Intent(this, ProfileListActivity.class);
390         startActivity(intent);
391     }
392     public class SectionsPagerAdapter extends FragmentPagerAdapter {
393
394         public SectionsPagerAdapter(FragmentManager fm) {
395             super(fm);
396         }
397
398         @Override
399         public Fragment getItem(int position) {
400             Log.d("main", String.format("Switching to gragment %d", position));
401             switch (position) {
402                 case 0:
403                     return new AccountSummaryFragment();
404                 case 1:
405                     return new TransactionListFragment();
406                 default:
407                     throw new IllegalStateException(
408                             String.format("Unexpected fragment index: " + "%d", position));
409             }
410         }
411
412         @Override
413         public int getCount() {
414             return 2;
415         }
416     }
417
418 }