package com.example.citrusclient.views;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
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;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import com.example.citrusclient.Citrus;
import com.example.citrusclient.R;
import com.example.citrusclient.models.Book;
import com.example.citrusclient.rest.AccountsRest;
import com.example.citrusclient.viewmodels.SettingsViewModel;
import com.example.citrusclient.viewmodels.TodosViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 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;
private String strAccountColor; //色
@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);
}
@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();
//いいね一覧へ変異
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 strAccountcolor) {
if (strAccountcolor!=null){
returnAccountColor(strAccountColor);
}
}
});
settingsViewModel.loadAccountColor(accountId);
//アカウント
error_changeAccount_LiveData.observe(getViewLifecycleOwner(), new Observer<Integer>() {
@Override
public void onChanged(Integer error_changeAccount_LiveData) {
if (error_changeAccount_LiveData == 204) {
System.out.println("成功");
TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1);
textNotes1.setText("アカウント名が変更されました");
} else if (error_changeAccount_LiveData == 404) {
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 == 204) {
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("パスワードが変更されました");
}
}
});
//アカウント削除
error_deleteAccount_LiveData.observe(getViewLifecycleOwner(), new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
if (integer == 204){
TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3);
textNotes3.setText("アカウントが削除されました");
//画面遷移処理
}else if(integer == 404){
TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3);
textNotes3.setText("アカウントが削除されませんでした");
}
}
});
//ユーザネーム変更
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();
//String nowpass = NowPass.getText().toString().trim(); //こっちでもよき
/* if (nowpass == null || nowpass.length() == 0) { //現在のパスワードの入力がなかったら
System.out.println("現在のパスワードを正しく入力してください");
textNote1.setText("現在のパスワードを正しく入力してください!");
}
// String newUsername = newUsername.getText().toString().trim(); //こっちでもよき
if (newUsername == null || newUsername.length() == 0) { //新しいユーザネームの入力がなかったら
System.out.println("新しいユーザネームを正しく入力してください");
textNote1.setText("新しいユーザネームを正しく入力してください");
}
//String accountId = ((Citrus) getActivity().getApplication()).getAccountId();
//String token = ((Citrus) getActivity().getApplication()).getToken();
*/
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();
//String nowpass = NowPass.getText().toString().trim(); //こっちでもよき
/*if (nowpass == null || nowpass.length() == 0) { //現在のパスワードの入力がなかったら
System.out.println("現在のパスワードを正しく入力してください");
return;
}
String newPassword = NewPassEditable.toString().trim();
// String newUsername = newUsername.getText().toString().trim(); //こっちでもよき
if (newPassword == null || newPassword.length() == 0) { //新しいユーザネームの入力がなかったら
System.out.println("新しいパスワードを正しく入力してください");
return;
}*/
//String accountId = ((Citrus) getActivity().getApplication()).getAccountId();
//String token = ((Citrus) getActivity().getApplication()).getToken();
settingsViewModel.changePW(accountId, newPassword, nowpass, token); //SettingsViewModelを呼ぶ
}
}
});
//アカウント削除
view.findViewById(R.id.UpdatePass3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText enter_nowPassword = (EditText) view.findViewById(R.id.EnterNowPassword3);
Editable editable_nowPasswrd = enter_nowPassword.getText();
TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3);
if (editable_nowPasswrd.length() == 0){
textNotes3.setText("現在のパスワードを入力してください");
}else{
String nowPassword = editable_nowPasswrd.toString().trim();
settingsViewModel.deleteId(accountId, token, nowPassword);
}
}
});
//
//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) {
strAccountColor = iconColor;//SettingsFragmentの変数accountColorに入る
View view = getView(); // FragmentのView
if (view != null) {
Button iconButton = view.findViewById(R.id.icon);
int parsedColor = Color.parseColor(strAccountColor);
iconButton.setBackgroundTintList(ColorStateList.valueOf(parsedColor));
}
}
}