package com.example.nemophila; 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.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.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 img1 = null; private String img2 = null; private String img3 = null; private String shopName; private PostsViewModel postsViewModel; private ShopsViewModel shopsViewModel; private Account account; private Retrofit retrofit; private Nemophila nemophila; private ActivityResultLauncher<String> launcher = registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri result) { } }); @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); //店舗新規作成のオブザーバー 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, img1, img2, img3); 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); } }); ArrayAdapter<String> genreAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); genreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); genreAdapter.add("ジャンルを選択"); genreAdapter.add("中華"); genreAdapter.add("和食"); genreAdapter.add("イタリアン"); genreAdapter.add("フレンチ"); genreAdapter.add("カフェ"); genreAdapter.add("バー"); genreAdapter.add("居酒屋"); genreAdapter.add("ラーメン"); genreAdapter.add("うどん・そば"); genreAdapter.add("その他 海外料理"); genreAdapter.add("その他 和食"); genreAdapter.add("その他"); Spinner genreSpinner = (Spinner) findViewById(R.id.inputGenre); genreSpinner.setAdapter(genreAdapter); genreSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { //ジャンルプルダウンで選択が何もなかった時の動作 @Override public void onNothingSelected(AdapterView<?> adapterView) {} //ジャンルプルダウンで何か選択された時の動作 @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { genre = (String) genreSpinner.getSelectedItem(); if (genre.equals("ジャンルを選択")){ genre = null; } } }); //製作途中 Button selectPictureButton = (Button)findViewById(R.id.selectpicturebutton); selectPictureButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { // 画像選択(3枚) launcher.launch("image/*"); } }); //投稿ボタンの処理 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 && genre != null) { //評価とジャンル入力済の時 if (shop == null) { shopsViewModel.createShop(shopName, nemophila.getCurrentLongitude(), nemophila.getCurrentLatitude()); } else { postsViewModel.createPost(uid, token, sid, rate, genre, comment, img1, img2, img3); 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); } }else if(rate == 0) { //評価未入力 System.out.println("評価未設定クリック"); Snackbar.make(v, "評価を選択してください", Snackbar.LENGTH_SHORT).show(); // Intent intent = new Intent(getApplication(), ShopActivity.class); // startActivity(intent); }else if (genre == null){ System.out.println("ジャンル未設定クリック"); Snackbar.make(v, "ジャンルを選択してください", Snackbar.LENGTH_SHORT).show(); } } }); } }