Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / viewmodels / AccountViewModel.java
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<String> nameLiveData;
    private final MutableLiveData<String> pwLiveData;
    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.nameLiveData = new MutableLiveData<>();
        this.pwLiveData = new MutableLiveData<>();
        this.accountPostsLiveData = new MutableLiveData<>();
    }

    // ライブデータの取得(ゲッター)
    public MutableLiveData<String> getNameLiveData() {
        return nameLiveData;
    }
    public MutableLiveData<String> getPwLiveData() {
        return pwLiveData;
    }
    public MutableLiveData<Collection<Post>> getAccountPostsLiveData() { return accountPostsLiveData; }

    // 対象のアカウント情報の削除
    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("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("Success ChangePW");
                } else {
                    System.out.println("response error");
                }
            }
            @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);
    }

    // 対象のアカウントがした投稿の削除
    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" + t);
            }
        });
    }
}