diff --git a/app/src/main/java/com/example/citrusclient/viewmodels/SettingsViewModel.java b/app/src/main/java/com/example/citrusclient/viewmodels/SettingsViewModel.java index d9fe7f6..1fee00a 100644 --- a/app/src/main/java/com/example/citrusclient/viewmodels/SettingsViewModel.java +++ b/app/src/main/java/com/example/citrusclient/viewmodels/SettingsViewModel.java @@ -71,19 +71,29 @@ public void onResponse(Call call, Response response) { if (response.isSuccessful()) { System.out.println("success"); - error_changePw_LiveData.setValue(204); + error_changePw_LiveData.setValue(200); //成功処理 } else { System.out.println("fail"); - error_changePw_LiveData.setValue(404); + switch (response.code()){ + case 404: + //パスワードが違う + error_changePw_LiveData.setValue(404); + break; + case 403: + //認証失敗 + error_changePw_LiveData.setValue(403); + break; + } + } } @Override public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError"+t); - error_changePw_LiveData.setValue(404); + error_changePw_LiveData.setValue(401); } }); } @@ -97,10 +107,10 @@ if(response.isSuccessful()){ System.out.println("success"); //成功処理 - error_deleteAccount_LiveData.setValue(204); + error_deleteAccount_LiveData.setValue(200); }else{ System.out.println("fail"); - //もしコンソール表記をするならこの下に処理を記載 + //パスワードが違う error_deleteAccount_LiveData.setValue(404); } } @@ -109,7 +119,7 @@ public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError"+t); //もしコンソール表記をするならこの下に処理を記載 - error_deleteAccount_LiveData.setValue(404); + error_deleteAccount_LiveData.setValue(401); } }); } @@ -146,17 +156,27 @@ public void onResponse(Call call, Response response) { if(response.isSuccessful()){ System.out.println("success"); - error_changeAccount_LiveData.setValue(204); + error_changeAccount_LiveData.setValue(200); }else{ System.out.println("fail"); - error_changeAccount_LiveData.setValue(404); + switch (response.code()){ + case 404: + //ID・PWが違う + error_changeAccount_LiveData.setValue(404); + break; + case 403: + //承認失敗 + error_changeAccount_LiveData.setValue(403); + break; + } + } } @Override public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError"+t); - error_changeAccount_LiveData.setValue(404); + error_changeAccount_LiveData.setValue(401); } }); } diff --git a/app/src/main/java/com/example/citrusclient/views/SettingsFragment.java b/app/src/main/java/com/example/citrusclient/views/SettingsFragment.java index 901211e..1ca29e4 100644 --- a/app/src/main/java/com/example/citrusclient/views/SettingsFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/SettingsFragment.java @@ -28,12 +28,16 @@ import com.example.citrusclient.R; import com.example.citrusclient.models.Book; import com.example.citrusclient.rest.AccountsRest; +import com.example.citrusclient.viewmodels.BooksViewModel; +import com.example.citrusclient.viewmodels.FavoritesViewModel; 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.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; @@ -75,8 +79,13 @@ } SettingsViewModel settingsViewModel; + BooksViewModel booksViewModel; + FavoritesViewModel favoritesViewModel; - private String strAccountColor; //色 + //private String strAccountColor; //色 + + + @Override @@ -87,6 +96,8 @@ 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 @@ -108,35 +119,42 @@ String accountId = citrus.getAccountId(); + MutableLiveData accountColor = settingsViewModel.getAccountColorLiveData(); MutableLiveData error_changePw_LiveData = settingsViewModel.getError_changePw_LiveData(); MutableLiveData error_changeAccount_LiveData = settingsViewModel.getError_changeAccount_LiveData(); MutableLiveData error_deleteAccount_LiveData = settingsViewModel.getError_deleteAccount_LiveData(); + MutableLiveData> booksLiveData = booksViewModel.getBookLiveData(); + MutableLiveData> favoritedAccountsLiveData= favoritesViewModel.getFavoritedAccountsLiveData(); //いいね一覧へ変異 - Button FavoriteButton = view.findViewById((R.id.goFavoriteList)); + /*Button FavoriteButton = view.findViewById((R.id.goFavoriteList)); FavoriteButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new FavoritesBooksFragment()); - }); + });*/ + //観察部 //色 accountColor.observe(getViewLifecycleOwner(), new Observer() { @Override - public void onChanged(String strAccountcolor) { - if (strAccountcolor!=null){ - returnAccountColor(strAccountColor); + public void onChanged(String s) { + if (s!=null){ + Button iconButton = view.findViewById(R.id.icon); + int parsedColor = Color.parseColor(s); + iconButton.setBackgroundTintList(ColorStateList.valueOf(parsedColor)); } } }); settingsViewModel.loadAccountColor(accountId); + //観察部 //アカウント error_changeAccount_LiveData.observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(Integer error_changeAccount_LiveData) { - if (error_changeAccount_LiveData == 204) { + if (error_changeAccount_LiveData == 200) { System.out.println("成功"); TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); textNotes1.setText("アカウント名が変更されました"); @@ -144,17 +162,21 @@ } else if (error_changeAccount_LiveData == 404) { System.out.println("失敗"); TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); - textNotes1.setText("アカウント名が変更されませんでした"); - + textNotes1.setText("パスワードが違います"); + } else if (error_changeAccount_LiveData == 403 || error_changeAccount_LiveData == 401) { + System.out.println("失敗"); + TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); + textNotes1.setText("アカウント名が変更できませんでした"); } } }); + //観察部 //パスワード変更 error_changePw_LiveData.observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(Integer error_changePw_LiveData) { - if (error_changePw_LiveData == 204) { + if (error_changePw_LiveData == 200) { System.out.println("成功"); TextView textNotes2 = (TextView) view.findViewById(R.id.textNotes2); textNotes2.setText("パスワードが変更されました"); @@ -162,28 +184,76 @@ } else if (error_changePw_LiveData == 404) { System.out.println("失敗"); TextView textNotes2 = (TextView) view.findViewById(R.id.textNotes2); - textNotes2.setText("パスワードが変更されました"); + 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() { @Override public void onChanged(Integer integer) { - if (integer == 204){ + if (integer == 200){ TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); textNotes3.setText("アカウントが削除されました"); //画面遷移処理 + Intent intent = (new Intent(getActivity(), SignUpActivity.class)); + startActivity(intent); }else if(integer == 404){ TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); - textNotes3.setText("アカウントが削除されませんでした"); + textNotes3.setText("パスワードが違います"); + }else if(integer == 401){ + TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); + textNotes3.setText("アカウントが削除できませんでした"); } } }); + //観察部 + //ユーザ(主)が持つ本一覧 + List bookList = new ArrayList<>(); + List point = new ArrayList<>(); + booksLiveData.observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(HashMap integerBookHashMap) { + if(integerBookHashMap != null){ + bookList.clear(); + bookList.addAll(integerBookHashMap.keySet()); + }else{ + bookList.clear(); + TextView textLikes = (TextView) view.findViewById(R.id.textLikes); + textLikes.setText("0"); + } + } + }); + booksViewModel.loadBooks(accountId, token); + //観察部 + //本に対するいいね一覧 + favoritedAccountsLiveData.observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List strings) { + if(strings != null){ + point.add(strings.size()); + int totalamount = point.stream().mapToInt(Integer::intValue).sum(); + TextView textLikes = (TextView) view.findViewById(R.id.textLikes); + textLikes.setText(Integer.toString(totalamount)); + }else{ + + } + } + + }); + for(int i=0;i