package com.example.citrusclient.views; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Color; import android.os.Bundle; import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.citrusclient.Citrus; import com.example.citrusclient.R; import com.example.citrusclient.models.Book; import com.example.citrusclient.viewmodels.BooksViewModel; import com.example.citrusclient.viewmodels.FavoritesViewModel; import com.example.citrusclient.viewmodels.SettingsViewModel; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; /** * A simple {@link Fragment} subclass. Use the {@link SettingsFragment#newInstance} factory method * to create an instance of this fragment. */ public class SettingsFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; public SettingsFragment() { // Required empty public constructor } /** * Use this factory method to create a new instance of this fragment using the provided * parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment SettingsFragment. */ // TODO: Rename and change types and number of parameters public static SettingsFragment newInstance(String param1, String param2) { SettingsFragment fragment = new SettingsFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } SettingsViewModel settingsViewModel; BooksViewModel booksViewModel; FavoritesViewModel favoritesViewModel; private Integer strAccountColor; //色 private int totalamount = 0; private List<Integer> bookList = new ArrayList<>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } settingsViewModel = new ViewModelProvider(this).get(SettingsViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); favoritesViewModel = new ViewModelProvider(this).get(FavoritesViewModel.class); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_settings, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); Citrus citrus = (Citrus) (getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); MutableLiveData<String> accountColor = settingsViewModel.getAccountColorLiveData(); MutableLiveData<Integer> error_changePw_LiveData = settingsViewModel.getError_changePw_LiveData(); MutableLiveData<Integer> error_changeAccount_LiveData = settingsViewModel.getError_changeAccount_LiveData(); MutableLiveData<Integer> error_deleteAccount_LiveData = settingsViewModel.getError_deleteAccount_LiveData(); MutableLiveData<HashMap<Integer, Book>> booksLiveData = booksViewModel.getBookLiveData(); MutableLiveData<List<String>> favoritedAccountsLiveData= favoritesViewModel.getFavoritedAccountsLiveData(); //いいね一覧へ変異 Button FavoriteButton = view.findViewById((R.id.goFavoriteList)); FavoriteButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new FavoritesBooksFragment()); }); //観察部 //色 accountColor.observe(getViewLifecycleOwner(), new Observer<String>() { @Override public void onChanged(String s) { if (s!=null){ Button iconButton = view.findViewById(R.id.icon); strAccountColor = Color.parseColor(s); iconButton.setBackgroundTintList(ColorStateList.valueOf(strAccountColor)); } } }); settingsViewModel.loadAccountColor(accountId); if(strAccountColor != null) { Button iconButton = view.findViewById(R.id.icon); iconButton.setBackgroundTintList(ColorStateList.valueOf(strAccountColor)); } //観察部 //アカウント error_changeAccount_LiveData.observe(getViewLifecycleOwner(), new Observer<Integer>() { @Override public void onChanged(Integer error_changeAccount_LiveData) { if (error_changeAccount_LiveData == 200) { System.out.println("成功"); TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); textNotes1.setText("アカウント名が変更されました"); } else if (error_changeAccount_LiveData == 404 || error_changeAccount_LiveData == 401) { System.out.println("失敗"); TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); textNotes1.setText("アカウント名が変更できませんでした"); } } }); //観察部 //パスワード変更 error_changePw_LiveData.observe(getViewLifecycleOwner(), new Observer<Integer>() { @Override public void onChanged(Integer error_changePw_LiveData) { if (error_changePw_LiveData == 200) { System.out.println("成功"); TextView textNotes2 = (TextView) view.findViewById(R.id.textNotes2); textNotes2.setText("パスワードが変更されました"); } else if (error_changePw_LiveData == 404) { System.out.println("失敗"); TextView textNotes2 = (TextView) view.findViewById(R.id.textNotes2); textNotes2.setText("現在のパスワードが違います"); }else if (error_changePw_LiveData == 403 || error_changePw_LiveData == 401){ System.out.println("失敗"); TextView textNotes2 = (TextView) view.findViewById(R.id.textNotes2); textNotes2.setText("パスワードが変更できませんでした"); } } }); //観察部 //アカウント削除 error_deleteAccount_LiveData.observe(getViewLifecycleOwner(), new Observer<Integer>() { @Override public void onChanged(Integer integer) { if (integer == 200){ TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); textNotes3.setText("アカウントが削除されました"); //画面遷移処理 Intent intent = (new Intent(getActivity(),LoginActivity.class)); startActivity(intent); }else if(integer == 404 || integer == 401){ TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); textNotes3.setText("アカウントが削除できませんでした"); } } }); //観察部 //ユーザ(主)が持つ本一覧 //List<Integer> bookList = new ArrayList<>(); List<Integer> point = new ArrayList<>(); booksLiveData.observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() { @Override public void onChanged(HashMap<Integer, Book> integerBookHashMap) { if(integerBookHashMap != null){ bookList.clear(); bookList.addAll(integerBookHashMap.keySet()); //デモ loadFavoritesForBooks(); }else{ bookList.clear(); TextView textLikes = (TextView) view.findViewById(R.id.textLikes); textLikes.setText(Integer.toString(totalamount)); } } }); booksViewModel.loadBooks(accountId, token); //観察部 //本に対するいいね一覧 favoritedAccountsLiveData.observe(getViewLifecycleOwner(), new Observer<List<String>>() { @Override public void onChanged(List<String> strings) { if(strings != null){ point.add(strings.size()); totalamount = point.stream().mapToInt(Integer::intValue).sum(); TextView textLikes = (TextView) view.findViewById(R.id.textLikes); textLikes.setText(Integer.toString(totalamount)); }else{ TextView textLikes = (TextView) view.findViewById(R.id.textLikes); textLikes.setText(Integer.toString(totalamount)); } } }); /* for(int i=0;i<bookList.size();i++){ Integer bookId = bookList.get(i); favoritesViewModel.loadFavoritedAccounts(accountId,bookId,token); }*/ // //ユーザネーム変更 view.findViewById(R.id.UpdateName).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view2) { EditText NowPass = (EditText) view.findViewById(R.id.EnterNowPassword); //現在のパスワードを入力する Editable NowPassEditable = NowPass.getText(); EditText NewUsername = (EditText) view.findViewById(R.id.EnterNewUsename); //新しいユーザーネームを入力する Editable NewIdEditable = NewUsername.getText(); TextView textNote1 = (TextView) view.findViewById(R.id.textNotes1); //ユーザネーム変更でパスワードやユーザーネームなどの入力が無かったら入力してと表示する if (NowPassEditable.length() == 0) { //現在のパスワードの入力がなかったら System.out.println("現在のパスワードを正しく入力してください"); textNote1.setText("現在のパスワードを正しく入力してください!");//画面に表示 } else if (NewIdEditable.length() == 0) { //新しいユーザーネームの入力が無かったら System.out.println("新しいユーザネームを正しく入力してください"); textNote1.setText("新しいユーザネームを正しく入力してください!"); //画面に表示 } else { System.out.println(NowPass); //確認のため System.out.println(NewUsername); String nowpass = NowPassEditable.toString().trim(); String newUsername = NewIdEditable.toString().trim(); settingsViewModel.changeAccount(accountId, newUsername, nowpass, token); //SettingsViewModelを呼ぶ } } }); //パスワード変更 view.findViewById(R.id.UpdatePass).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view2) { EditText NowPass = (EditText) view.findViewById(R.id.EnterNowPassword2); //現在のパスワードを入力する Editable NowPassEditable = NowPass.getText(); EditText NewPass = (EditText) view.findViewById(R.id.EnterNewPassword); //新しいパスワードを入力する Editable NewPassEditable = NewPass.getText(); TextView textNote2 = (TextView) view.findViewById(R.id.textNotes2); //パスワード変更で入力が足りないとき if (NowPassEditable.length() == 0) { //現在のパスワードの入力がなかったら System.out.println("現在のパスワードを正しく入力してください"); textNote2.setText("現在のパスワードを正しく入力してください!");//画面に表示 } else if (NewPassEditable.length() == 0) { //新しいパスワードの入力が無かったら System.out.println("新しいパスワードを正しく入力してください"); textNote2.setText("新しいパスワードを正しく入力してください!"); //画面に表示 } else { System.out.println(NowPass); //確認のため System.out.println(NewPass); String nowpass = NowPassEditable.toString().trim(); String newPassword = NewPassEditable.toString().trim(); settingsViewModel.changePW(accountId, newPassword, nowpass, token); //SettingsViewModelを呼ぶ } } }); //アカウント削除 view.findViewById(R.id.UpdatePass3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view2) { EditText enter_nowPassword = (EditText) view.findViewById(R.id.EnterNowPassword3); Editable editable_nowPassword = enter_nowPassword.getText(); TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); if (editable_nowPassword.length() == 0){ textNotes3.setText("現在のパスワードを入力してください"); }else{ String password = editable_nowPassword.toString().trim(); settingsViewModel.deleteId(accountId, token, password); } } }); // //textLikes.setText(); //アイコンボタンが押されたら view.findViewById(R.id.icon).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //フラグメント表示 AccountColorDialogFragment accountColorDialogFragment = new AccountColorDialogFragment(SettingsFragment.this); accountColorDialogFragment.show(getActivity().getSupportFragmentManager(), "simple2"); } }); } //accountの色を持ってくる public void returnAccountColor(String iconColor) { String strAccountColor = iconColor;//SettingsFragmentの変数accountColorに入る View view = getView(); // FragmentのView Citrus citrus = (Citrus) (getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); if (view != null) { settingsViewModel.changeColor(accountId,strAccountColor,token); } } private void loadFavoritesForBooks() { Citrus citrus = (Citrus) (getActivity().getApplication()); String token = citrus.getToken(); String accountId = citrus.getAccountId(); for(int i = 0; i < bookList.size(); i++){ Integer bookId = bookList.get(i); favoritesViewModel.loadFavoritedAccounts(accountId, bookId, token); } } }