]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
set title when fragment is changed
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / MainActivity.java
index e66cf0451769101c968d9d8be193fb8f7925c3ef..64f613827dc730e2e6f9efe654dabddb58bb1a85 100644 (file)
@@ -21,6 +21,7 @@ import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.os.Build;
 import android.os.Bundle;
+import android.support.annotation.ColorInt;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
@@ -34,9 +35,11 @@ import android.view.ContextMenu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.LinearLayout;
+import android.widget.TextView;
 
-import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment;
 import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
 
 public class MainActivity extends AppCompatActivity {
@@ -44,11 +47,12 @@ public class MainActivity extends AppCompatActivity {
     private AccountSummaryFragment accountSummaryFragment;
     private TransactionListFragment transactionListFragment;
     private Fragment currentFragment = null;
+    private FragmentManager fragmentManager;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_account_summary);
+        setContentView(R.layout.activity_main);
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
 
@@ -70,33 +74,10 @@ public class MainActivity extends AppCompatActivity {
             e.printStackTrace();
         }
 
-        onAccountSummaryClicked(null);
-    }
+        fragmentManager = getSupportFragmentManager();
 
-    @Override
-    protected void onStart() {
-        super.onStart();
-        LinearLayout grp = drawer.findViewById(R.id.nav_actions);
-        for (int i = 0; i < grp.getChildCount(); i++) {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-                grp.getChildAt(i).setBackgroundColor(
-                        getResources().getColor(R.color.drawer_background, getTheme()));
-            }
-            else {
-                grp.getChildAt(i)
-                        .setBackgroundColor(getResources().getColor(R.color.drawer_background));
-            }
-        }
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
-            drawer.findViewById(R.id.nav_account_summary).setBackgroundColor(
-                    getResources().getColor(R.color.table_row_even_bg, getTheme()));
-        }
-        else {
-            drawer.findViewById(R.id.nav_account_summary)
-                    .setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
-        }
+        onAccountSummaryClicked(null);
     }
-
     public void fab_new_transaction_clicked(View view) {
         Intent intent = new Intent(this, NewTransactionActivity.class);
         startActivity(intent);
@@ -112,8 +93,8 @@ public class MainActivity extends AppCompatActivity {
         Intent intent = new Intent(this, SettingsActivity.class);
         startActivity(intent);
     }
-    private void markDrawerItemCurrent(int id) {
-        View item = drawer.findViewById(id);
+    public void markDrawerItemCurrent(int id) {
+        TextView item = drawer.findViewById(id);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg, getTheme()));
         }
@@ -121,11 +102,15 @@ public class MainActivity extends AppCompatActivity {
             item.setBackgroundColor(getResources().getColor(R.color.table_row_even_bg));
         }
 
+        setTitle(item.getText());
+
+        @ColorInt int transparent = getResources().getColor(android.R.color.transparent);
+
         LinearLayout actions = drawer.findViewById(R.id.nav_actions);
         for (int i = 0; i < actions.getChildCount(); i++) {
             View view = actions.getChildAt(i);
             if (view.getId() != id) {
-                view.setBackgroundColor(getResources().getColor(android.R.color.transparent));
+                view.setBackgroundColor(transparent);
             }
         }
     }
@@ -163,24 +148,50 @@ public class MainActivity extends AppCompatActivity {
         }
     }
     public void onAccountSummaryClicked(View view) {
-        markDrawerItemCurrent(R.id.nav_account_summary);
         drawer.closeDrawers();
 
-        FragmentManager fm = getSupportFragmentManager();
-        FragmentTransaction ft = fm.beginTransaction();
-        currentFragment = accountSummaryFragment = new AccountSummaryFragment();
+        resetFragmentBackStack();
+
+        showAccountSummaryFragment();
+    }
+    private void showAccountSummaryFragment() {
+        FragmentTransaction ft = fragmentManager.beginTransaction();
+        accountSummaryFragment = new AccountSummaryFragment();
         ft.replace(R.id.root_frame, accountSummaryFragment);
         ft.commit();
+        currentFragment = accountSummaryFragment;
     }
     public void onLatestTransactionsClicked(View view) {
-        markDrawerItemCurrent(R.id.nav_latest_transactions);
         drawer.closeDrawers();
 
-        FragmentManager fm = getSupportFragmentManager();
-        FragmentTransaction ft = fm.beginTransaction();
-        currentFragment = transactionListFragment = new TransactionListFragment();
+        resetFragmentBackStack();
+
+        showTransactionsFragment(null);
+    }
+    private void resetFragmentBackStack() {
+//        fragmentManager.popBackStack(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
+    }
+    private void showTransactionsFragment(LedgerAccount account) {
+        FragmentTransaction ft = fragmentManager.beginTransaction();
+        if (transactionListFragment == null) {
+            Log.d("flow", "MainActivity creating TransactionListFragment");
+            transactionListFragment = new TransactionListFragment();
+        }
+        Bundle bundle = new Bundle();
+        if (account != null) {
+            bundle.putString(TransactionListFragment.BUNDLE_KEY_FILTER_ACCOUNT_NAME,
+                    account.getName());
+        }
+        transactionListFragment.setArguments(bundle);
         ft.replace(R.id.root_frame, transactionListFragment);
+        if (account != null)
+            ft.addToBackStack(getResources().getString(R.string.title_activity_transaction_list));
         ft.commit();
+
+        currentFragment = transactionListFragment;
+    }
+    public void showAccountTransactions(LedgerAccount account) {
+        showTransactionsFragment(account);
     }
     @Override
     public void onBackPressed() {
@@ -189,6 +200,9 @@ public class MainActivity extends AppCompatActivity {
             drawer.closeDrawer(GravityCompat.START);
         }
         else {
+            Log.d("fragments",
+                    String.format("manager stack: %d", fragmentManager.getBackStackEntryCount()));
+
             super.onBackPressed();
         }
     }