Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / CreateGroupActivity.java
k-morimoto on 17 Oct 2019 5 KB baseUrlをcosmos/rest/に変更
  1. package com.example.cosmosclient.views;
  2.  
  3. import android.content.Intent;
  4. import android.os.Handler;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. import com.example.cosmosclient.R;
  15. import com.example.cosmosclient.app.Cosmos;
  16. import com.example.cosmosclient.entities.CreateGroupResponse;
  17. import com.example.cosmosclient.entities.Group;
  18. import com.example.cosmosclient.resources.GroupsRest;
  19.  
  20. import retrofit2.Call;
  21. import retrofit2.Callback;
  22. import retrofit2.Response;
  23. import retrofit2.Retrofit;
  24. import retrofit2.converter.jackson.JacksonConverterFactory;
  25.  
  26. public class CreateGroupActivity extends AppCompatActivity {
  27. private Button createGroupButton;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_make_group);
  33.  
  34. //各種IDを取得
  35. final EditText groupNameText = findViewById(R.id.groupNameText);
  36. createGroupButton = findViewById(R.id.createGroupButton);
  37. final String uId,token;
  38.  
  39. //ボタン有効化&監視
  40. createGroupButton.setEnabled(false);
  41. createGroupButton.setBackgroundColor(0xaa808080);
  42. groupNameText.addTextChangedListener(new CreateGroupActivity.GenericTextWatcher(groupNameText));
  43.  
  44. //グループ作成に必要な情報の取得
  45. Cosmos app = (Cosmos) getApplication();
  46. uId = app.getuId();
  47. token = app.getToken();
  48.  
  49. //retrofitの処理
  50. final Retrofit retrofit = new Retrofit.Builder()
  51. .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/cosmos/rest/")
  52. .addConverterFactory(JacksonConverterFactory.create())
  53. .build();
  54. //interfaceから実装を取得
  55. final GroupsRest createGroupService = retrofit.create(GroupsRest.class);
  56.  
  57. //グループ作成ボタン処理
  58. createGroupButton.setOnClickListener(new View.OnClickListener(){
  59. @Override
  60. public void onClick(View view){
  61. //ボタン連打防止
  62. createGroupButton.setEnabled(false);
  63. createGroupButton.setBackgroundColor(0xaa808080);
  64. new Handler().postDelayed(new Runnable() {
  65. public void run() {
  66. createGroupButton.setEnabled(true);
  67. createGroupButton.setBackgroundColor(0xaa009688);
  68. }
  69. }, 1000L);
  70.  
  71. Call<CreateGroupResponse> createGroup = createGroupService.createGroup(groupNameText.getText().toString(), uId, token);
  72.  
  73. createGroup.enqueue(new Callback<CreateGroupResponse>() {
  74. @Override
  75. public void onResponse(Call<CreateGroupResponse> call, Response<CreateGroupResponse> response) {
  76. if (response.isSuccessful()) {
  77. //成功時
  78. CreateGroupResponse result = response.body();
  79. Group g = new Group(result.gId,result.uri,groupNameText.getText().toString(),uId);
  80.  
  81. //app/CosmosにgIdを保存
  82. Cosmos app = (Cosmos)getApplication();
  83. app.setCurrentGroup(g);
  84.  
  85. Intent intent = new Intent(getApplication(), RequestListActivity.class);
  86. startActivity(intent);
  87. Toast.makeText(CreateGroupActivity.this,
  88. "グループを作成しました", Toast.LENGTH_SHORT).show();
  89. finish();
  90. } else {
  91. //onFailureでキャッチできない用のエラー
  92. System.out.println("");
  93. Toast.makeText(CreateGroupActivity.this,
  94. "通信エラー",Toast.LENGTH_SHORT).show();
  95. }
  96. }
  97.  
  98. @Override
  99. public void onFailure(Call<CreateGroupResponse> call, Throwable t) {
  100. //失敗時
  101. t.printStackTrace();
  102. Toast.makeText(CreateGroupActivity.this,
  103. "グループ作成失敗",Toast.LENGTH_SHORT).show();
  104. }
  105. });
  106. }
  107. });
  108. }
  109. //テキスト欄監視
  110. private class GenericTextWatcher implements TextWatcher {
  111. private View view;
  112.  
  113. private GenericTextWatcher(View view){
  114. this.view = view;
  115. }
  116.  
  117. @Override
  118. public void beforeTextChanged(CharSequence s, int start, int count,int after){/*記述不要*/};
  119. @Override
  120. public void onTextChanged(CharSequence s, int start, int before, int count){/*記述不要*/};
  121.  
  122. @Override
  123. public void afterTextChanged(Editable s){
  124. //ボタン有効&無効
  125. if(s.length()>0){
  126. createGroupButton.setEnabled(true);
  127. createGroupButton.setBackgroundColor(0xaa009688);
  128. }else{
  129. createGroupButton.setEnabled(false);
  130. createGroupButton.setBackgroundColor(0xaa808080);
  131. }
  132. }
  133. }
  134. }