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.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    BooksViewModel booksViewModel;
    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();
        booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class);
        booksViewModel.getBookLiveData().observe(this, new Observer<HashMap<Integer, Book>>() {
            @Override
            public void onChanged(HashMap<Integer, Book> integerBookHashMap) {
                citrus.setBook(new ArrayList<>(integerBookHashMap.values()));
            }
        });

        showFragment(new MyBookshelfFragment());

        setNavViewEvent();

    }

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

    private void setNavViewEvent(){
        BottomNavigationView navView = findViewById(R.id.bottomNavigationView);
        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){//検索

                } else if(itemId == R.id.home){//ホーム
                    showFragment(new HomeFragment());
                }else if(itemId == R.id.calendar){ //カレンダ

                }else if(itemId == R.id.face){//マイページ

                }
                return true;
            }
        });
    }
}