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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.activity;
20 import android.content.Intent;
21 import android.content.pm.PackageInfo;
22 import android.os.Build;
23 import android.os.Bundle;
24 import android.support.annotation.ColorInt;
25 import android.support.v4.app.Fragment;
26 import android.support.v4.app.FragmentManager;
27 import android.support.v4.app.FragmentTransaction;
28 import android.support.v4.view.GravityCompat;
29 import android.support.v4.widget.DrawerLayout;
30 import android.support.v7.app.ActionBarDrawerToggle;
31 import android.support.v7.app.AppCompatActivity;
32 import android.support.v7.widget.Toolbar;
33 import android.util.Log;
34 import android.view.ContextMenu;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.widget.LinearLayout;
38 import android.widget.TextView;
40 import net.ktnx.mobileledger.R;
41 import net.ktnx.mobileledger.model.LedgerAccount;
42 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
43 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
44 import net.ktnx.mobileledger.utils.MLDB;
46 import java.time.ZoneId;
47 import java.time.format.DateTimeFormatter;
48 import java.util.Date;
50 public class MainActivity extends AppCompatActivity {
52 private AccountSummaryFragment accountSummaryFragment;
53 private TransactionListFragment transactionListFragment;
54 private Fragment currentFragment = null;
55 private FragmentManager fragmentManager;
56 private TextView tvLastUpdate;
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 setContentView(R.layout.activity_main);
62 Toolbar toolbar = findViewById(R.id.toolbar);
63 setSupportActionBar(toolbar);
65 drawer = findViewById(R.id.drawer_layout);
66 ActionBarDrawerToggle toggle =
67 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
68 R.string.navigation_drawer_close);
69 drawer.addDrawerListener(toggle);
72 android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
76 getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
77 ver.setText(pi.versionName);
83 tvLastUpdate = findViewById(R.id.transactions_last_update);
84 updateLastUpdateText();
85 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
86 Log.d("transactions", String.format("Last update = %d", last_update));
89 fragmentManager = getSupportFragmentManager();
91 onAccountSummaryClicked(null);
93 public void fab_new_transaction_clicked(View view) {
94 Intent intent = new Intent(this, NewTransactionActivity.class);
95 startActivity(intent);
96 overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
99 public void nav_exit_clicked(View view) {
100 Log.w("app", "exiting");
104 public void nav_settings_clicked(View view) {
105 Intent intent = new Intent(this, SettingsActivity.class);
106 startActivity(intent);
108 public void markDrawerItemCurrent(int id) {
109 TextView item = drawer.findViewById(id);
110 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
111 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
114 item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
117 setTitle(item.getText());
119 @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
121 LinearLayout actions = drawer.findViewById(R.id.nav_actions);
122 for (int i = 0; i < actions.getChildCount(); i++) {
123 View view = actions.getChildAt(i);
124 if (view.getId() != id) {
125 view.setBackgroundColor(transparent);
129 public void onOptionsMenuClicked(MenuItem menuItem) {
130 ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
131 switch (menuItem.getItemId()) {
132 case R.id.menu_acc_summary_cancel_selection:
133 if (accountSummaryFragment != null)
134 accountSummaryFragment.onCancelAccSelection(menuItem);
136 case R.id.menu_acc_summary_confirm_selection:
137 if (accountSummaryFragment != null)
138 accountSummaryFragment.onConfirmAccSelection(menuItem);
140 case R.id.menu_acc_summary_only_starred:
141 if (accountSummaryFragment != null)
142 accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
144 case R.id.menu_transaction_list_filter:
145 if (transactionListFragment != null)
146 transactionListFragment.onShowFilterClick(menuItem);
149 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
152 public void onViewClicked(View view) {
153 switch (view.getId()) {
154 case R.id.clearAccountNameFilter:
155 if (transactionListFragment != null)
156 transactionListFragment.onClearAccountNameClick(view);
159 Log.e("click", String.format("View %d click not handled", view.getId()));
162 public void onAccountSummaryClicked(View view) {
163 drawer.closeDrawers();
165 resetFragmentBackStack();
167 showAccountSummaryFragment();
169 private void showAccountSummaryFragment() {
170 FragmentTransaction ft = fragmentManager.beginTransaction();
171 accountSummaryFragment = new AccountSummaryFragment();
172 ft.replace(R.id.root_frame, accountSummaryFragment);
174 currentFragment = accountSummaryFragment;
176 public void onLatestTransactionsClicked(View view) {
177 drawer.closeDrawers();
179 resetFragmentBackStack();
181 showTransactionsFragment(null);
183 private void resetFragmentBackStack() {
184 // fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
186 private void showTransactionsFragment(LedgerAccount account) {
187 FragmentTransaction ft = fragmentManager.beginTransaction();
188 if (transactionListFragment == null) {
189 Log.d("flow", "MainActivity creating TransactionListFragment");
190 transactionListFragment = new TransactionListFragment();
192 Bundle bundle = new Bundle();
193 if (account != null) {
194 bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
197 transactionListFragment.setArguments(bundle);
198 ft.replace(R.id.root_frame, transactionListFragment);
200 ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
203 currentFragment = transactionListFragment;
205 public void showAccountTransactions(LedgerAccount account) {
206 showTransactionsFragment(account);
209 public void onBackPressed() {
210 DrawerLayout drawer = findViewById(R.id.drawer_layout);
211 if (drawer.isDrawerOpen(GravityCompat.START)) {
212 drawer.closeDrawer(GravityCompat.START);
216 String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
218 super.onBackPressed();
221 public void updateLastUpdateText() {
223 long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L);
224 Log.d("transactions", String.format("Last update = %d", last_update));
225 if (last_update == 0) {
226 tvLastUpdate.setText(getString(R.string.transaction_last_update_never));
229 Date date = new Date(last_update);
230 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
231 tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault())
232 .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
235 tvLastUpdate.setText(date.toLocaleString());