diff --git a/app/src/main/java/org/ntlab/acanthus_client/entities/InvitesJson.java b/app/src/main/java/org/ntlab/acanthus_client/entities/InvitesJson.java new file mode 100644 index 0000000..6e8db8f --- /dev/null +++ b/app/src/main/java/org/ntlab/acanthus_client/entities/InvitesJson.java @@ -0,0 +1,21 @@ +package org.ntlab.acanthus_client.entities; + +import java.util.Collection; + +public class InvitesJson { + private Collection invites; + + public InvitesJson(Collection accounts) { + for(Account inviter : accounts){ + this.invites.add(inviter.getUid()); + } + } + + public Collection getInvitesJson() { + return invites; + } + + public void setInvitesJson(Integer uid){ + this.invites.add(uid); + } +} diff --git a/app/src/main/java/org/ntlab/acanthus_client/resources/gallery/InvitesRest.java b/app/src/main/java/org/ntlab/acanthus_client/resources/gallery/InvitesRest.java index fc9ced3..8aa03ae 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/resources/gallery/InvitesRest.java +++ b/app/src/main/java/org/ntlab/acanthus_client/resources/gallery/InvitesRest.java @@ -1,6 +1,7 @@ package org.ntlab.acanthus_client.resources.gallery; import org.ntlab.acanthus_client.entities.Account; +import org.ntlab.acanthus_client.entities.InvitesJson; import java.util.Collection; @@ -18,7 +19,7 @@ //----------------------------------------------------------------- @GET("gallery/{aid}/invites") - Call> isGalleryInvites( + Call isGalleryInvites( @Path("aid") Integer aid, @Query("invited") String invitedUid, @Query("invitedUserToken") String invitedUserToken, diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteActivity.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteActivity.java index 2ad1829..c01a1c9 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteActivity.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteActivity.java @@ -8,13 +8,34 @@ import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import org.ntlab.acanthus_client.Acanthus; import org.ntlab.acanthus_client.R; +import org.ntlab.acanthus_client.entities.Account; +import org.ntlab.acanthus_client.entities.AccountJson; +import org.ntlab.acanthus_client.entities.EditorJson; +import org.ntlab.acanthus_client.entities.FollowerUidJson; +import org.ntlab.acanthus_client.entities.InvitesJson; +import org.ntlab.acanthus_client.entities.WorkJson; +import org.ntlab.acanthus_client.views.main_menu_ui.edit.EditViewModel; -public class InviteActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { +import java.util.ArrayList; +import java.util.Collection; + +public class InviteActivity extends AppCompatActivity { private InviteViewModel inviteViewModel; + private Integer aid; + private Integer uid; + private Integer invitedUid; + private String ownerToken; + private Acanthus acanthus; + private Collection followersUidList = new ArrayList<>(); + private ArrayList collectionAccountUid = new ArrayList<>(); + private Collection collectionAccountName = new ArrayList<>(); + private EditorJson editorJson; + private InvitesJson invitedUidList; private static final String[] scenes = { "UserName", }; @@ -27,32 +48,94 @@ setContentView((R.layout.activity_invite)); // ListViewのインスタンスを生成 ListView listView = findViewById(R.id.list_view); + Acanthus acanthus = (Acanthus) getApplication(); + inviteViewModel = new ViewModelProvider(this).get(InviteViewModel.class); + aid = acanthus.getAid(); + uid = 1; + ownerToken = acanthus.getPreferenceToken(); - // BaseAdapter を継承したadapterのインスタンスを生成 - // レイアウトファイル list.xml を activity_main.xml に - // inflate するためにadapterに引数として渡す - BaseAdapter adapter = new ListViewAdapter(this.getApplicationContext(), R.layout.list, scenes); - // ListViewにadapterをセット - listView.setAdapter(adapter); + //フォロワー情報を取得する + inviteViewModel.startFollowersUidJson(uid, aid, ownerToken); + inviteViewModel.getFollowerUidJson().observe(this, new Observer() { + @Override + public void onChanged(FollowerUidJson followerUidJson) { + followersUidList = (ArrayList) inviteViewModel.getFollowerUidJson().getValue().getFollowersUid(); + inviteViewModel.startGalleryInvites(aid, ownerToken); + } + }); - // クリックリスナーをセット - listView.setOnItemClickListener(this); + //すべての招待情報を取得する + inviteViewModel.getInvitesJson().observe(this, new Observer() { + @Override + public void onChanged(InvitesJson collectionAccount) { + invitedUidList = inviteViewModel.getInvitesJson().getValue(); + + inviteViewModel.startGalleryEditors(aid); + } + }); + + inviteViewModel.getEditorJson().observe(this, new Observer() { + @Override + public void onChanged(EditorJson collectionAccount) { + editorJson = inviteViewModel.getEditorJson().getValue(); + + inviteViewModel.startAccountJson(); + } + }); + + //すべてのユーザIDと名前を取得 + inviteViewModel.getCollectionAccountJson().observe(this, new Observer>() { + @Override + public void onChanged(Collection collectionAccountJson) { + collectionAccountJson = (Collection) inviteViewModel.getCollectionAccountJson().getValue(); + if(collectionAccountJson != null){ + if(editorJson != null){ + for(Integer editorUid : editorJson.getUidList()){ + followersUidList.remove(editorUid); + } + } + + if(invitedUidList != null){ + for(Integer invitedUid : invitedUidList.getInvitesJson()){ + followersUidList.remove(invitedUid); + } + } + + + for(AccountJson accountJson : collectionAccountJson){ + if(followersUidList.contains(accountJson.getUid())) { + collectionAccountUid.add(accountJson.getUid()); + collectionAccountName.add(accountJson.getName()); + } + } + // BaseAdapter を継承したadapterのインスタンスを生成 + // レイアウトファイル list.xml を activity_main.xml に + // inflate するためにadapterに引数として渡す + BaseAdapter adapter = new ListViewAdapter(getApplication(), R.layout.list, (ArrayList) collectionAccountName); + + // ListViewにadapterをセット + listView.setAdapter(adapter); + + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapterView, View view, int position, long id) { + invitedUid = collectionAccountUid.get(position); + inviteViewModel.addInvite(aid, uid.toString(), invitedUid.toString(), ownerToken); + } + }); + } + } + }); + + inviteViewModel.getMessage().observe(this, new Observer() { + @Override + public void onChanged(String message) { + finish(); + } + }); } - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - Intent intent = new Intent( - this.getApplicationContext(), PaintActivity.class); - - // clickされたpositionのtextとphotoのID - String selectedText = scenes[position]; - // インテントにセット - intent.putExtra("Text", selectedText); - - // SubActivityへ遷移 - startActivity(intent); - } //----------------------------------------------------------------- // init @@ -60,4 +143,4 @@ Acanthus acanthus = (Acanthus) getApplication(); inviteViewModel = new ViewModelProvider(this).get(InviteViewModel.class); } -} +} \ No newline at end of file diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteViewModel.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteViewModel.java index 7de98da..25b0742 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteViewModel.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/InviteViewModel.java @@ -1,8 +1,191 @@ package org.ntlab.acanthus_client.views.paint; +import android.util.Log; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; +import org.ntlab.acanthus_client.Acanthus; +import org.ntlab.acanthus_client.entities.Account; +import org.ntlab.acanthus_client.entities.AccountJson; +import org.ntlab.acanthus_client.entities.AidJson; +import org.ntlab.acanthus_client.entities.Animation; +import org.ntlab.acanthus_client.entities.AnimationJson; +import org.ntlab.acanthus_client.entities.EditorJson; +import org.ntlab.acanthus_client.entities.FollowerUidJson; +import org.ntlab.acanthus_client.entities.InvitesJson; +import org.ntlab.acanthus_client.resources.accounts.AccountsRest; +import org.ntlab.acanthus_client.resources.accounts.FollowersRest; +import org.ntlab.acanthus_client.resources.gallery.EditorsRest; +import org.ntlab.acanthus_client.resources.gallery.GalleryRest; +import org.ntlab.acanthus_client.resources.gallery.InvitesRest; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; + public class InviteViewModel extends ViewModel { + private MutableLiveData followersUidJsonMutableLiveData; + private MutableLiveData> collectionAccountJsonMutableLiveData; + private MutableLiveData collectionInvitesJsonMutableLiveData; + private MutableLiveData messageMutableLiveData; + private MutableLiveData editorJsonMutableLiveData; -} + + public InviteViewModel() { + this.followersUidJsonMutableLiveData = new MutableLiveData<>(); + this.collectionAccountJsonMutableLiveData = new MutableLiveData<>(); + this.collectionInvitesJsonMutableLiveData = new MutableLiveData<>(); + this.messageMutableLiveData = new MutableLiveData<>(); + this.editorJsonMutableLiveData = new MutableLiveData<>(); + } + + public LiveData getFollowerUidJson() { + return this.followersUidJsonMutableLiveData; + } + + public LiveData> getCollectionAccountJson() { + return this.collectionAccountJsonMutableLiveData; + } + + public LiveData getInvitesJson() { + return this.collectionInvitesJsonMutableLiveData; + } + + public LiveData getMessage() { + return this.messageMutableLiveData; + } + + public LiveData getEditorJson() { + return this.editorJsonMutableLiveData; + } + + public void startFollowersUidJson(Integer uid, Integer aid, String ownerToken){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final FollowersRest followersRest = retrofit.create(FollowersRest.class); + Call call = followersRest.getFollowers(uid); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + if (response.body() != null) { + followersUidJsonMutableLiveData.setValue(response.body()); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + startGalleryInvites(aid, ownerToken); + } + }); + } + + public void startGalleryInvites(Integer aid, String ownerToken){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final InvitesRest invitesRest = retrofit.create(InvitesRest.class); + Call call = invitesRest.isGalleryInvites(aid, null, null, ownerToken); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + if (response.body() != null) { + collectionInvitesJsonMutableLiveData.setValue(response.body()); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + startGalleryEditors(aid); + } + }); + } + + public void startGalleryEditors(Integer aid){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final EditorsRest invitesRest = retrofit.create(EditorsRest.class); + Call call = invitesRest.getEditors(aid); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + if (response.body() != null) { + editorJsonMutableLiveData.setValue(response.body()); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + startAccountJson(); + } + }); + } + + public void startAccountJson(){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final AccountsRest accountsRest = retrofit.create(AccountsRest.class); + Call> call = accountsRest.getAccounts(null); + call.enqueue(new Callback>() { + @Override + public void onResponse(Call> call, Response> response) { + if(response.isSuccessful()){ + if (response.body() != null) { + collectionAccountJsonMutableLiveData.setValue(response.body()); + } + } + } + + @Override + public void onFailure(Call> call, Throwable t) { + } + }); + } + + + + public void addInvite(Integer aid, String ownerUid, String invitedUid, String ownerToken){ + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/acanthus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + final InvitesRest invitesRest = retrofit.create(InvitesRest.class); + Call call = invitesRest.addInvite(aid, ownerUid, invitedUid, ownerToken); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if(response.isSuccessful()){ + if (response.body() != null) { + messageMutableLiveData.setValue(response.body()); + } + } + } + + @Override + public void onFailure(Call call, Throwable t) { + } + }); + } +} \ No newline at end of file diff --git a/app/src/main/java/org/ntlab/acanthus_client/views/paint/ListViewAdapter.java b/app/src/main/java/org/ntlab/acanthus_client/views/paint/ListViewAdapter.java index c4c0adf..462e862 100644 --- a/app/src/main/java/org/ntlab/acanthus_client/views/paint/ListViewAdapter.java +++ b/app/src/main/java/org/ntlab/acanthus_client/views/paint/ListViewAdapter.java @@ -9,6 +9,9 @@ import org.ntlab.acanthus_client.R; +import java.util.ArrayList; +import java.util.Collection; + public class ListViewAdapter extends BaseAdapter { static class ViewHolder { @@ -17,16 +20,16 @@ private LayoutInflater inflater; private int itemLayoutId; - private String[] titles; + private ArrayList titles = new ArrayList<>(); private int[] ids; ListViewAdapter(Context context, int itemLayoutId, - String[] scenes) { + ArrayList collectionAccountName) { super(); this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.itemLayoutId = itemLayoutId; - this.titles = scenes; + this.titles = collectionAccountName; } @Override @@ -47,7 +50,7 @@ } // 現在の position にあるファイル名リストを holder の textView にセット - holder.textView.setText(titles[position]); + holder.textView.setText(titles.get(position)); return convertView; } @@ -55,7 +58,7 @@ @Override public int getCount() { // texts 配列の要素数 - return titles.length; + return titles.size(); } @Override @@ -67,4 +70,4 @@ public long getItemId(int position) { return 0; } -} +} \ No newline at end of file