Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / PostActivity.java
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
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.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.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;

    @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(), MainActivity.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(), MainActivity.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枚)
//                final int maxNumPhotosAndVideos = 10;
//                Intent intent = new Intent(MediaStore.ACTION_PICK_IMAGES);
//                intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxNumPhotosAndVideos);
//                startActivityForResult(intent, PHOTO_PICKER_MULTI_SELECT_REQUEST_CODE);
            }
        });


        //投稿ボタンの処理
        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, 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(), MainActivity.class);
                        startActivity(intent);
                    }
                }
            }

        });
    }
}