Newer
Older
CitrusClient / app / src / main / java / com / example / citrusclient / views / MainActivity.java
package com.example.citrusclient.views;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.citrusclient.Citrus;
import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;
import com.example.citrusclient.viewmodels.BooksViewModel;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Citrus citrus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });



        citrus = (Citrus) getApplication();
        Calendar calendar = Calendar.getInstance();
        citrus.setCurYear(calendar.get(Calendar.YEAR));
        citrus.setCurMonth(calendar.get(Calendar.MONTH) + 1);
        citrus.setCurDay(calendar.get(Calendar.DATE));
        citrus.setCurBookId(-1);
//        showFragment(new HomeFragment());

        setNavViewEvent();

    }

    public void showFragment(Fragment fragment){
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .addToBackStack(null)
                .commit();
    }

    public void backFragment(){
        getSupportFragmentManager().popBackStack();
    }

    private void setNavViewEvent(){
        BottomNavigationView navView = findViewById(R.id.bottomNavigationView);
        navView.setSelectedItemId(R.id.home);
        showFragment(new HomeFragment());
        navView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int itemId = menuItem.getItemId();
                if(itemId == R.id.book){// 本棚
                    showFragment(new MyBookshelfFragment());
                } else if(itemId == R.id.search){//検索
                    showFragment(new SearchFragment());
                } else if(itemId == R.id.home){//ホーム
                    showFragment(new HomeFragment());
                }else if(itemId == R.id.calendar){ //カレンダ
                    showFragment(new CalendarFragment());
                }else if(itemId == R.id.face){//マイページ
                    showFragment(new SettingsFragment());
                }
                return true;
            }
        });
    }
}