diff --git a/app/src/main/java/com/example/nemophila/PostActivity.java b/app/src/main/java/com/example/nemophila/PostActivity.java index e7aa02b..6dc1b03 100644 --- a/app/src/main/java/com/example/nemophila/PostActivity.java +++ b/app/src/main/java/com/example/nemophila/PostActivity.java @@ -2,46 +2,34 @@ 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.net.URI; -import java.sql.Array; -import java.util.ArrayList; -import java.util.Arrays; +import java.nio.charset.StandardCharsets; import java.util.Base64; -import java.util.Collection; -import java.util.Collections; import java.util.List; -import java.util.stream.Stream; import retrofit2.Retrofit; @@ -55,33 +43,46 @@ 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.OpenMultipleDocuments(), new ActivityResultCallback>() { @Override public void onActivityResult(List uriList) { - ImageView[] images = new ImageView[3]; - images[0] = findViewById(R.id.imageView1); - images[1] = findViewById(R.id.imageView2); - images[2] = findViewById(R.id.imageView3); -// ImageView image1 = findViewById(R.id.imageView1); -// ImageView image2 = findViewById(R.id.imageView2); -// ImageView image3 = findViewById(R.id.imageView3); + uriLists = uriList; - System.out.println(uriList.get(0)); + postImages[0] = findViewById(R.id.imageView1); + postImages[1] = findViewById(R.id.imageView2); + postImages[2] = findViewById(R.id.imageView3); - for(int i = 0; i < 3 && i < uriList.size(); i++){ - images[i].setImageURI(uriList.get(i)); + 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()); @@ -206,8 +211,66 @@ } }); +// 写真の削除ボタン + 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); @@ -234,7 +297,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());