package com.example.nemophila.viewmodels; import android.util.Log; import android.widget.TextView; import com.example.nemophila.R; import com.example.nemophila.entities.Account; import com.example.nemophila.entities.AccountJson; import com.example.nemophila.entities.AccountNameJson; import com.example.nemophila.entities.ErrorType; import com.example.nemophila.entities.Post; import com.example.nemophila.entities.PostJson; import com.example.nemophila.resources.AccountsRest; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.jackson.JacksonConverterFactory; public class AccountViewModel extends ViewModel { // フィールド private final Retrofit retrofit; private final AccountsRest accountsRest; // ライブデータ private final MutableLiveData<String> nameLiveData; private final MutableLiveData<String> pwLiveData; private final MutableLiveData<Collection<Post>> accountPostsLiveData; private final MutableLiveData<String> pwErrorLiveData; private final MutableLiveData<Account> accountLiveData; private final MutableLiveData<String> errorLiveData; // コンストラクタ public AccountViewModel() { this.retrofit = new Retrofit.Builder() .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/") .addConverterFactory(JacksonConverterFactory.create()) .build(); this.accountsRest = retrofit.create(AccountsRest.class); this.nameLiveData = new MutableLiveData<>(); this.pwLiveData = new MutableLiveData<>(); this.accountPostsLiveData = new MutableLiveData<>(); this.pwErrorLiveData = new MutableLiveData<>(); this.accountLiveData = new MutableLiveData<>(); this.errorLiveData = new MutableLiveData<>(); } // ライブデータの取得(ゲッター) public MutableLiveData<String> getNameLiveData() { return nameLiveData; } public MutableLiveData<String> getPwLiveData() { return pwLiveData; } public MutableLiveData<Collection<Post>> getAccountPostsLiveData() { return accountPostsLiveData; } public MutableLiveData<String> getPwErrorLiveData() { return pwErrorLiveData; } public MutableLiveData<Account> getAccountLiveData() { return accountLiveData; } public MutableLiveData<String> getErrorLiveData() { return errorLiveData; } // 対象のアカウント情報の削除 public void deleteAccount(String uid, String token) { Call<Void> call = accountsRest.deleteAccount(uid, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("DeleteAccount Successful"); } else { System.out.println("DeleteAccount ResponseError"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("DeleteAccount NetworkError" + t); } }); } // 対象のアカウントパスワードの変更 public void changePw(String uid, String oldPw, String newPw, String token) { Call<Void> call = accountsRest.changePw(uid, oldPw, newPw, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { pwLiveData.setValue(newPw); System.out.println(response.code()); System.out.println("Success ChangePW"); } else { if (response.code() == 401){ pwErrorLiveData.setValue("error"); System.out.println("miss ChangePW"); } } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("ChangePw NetworkError" + t); } }); } // 対象のアカウント名の変更 public void changeName(String uid, String name, String token) { Call<Void> call = accountsRest.changeName(uid, name, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { nameLiveData.setValue(name); System.out.println("Success ChangeName:" + name); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("ChangeName NetWorkError" + t); } }); } // 対象のアカウントがした投稿の全取得 public void getAccountPosts(String uid) { Call<Collection<PostJson>> call = accountsRest.getAccountPosts(uid); call.enqueue(new Callback<Collection<PostJson>>() { @Override public void onResponse(Call<Collection<PostJson>> call, Response<Collection<PostJson>> response) { if (response.isSuccessful()) { Collection<PostJson> postJson = response.body(); setAccountPostLiveDataFromJson(postJson); System.out.println("Success" + accountPostsLiveData.getValue().toString()); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Collection<PostJson>> call, Throwable t) { System.out.println("correspondence error" + t); } }); } //PostJsonからPostを作成し,対象のライブデータに設定する private void setAccountPostLiveDataFromJson(Collection<PostJson> postJson) { ArrayList<Post> posts = new ArrayList<>(); for(PostJson pj: postJson) { Post post = new Post(pj); posts.add(post); } accountPostsLiveData.setValue(posts); } //idからアカウント情報を取得するメソッド public void fetchAccount(String id) { Call<AccountJson> call = accountsRest.getAccount(id); call.enqueue(new Callback<AccountJson>() { @Override public void onResponse(Call<AccountJson> call, Response<AccountJson> response) { if (response.isSuccessful()) { if(response.body() == null){ accountLiveData.setValue(null); System.out.println("response error"); }else { Account ac = new Account(response.body()); accountLiveData.setValue(ac); System.out.println("success"); } } else { accountLiveData.setValue(null); System.out.println("response error"); } } @Override public void onFailure(Call<AccountJson> call, Throwable t) { System.out.println("network error" + t); } }); } //同期的にidからアカウント情報を取得するメソッド public Account sample(String id) { try { Response<AccountJson> response = accountsRest.getAccount(id).execute(); if (response.isSuccessful()) { Account account = new Account(response.body()); return account; } else { Log.d("message", "error" + response.code()); } } catch (IOException e) { Log.i("message", "error" + e.getMessage()); } return null; } // 対象のアカウントがした投稿の削除 public void deleteAccountPost(String uid, String pid, String token) { Call<Void> call = accountsRest.deletePost(uid, pid, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { deleteAccountPostLiveData(pid); System.out.println("successful"); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("correspondence error" + t); } }); } private void deleteAccountPostLiveData(String id) { ArrayList<Post> preData = new ArrayList<>(); for (Post post: accountPostsLiveData.getValue()) { if (post.getPid().equals(id)) { continue; } preData.add(post); } accountPostsLiveData.setValue(preData); } private String parseStatusCode(Integer stats) { switch (stats) { case 404: return ErrorType.ResponseNoyFound.getText(); case 401: return ErrorType.InvalidToken.getText(); case 400: return ErrorType.ResponseError.getText(); default: return ErrorType.UnknownError.getText(); } } }