package com.example.nemophila.viewmodels; import com.example.nemophila.entities.Account; import com.example.nemophila.entities.AccountJson; 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.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<Account> accountLiveData; private final MutableLiveData<Collection<Post>> accountPostsLiveData; // コンストラクタ 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.accountLiveData = new MutableLiveData<>(); this.accountPostsLiveData = new MutableLiveData<>(); } // ライブデータの取得(ゲッター) public MutableLiveData<Account> getAccountLiveData() { return accountLiveData; } public MutableLiveData<Collection<Post>> getAccountPostsLiveData() { return accountPostsLiveData; } //AccountJsonからAccountを作成し,対象のライブデータに設定する private void setAccountLiveDataFromJson(AccountJson accountJson) { Account account = new Account(accountJson); accountLiveData.setValue(account); } //PostJsonからPostを作成し,対象のライブデータに設定する private void setAccountPostLiveDataFromJson(Collection<PostJson> postJson) { ArrayList<Post> posts = new ArrayList<>(); for(PostJson i: postJson) { Post post = new Post(i); posts.add(post); } accountPostsLiveData.setValue(posts); } // 対象のアカウント情報の取得 public void getAccount(String uid) { Call<AccountJson> call = accountsRest.getAccount(uid); call.enqueue(new Callback<AccountJson>() { @Override public void onResponse(Call<AccountJson> call, Response<AccountJson> response) { if (response.isSuccessful()) { AccountJson accountJson = response.body(); setAccountLiveDataFromJson(accountJson); } else { System.out.println("response error"); } } @Override public void onFailure(Call<AccountJson> call, Throwable t) { System.out.println("correspondence error"); } }); } // 対象のアカウント情報の削除 public void deleteAccount(String uid) { Call<Void> call = accountsRest.deleteAccount(uid); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("successful"); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("correspondence error"); } }); } // 対象のアカウントパスワードの変更 public void changePw(String uid, String oldPw, String newPw, String token) { Call<AccountJson> call = accountsRest.changePw(uid, oldPw, newPw, token); call.enqueue(new Callback<AccountJson>() { @Override public void onResponse(Call<AccountJson> call, Response<AccountJson> response) { if (response.isSuccessful()) { AccountJson accountJson = response.body(); setAccountLiveDataFromJson(accountJson); } else { System.out.println("response error"); } } @Override public void onFailure(Call<AccountJson> call, Throwable t) { System.out.println("correspondence error"); } }); } // 対象のアカウント名の変更 public void changeName(String uid, String name, String token) { Call<AccountJson> call = accountsRest.changeName(uid, name, token); call.enqueue(new Callback<AccountJson>() { @Override public void onResponse(Call<AccountJson> call, Response<AccountJson> response) { if (response.isSuccessful()) { AccountJson accountJson = response.body(); setAccountLiveDataFromJson(accountJson); } else { System.out.println("response error"); } } @Override public void onFailure(Call<AccountJson> call, Throwable t) { System.out.println("correspondence error"); } }); } // 対象のアカウントがした投稿の全取得 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); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Collection<PostJson>> call, Throwable t) { System.out.println("correspondence error"); } }); } // 対象のアカウントがした投稿の削除 public void deleteAccountPost(String sid, String uid, String pid, String token) { Call<Void> call = accountsRest.deletePost(sid, uid, pid, token); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { if (response.isSuccessful()) { System.out.println("successful"); } else { System.out.println("response error"); } } @Override public void onFailure(Call<Void> call, Throwable t) { System.out.println("correspondence error"); } }); } }