| | package com.example.nemophila; |
---|
| | |
---|
| | import androidx.appcompat.app.AppCompatActivity; |
---|
| | import androidx.lifecycle.ViewModelProvider; |
---|
| | |
---|
| | import android.content.Intent; |
---|
| | import android.os.Bundle; |
---|
| | import android.view.Gravity; |
---|
| | import android.view.View; |
---|
| | import android.widget.Button; |
---|
| | import android.widget.EditText; |
---|
| | import android.widget.Toast; |
---|
| | |
---|
| | import com.example.nemophila.entities.Shop; |
---|
| | import com.example.nemophila.viewmodels.ShopsViewModel; |
---|
| | |
---|
| | import java.io.IOException; |
---|
| | |
---|
| | public class ShopCreateActivity extends AppCompatActivity { |
---|
| | |
---|
| | @Override |
---|
| |
---|
| | setContentView(R.layout.activity_shop_create); |
---|
| | |
---|
| | Nemophila nemophila = (Nemophila) this.getApplication(); |
---|
| | |
---|
| | // ShopsViewModelにアクセス |
---|
| | ShopsViewModel shopsViewModel = new ViewModelProvider(this).get(ShopsViewModel.class); |
---|
| | |
---|
| | // LiveDataへの購読 |
---|
| | shopsViewModel.getCurrentLiveData().observe(this, shop -> { |
---|
| | // ShopActivityでどこの店についての投稿かを管理できるように更新する |
---|
| | nemophila.setCurrentShop(shop); |
---|
| | |
---|
| | // ShopActivityへ画面遷移する |
---|
| | Intent intent = new Intent(getApplication(), ShopActivity.class); |
---|
| | startActivity(intent); |
---|
| | }); |
---|
| | // フィールド |
---|
| | Shop dummyShop = new Shop(); |
---|
| | |
---|
| | // 店の名前が入力されていればsidを発行し、新しくShopを生成 |
---|
| | Button shopCreateButton = findViewById(R.id.buttonShopCreate); |
---|
| | shopCreateButton.setOnClickListener(v -> { |
---|
| | Button transitionButton = findViewById(R.id.buttonTransitionPostActivity); |
---|
| | transitionButton.setOnClickListener(v -> { |
---|
| | EditText nameTextBox = findViewById(R.id.editTextShopCreate); |
---|
| | String name = nameTextBox.getText().toString(); |
---|
| | |
---|
| | // 店の名前を入力されているときのみ処理を行う |
---|
| | if (!name.equals("")) { |
---|
| | shopsViewModel.createShop(name, nemophila.getCurrentLongitude(), nemophila.getCurrentLatitude()); |
---|
| | shopCreateButton.setEnabled(false); |
---|
| | dummyShop.setName(name); |
---|
| | dummyShop.setLongitude(nemophila.getCurrentLongitude()); |
---|
| | dummyShop.setLatitude(nemophila.getCurrentLatitude()); |
---|
| | nemophila.setDummyShop(dummyShop); |
---|
| | |
---|
| | // PostActivityへ画面を遷移する |
---|
| | Intent intent = new Intent(getApplication(), PostActivity.class); |
---|
| | startActivity(intent); |
---|
| | // 店の名前を入力されていないときToastを発行 |
---|
| | } else { |
---|
| | Toast ts = Toast.makeText(ShopCreateActivity.this, "名前を入力してください", Toast.LENGTH_SHORT); |
---|
| | ts.setGravity(Gravity.CENTER, 0, 0); |
---|
| | ts.show(); |
---|
| | } |
---|
| | }); |
---|
| | |
---|
| | Button cancelButton = findViewById(R.id.buttonCancel); |
---|
| | cancelButton.setOnClickListener(v -> { |
---|
| | cancelButton.setEnabled(false); |
---|
| | Intent intent = new Intent(getApplication(), MainActivity.class); |
---|
| | startActivity(intent); |
---|
| | }); |
---|
| | } |
---|
| | } |
---|
| | |