Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / PostActivity.java
k-fujii on 12 Oct 2023 12 KB Merge branch 'master' into rate
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.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.RatingBar;
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.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
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);

            for(int i = 0; i < 3 && i < uriLists.size(); i++){
                postImages[i].setImageURI(uriLists.get(i));
                deleteButtons[i].setVisibility(View.VISIBLE);

//                System.out.println(uriLists.get(i));

                try {
                    InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(uriLists.get(i));
                    Bitmap bitmap = BitmapFactory.decodeStream(new BufferedInputStream(inputStream));
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    byte[] byteArray = stream.toByteArray();
                    String encoded = Base64.getEncoder().encodeToString(byteArray);
//                    System.out.println(encoded);
//                    System.out.println(encoded.length());
                    img[i] = encoded;

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }
    });

    @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);
            }
        });

        //評価を入力するためのレーティングバー
        RatingBar ratingBar = findViewById(R.id.ratingBar);
        //評価が入力された時の処理
        ratingBar.setOnRatingBarChangeListener((inputRate,gettingrate,fromUser) -> {
            rate = (int)gettingrate;
        });

        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("カレー");
        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(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");
//                if (postImages.length)
                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));
                }
                for (int i = 0; i < postImages.length; 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;
                }
                for (int i = 0; i < postImages.length; i++){
                    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 && genre != null) {
                    //評価とジャンル入力済の時
                    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);
                    }
                }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();
                }
            }

        });
    }
}