package com.example.nemophila; import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import android.content.Intent; import android.net.Uri; import android.os.Bundle; 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.Shop; import com.example.nemophila.viewmodels.PostsViewModel; import com.example.nemophila.viewmodels.ShopsViewModel; import com.google.android.material.snackbar.Snackbar; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.List; import retrofit2.Retrofit; public class PostActivity extends AppCompatActivity { private String pid; private String uid; private String name; private String genre; private int rate; private String comment; private String token; private String sid; private String[] img; private String shopName; private ImageButton[] deleteButtons; private ImageView[] postImages; private List<Uri> uriLists; private PostsViewModel postsViewModel; private ShopsViewModel shopsViewModel; private Nemophila nemophila; //画像挿入 private ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.OpenMultipleDocuments(), new ActivityResultCallback<List<Uri>>() { @Override public void onActivityResult(List<Uri> 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 <postImages.length; i++){ // if (postImages[i] == null){ // deleteButtons[i].setVisibility(View.INVISIBLE); // if (i != postImages.length) { // postImages[i] = postImages[i + 1]; // } // } //// if (postImages[postImages.length] == null){ //// //// } // } for(int i = 0; i < 3 && i < uriLists.size(); i++){ postImages[i].setImageURI(uriLists.get(i)); deleteButtons[i].setVisibility(View.VISIBLE); String s = new String(Base64.getEncoder().encode(uriLists.get(i).toString().getBytes())); img[i] = s; } } }); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post); Shop shop; nemophila = (Nemophila) this.getApplication(); postsViewModel = new ViewModelProvider(this).get(PostsViewModel.class); shopsViewModel = new ViewModelProvider(this).get(ShopsViewModel.class); img = new String[3]; deleteButtons = new ImageButton[3]; postImages = new ImageView[3]; //店舗新規作成のオブザーバー shopsViewModel.getCurrentLiveData().observe(this, new Observer<Shop>() { @Override public void onChanged(Shop shop) { System.out.println("店舗新規作成完了"); postsViewModel.createPost(uid, token, shop.getSid(), rate, genre, comment, img[0], img[1], img[2]); System.out.println("lat" + nemophila.getCurrentLatitude() + "\nlong" + nemophila.getCurrentLongitude()); //投稿時にリセット nemophila.setDummyShop(null); nemophila.setCurrentShop(null); Intent intent = new Intent(getApplication(), MapsActivity.class); startActivity(intent); } }); uid = nemophila.getUid(); token = nemophila.getToken(); shop = nemophila.getCurrentShop(); if (shop == null){ Shop dummyShop = nemophila.getDummyShop(); sid = dummyShop.getSid(); shopName = dummyShop.getName(); }else{ sid = shop.getSid(); shopName = shop.getName(); } //店名を表示 TextView textView = findViewById(R.id.shopNamePoatAc); textView.setText(shopName); //左上のボタンでキャンセル(ShopActivityに戻る) //確認用ダイアログを入れたい Button returnButton = (Button)findViewById(R.id.postAcReturnButton); returnButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ //キャンセル時にリセット nemophila.setDummyShop(null); nemophila.setCurrentShop(null); //画面遷移 Intent intent = new Intent(getApplication(), MapsActivity.class); startActivity(intent); } }); //評価を入力するためのプルダウン ArrayAdapter<String> rateAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); rateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); rateAdapter.add("評価を選択"); rateAdapter.add("1"); rateAdapter.add("2"); rateAdapter.add("3"); rateAdapter.add("4"); rateAdapter.add("5"); Spinner rateSpinner = (Spinner) findViewById(R.id.inputRate); rateSpinner.setAdapter(rateAdapter); //プルダウンが変更されたら実行 rateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { //選択が何もなかった時の動作 @Override public void onNothingSelected(AdapterView<?> adapterView) {} //何か選択された時の動作 @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String gettedRate = (String)rateSpinner.getSelectedItem(); //プルダウンで取得した評価をintに変換してrateに入れる if(gettedRate != "評価を選択"){ rate = Integer.parseInt(gettedRate); //表示用にStringの星に変換 String displayRate = ""; for (int j = 0; j < 5; j++){ if(rate > j ){ displayRate += "★"; }else{ displayRate += "☆"; } } TextView rateView = (TextView) findViewById(R.id.ratePostAc); rateView.setText(displayRate); }else if (gettedRate.equals("評価を選択")){ rate = 0; } // System.out.println(gettedRate); // System.out.println(rate); } }); //製作途中 Button selectPictureButton = (Button)findViewById(R.id.selectpicturebutton); selectPictureButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // 画像選択(3枚) 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); postButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ System.out.println("投稿ボタンのクリック"); EditText editGenre = (EditText) findViewById(R.id.inputGenre); genre = editGenre.getText().toString(); EditText editComment = (EditText) findViewById(R.id.inputComment); comment = editComment.getText().toString(); //評価が未入力の時 if(rate == 0) { System.out.println("評価未設定クリック"); Snackbar.make(v, "評価を入力してください", Snackbar.LENGTH_SHORT).show(); // Intent intent = new Intent(getApplication(), ShopActivity.class); // startActivity(intent); } else { //評価入力済の時 if (shop == null) { shopsViewModel.createShop(shopName, nemophila.getCurrentLongitude(), nemophila.getCurrentLatitude()); } else { 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()); //投稿時にリセット nemophila.setDummyShop(null); nemophila.setCurrentShop(null); Intent intent = new Intent(getApplication(), MapsActivity.class); startActivity(intent); } } } }); } }