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 8afc4a4..b7a350e 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,20 @@ 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); + } } @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 +98,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 +110,7 @@ public void onFailure(Call call, Throwable t) { System.out.println("NetWorkError"+t); //もしコンソール表記をするならこの下に処理を記載 - error_deleteAccount_LiveData.setValue(404); + error_deleteAccount_LiveData.setValue(401); } }); } @@ -123,6 +124,7 @@ public void onResponse(Call call, Response response) { if(response.isSuccessful()){ System.out.println("success"); + accountColorLiveData.setValue(accountColor); }else{ System.out.println("fail"); @@ -145,7 +147,7 @@ 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); @@ -155,7 +157,7 @@ @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..e32be30 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,14 @@ } SettingsViewModel settingsViewModel; + BooksViewModel booksViewModel; + FavoritesViewModel favoritesViewModel; - private String strAccountColor; //色 + private Integer strAccountColor; //色 + private Integer totalamount = 0; + + + @Override @@ -87,6 +97,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,10 +120,13 @@ 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(); //いいね一覧へ変異 @@ -120,41 +135,49 @@ ((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); + 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() { @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("アカウント名が変更されました"); - } else if (error_changeAccount_LiveData == 404) { + } else if (error_changeAccount_LiveData == 404 || error_changeAccount_LiveData == 401) { System.out.println("失敗"); TextView textNotes1 = (TextView) view.findViewById(R.id.textNotes1); - textNotes1.setText("アカウント名が変更されませんでした"); - + 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 +185,71 @@ } 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("アカウントが削除されました"); //画面遷移処理 - }else if(integer == 404){ + Intent intent = (new Intent(getActivity(), SignUpActivity.class)); + startActivity(intent); + }else if(integer == 404 || integer == 401){ TextView textNotes3 = (TextView) view.findViewById(R.id.textNotes3); - textNotes3.setText("アカウントが削除されませんでした"); + 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(); + } + } + }); + booksViewModel.loadBooks(accountId, token); + + //観察部 + //本に対するいいね一覧 + favoritedAccountsLiveData.observe(getViewLifecycleOwner(), new Observer>() { + @Override + public void onChanged(List strings) { + if(strings != null){ + point.add(strings.size()); + totalamount = point.stream().mapToInt(Integer::intValue).sum(); + } + } + }); + + for(int i=0;i