]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
5aa6ace2adb5d3abbca451002ed48336b786bb49
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
1 /*
2  * Copyright © 2018 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.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
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.LedgerAccount;
41 import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
42 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
43
44 public class MainActivity extends AppCompatActivity {
45     DrawerLayout drawer;
46     private AccountSummaryFragment accountSummaryFragment;
47     private TransactionListFragment transactionListFragment;
48     private Fragment currentFragment = null;
49     private FragmentManager fragmentManager;
50
51     @Override
52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.activity_main);
55         Toolbar toolbar = findViewById(R.id.toolbar);
56         setSupportActionBar(toolbar);
57
58         drawer = findViewById(R.id.drawer_layout);
59         ActionBarDrawerToggle toggle =
60                 new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open,
61                         R.string.navigation_drawer_close);
62         drawer.addDrawerListener(toggle);
63         toggle.syncState();
64
65         android.widget.TextView ver = drawer.findViewById(R.id.drawer_version_text);
66
67         try {
68             PackageInfo pi =
69                     getApplicationContext().getPackageManager().getPackageInfo(getPackageName(), 0);
70             ver.setText(pi.versionName);
71         }
72         catch (Exception e) {
73             e.printStackTrace();
74         }
75
76         fragmentManager = getSupportFragmentManager();
77
78         onAccountSummaryClicked(null);
79     }
80     public void fab_new_transaction_clicked(View view) {
81         Intent intent = new Intent(this, NewTransactionActivity.class);
82         startActivity(intent);
83         overridePendingTransition(R.anim.slide_in_right, R.anim.dummy);
84     }
85
86     public void nav_exit_clicked(View view) {
87         Log.w("app", "exiting");
88         finish();
89     }
90
91     public void nav_settings_clicked(View view) {
92         Intent intent = new Intent(this, SettingsActivity.class);
93         startActivity(intent);
94     }
95     public void markDrawerItemCurrent(int id) {
96         View item = drawer.findViewById(id);
97         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
98             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
99         }
100         else {
101             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
102         }
103
104         @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
105
106         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
107         for (int i = 0; i < actions.getChildCount(); i++) {
108             View view = actions.getChildAt(i);
109             if (view.getId() != id) {
110                 view.setBackgroundColor(transparent);
111             }
112         }
113     }
114     public void onOptionsMenuClicked(MenuItem menuItem) {
115         ContextMenu.ContextMenuInfo info = menuItem.getMenuInfo();
116         switch (menuItem.getItemId()) {
117             case R.id.menu_acc_summary_cancel_selection:
118                 if (accountSummaryFragment != null)
119                     accountSummaryFragment.onCancelAccSelection(menuItem);
120                 break;
121             case R.id.menu_acc_summary_confirm_selection:
122                 if (accountSummaryFragment != null)
123                     accountSummaryFragment.onConfirmAccSelection(menuItem);
124                 break;
125             case R.id.menu_acc_summary_only_starred:
126                 if (accountSummaryFragment != null)
127                     accountSummaryFragment.onShowOnlyStarredClicked(menuItem);
128                 break;
129             case R.id.menu_transaction_list_filter:
130                 if (transactionListFragment != null)
131                     transactionListFragment.onShowFilterClick(menuItem);
132                 break;
133             default:
134                 Log.e("menu", String.format("Menu item %d not handled", menuItem.getItemId()));
135         }
136     }
137     public void onViewClicked(View view) {
138         switch (view.getId()) {
139             case R.id.clearAccountNameFilter:
140                 if (transactionListFragment != null)
141                     transactionListFragment.onClearAccountNameClick(view);
142                 break;
143             default:
144                 Log.e("click", String.format("View %d click not handled", view.getId()));
145         }
146     }
147     public void onAccountSummaryClicked(View view) {
148         drawer.closeDrawers();
149
150         resetFragmentBackStack();
151
152         showAccountSummaryFragment();
153     }
154     private void showAccountSummaryFragment() {
155         FragmentTransaction ft = fragmentManager.beginTransaction();
156         accountSummaryFragment = new AccountSummaryFragment();
157         ft.replace(R.id.root_frame, accountSummaryFragment);
158         ft.commit();
159         currentFragment = accountSummaryFragment;
160     }
161     public void onLatestTransactionsClicked(View view) {
162         drawer.closeDrawers();
163
164         resetFragmentBackStack();
165
166         showTransactionsFragment(null);
167     }
168     private void resetFragmentBackStack() {
169 //        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
170     }
171     private void showTransactionsFragment(LedgerAccount account) {
172         FragmentTransaction ft = fragmentManager.beginTransaction();
173         if (transactionListFragment == null) {
174             Log.d("flow", "MainActivity creating TransactionListFragment");
175             transactionListFragment = new TransactionListFragment();
176         }
177         Bundle bundle = new Bundle();
178         if (account != null) {
179             bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
180                     account.getName());
181         }
182         transactionListFragment.setArguments(bundle);
183         ft.replace(R.id.root_frame, transactionListFragment);
184         if (account != null)
185             ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
186         ft.commit();
187
188         currentFragment = transactionListFragment;
189     }
190     public void showAccountTransactions(LedgerAccount account) {
191         showTransactionsFragment(account);
192     }
193     @Override
194     public void onBackPressed() {
195         DrawerLayout drawer = findViewById(R.id.drawer_layout);
196         if (drawer.isDrawerOpen(GravityCompat.START)) {
197             drawer.closeDrawer(GravityCompat.START);
198         }
199         else {
200             Log.d("fragments",
201                     String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
202
203             super.onBackPressed();
204         }
205     }
206
207 }