diff --git a/app/src/main/java/com/example/nemophila/PostActivity.java b/app/src/main/java/com/example/nemophila/PostActivity.java index 36f7a4b..69db41e 100644 --- a/app/src/main/java/com/example/nemophila/PostActivity.java +++ b/app/src/main/java/com/example/nemophila/PostActivity.java @@ -2,38 +2,33 @@ import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; -import androidx.activity.result.contract.ActivityResultContract; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; -import android.app.Activity; import android.content.Intent; -import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; -import android.provider.MediaStore; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; +import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.Spinner; import android.widget.TextView; import com.example.nemophila.entities.Account; -import com.example.nemophila.entities.Post; import com.example.nemophila.entities.Shop; -import com.example.nemophila.resources.ShopsRest; import com.example.nemophila.viewmodels.PostsViewModel; import com.example.nemophila.viewmodels.ShopsViewModel; import com.google.android.material.snackbar.Snackbar; -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import retrofit2.Retrofit; @@ -48,24 +43,50 @@ private String comment; private String token; private String sid; - private String img1 = null; - private String img2 = null; - private String img3 = null; + private String[] img; private String shopName; + private ImageButton[] deleteButtons; + private ImageView[] postImages; + private List uriLists; private PostsViewModel postsViewModel; private ShopsViewModel shopsViewModel; - private Account account; - private Retrofit retrofit; private Nemophila nemophila; - private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.GetContent(), - new ActivityResultCallback() { - @Override - public void onActivityResult(Uri result) { + //画像挿入 + private ActivityResultLauncher launcher = registerForActivityResult(new ActivityResultContracts.OpenMultipleDocuments(), new ActivityResultCallback>() { + @Override + public void onActivityResult(List uriList) { + uriLists = uriList; - } - }); + postImages[0] = findViewById(R.id.imageView1); + postImages[1] = findViewById(R.id.imageView2); + postImages[2] = findViewById(R.id.imageView3); + + System.out.println(uriLists.get(0)); + +// for (int i = 0; i () { @Override public void onChanged(Shop shop) { System.out.println("店舗新規作成完了"); - postsViewModel.createPost(uid, token, shop.getSid(), rate, genre, comment, img1, img2, img3); + postsViewModel.createPost(uid, token, shop.getSid(), rate, genre, comment, img[0], img[1], img[2]); System.out.println("lat" + nemophila.getCurrentLatitude() + "\nlong" + nemophila.getCurrentLongitude()); @@ -220,12 +245,70 @@ selectPictureButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // 画像選択(3枚) - launcher.launch("image/*"); + launcher.launch(new String[]{"image/*"}); } }); +// 写真の削除ボタン + deleteButtons[0]= (ImageButton) findViewById(R.id.deleteButton1); + deleteButtons[1]= (ImageButton) findViewById(R.id.deleteButton2); + deleteButtons[2]= (ImageButton) findViewById(R.id.deleteButton3); + + deleteButtons[0].setVisibility(View.INVISIBLE); + deleteButtons[1].setVisibility(View.INVISIBLE); + deleteButtons[2].setVisibility(View.INVISIBLE); + + deleteButtons[0].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + postImages[0].setImageURI(null); + uriLists.set(0, null); +// uriLists.remove(1); + System.out.println("削除ボタン1"); + for (int i = 0; i < postImages.length-1; i++){ + postImages[i].setImageURI(uriLists.get(i+1)); +// postImages[i].setImageURI(uriList.get(i)); + postImages[i+1].setImageURI(null); +// uriLists.remove(i); + uriLists.set(i+1, null); +// postImages[i+1] = null; + System.out.println(uriLists.get(i)); + if (uriLists.get(i) == null){ + deleteButtons[i].setVisibility(View.INVISIBLE); + } + } + } + }); + + deleteButtons[1].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + postImages[1].setImageURI(null); + uriLists.set(1, null); +// postImages[1] = null; + System.out.println("削除ボタン2"); + for (int i = 1; i < postImages.length-1; i++){ + postImages[i].setImageURI(uriLists.get(i+1)); + postImages[i+1].setImageURI(null); +// postImages[i+1] = null; + if (uriLists.get(i)==null){ + deleteButtons[i].setVisibility(View.INVISIBLE); + } + } + } + }); + + deleteButtons[2].setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + postImages[2].setImageURI(null); + + System.out.println("削除ボタン3"); + deleteButtons[2].setVisibility(View.INVISIBLE); + } + }); //投稿ボタンの処理 Button postButton = (Button)findViewById(R.id.postButton); @@ -246,7 +329,7 @@ if (shop == null) { shopsViewModel.createShop(shopName, nemophila.getCurrentLongitude(), nemophila.getCurrentLatitude()); } else { - postsViewModel.createPost(uid, token, sid, rate, genre, comment, img1, img2, img3); + postsViewModel.createPost(uid, token, sid, rate, genre, comment, img[0], img[1], img[2]); System.out.println("既存店舗に投稿"); System.out.println("lat" + nemophila.getCurrentLatitude() + "\nlong" + nemophila.getCurrentLongitude());