2024-01-17 07:55:44 +00:00
|
|
|
|
package com.example.childguard;
|
|
|
|
|
|
2024-01-17 08:55:04 +00:00
|
|
|
|
import static android.content.ContentValues.TAG;
|
|
|
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
|
|
|
|
|
|
|
|
import android.content.SharedPreferences;
|
2024-01-17 07:55:44 +00:00
|
|
|
|
import android.os.Bundle;
|
2024-01-17 08:55:04 +00:00
|
|
|
|
import android.util.Log;
|
2024-01-17 07:55:44 +00:00
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
2024-01-17 08:55:04 +00:00
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2024-01-19 01:24:05 +00:00
|
|
|
|
import androidx.fragment.app.Fragment;
|
|
|
|
|
|
2024-01-17 08:55:04 +00:00
|
|
|
|
import com.google.firebase.firestore.FirebaseFirestore;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2024-01-17 07:55:44 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A simple {@link Fragment} subclass.
|
|
|
|
|
* Use the {@link SettingFragment#newInstance} factory method to
|
|
|
|
|
* create an instance of this fragment.
|
|
|
|
|
*/
|
|
|
|
|
public class SettingFragment extends Fragment {
|
2024-01-17 08:55:04 +00:00
|
|
|
|
|
|
|
|
|
FirebaseFirestore db;
|
2024-02-01 06:30:39 +00:00
|
|
|
|
|
2024-01-17 07:55:44 +00:00
|
|
|
|
public SettingFragment() {
|
|
|
|
|
// Required empty public constructor
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 07:56:14 +00:00
|
|
|
|
public static SettingFragment newInstance() {
|
2024-01-17 07:55:44 +00:00
|
|
|
|
SettingFragment fragment = new SettingFragment();
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
|
fragment.setArguments(args);
|
|
|
|
|
return fragment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
|
// Inflate the layout for this fragment
|
|
|
|
|
return inflater.inflate(R.layout.fragment_setting, container, false);
|
|
|
|
|
}
|
2024-01-17 08:23:07 +00:00
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState);
|
|
|
|
|
|
2024-01-17 08:55:04 +00:00
|
|
|
|
db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ
|
|
|
|
|
|
2024-02-01 06:30:39 +00:00
|
|
|
|
view.findViewById(R.id.button_bluetooth_setting).setOnClickListener(v -> {
|
2024-01-17 08:55:04 +00:00
|
|
|
|
getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, bluetooth_setupFragment.newInstance("test", "test")).addToBackStack(null).commit();
|
2024-01-17 08:23:07 +00:00
|
|
|
|
});
|
|
|
|
|
|
2024-02-01 06:30:39 +00:00
|
|
|
|
view.findViewById(R.id.button_print_qr).setOnClickListener(v -> {
|
2024-01-17 08:55:04 +00:00
|
|
|
|
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE);
|
|
|
|
|
boolean alreadySaved = sharedPreferences.getBoolean("alreadySaved", false);
|
|
|
|
|
//falseのときにFirebaseへの登録
|
|
|
|
|
if (alreadySaved) {
|
|
|
|
|
Log.d("HomeFragment", "already printed");
|
|
|
|
|
//画面遷移&ID受け渡し
|
|
|
|
|
Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show();
|
2024-01-17 09:58:25 +00:00
|
|
|
|
getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(sharedPreferences.getString("ID", "none"))).commit();
|
2024-01-17 08:55:04 +00:00
|
|
|
|
} else {
|
2024-01-18 15:57:33 +00:00
|
|
|
|
Map<String, Boolean> DEFAULT_ITEM = new HashMap<>();//mapの宣言
|
2024-01-17 08:55:04 +00:00
|
|
|
|
|
|
|
|
|
Log.d("HomeFragment", "onClick is called");
|
|
|
|
|
|
|
|
|
|
//mapに入れる
|
2024-01-18 15:57:33 +00:00
|
|
|
|
DEFAULT_ITEM.put("isInCar", false);
|
|
|
|
|
DEFAULT_ITEM.put("isReported", false);
|
2024-01-17 08:55:04 +00:00
|
|
|
|
//新しいドキュメントにIDを作って追加
|
2024-01-18 15:57:33 +00:00
|
|
|
|
db.collection("status")
|
|
|
|
|
.add(DEFAULT_ITEM)
|
2024-01-17 08:55:04 +00:00
|
|
|
|
.addOnSuccessListener(documentReference -> {
|
|
|
|
|
//成功したら
|
|
|
|
|
//documentReference.getId()でID取得
|
|
|
|
|
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
|
|
|
|
|
SharedPreferences.Editor e = sharedPreferences.edit();
|
|
|
|
|
// キー"alreadySaved"の値をtrueにする
|
|
|
|
|
e.putBoolean("alreadySaved", true);
|
2024-01-18 15:57:33 +00:00
|
|
|
|
|
2024-01-17 08:55:04 +00:00
|
|
|
|
//確定処理
|
|
|
|
|
e.apply();
|
|
|
|
|
//画面遷移&ID受け渡し
|
|
|
|
|
SharedPreferences sharedPreferences1 = getActivity().getSharedPreferences("app_situation", MODE_PRIVATE);
|
|
|
|
|
SharedPreferences.Editor editor = sharedPreferences1.edit();
|
|
|
|
|
editor.putString("ID", documentReference.getId());
|
|
|
|
|
editor.apply();
|
|
|
|
|
|
|
|
|
|
Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show();
|
2024-01-17 09:58:25 +00:00
|
|
|
|
getParentFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragmentContainerView, GenerateQrFragment.newInstance(documentReference.getId())).commit();
|
2024-01-17 08:55:04 +00:00
|
|
|
|
})
|
|
|
|
|
.addOnFailureListener(e -> {
|
|
|
|
|
//失敗したら
|
|
|
|
|
Log.w(TAG, "Error adding document", e);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-01-17 08:23:07 +00:00
|
|
|
|
}
|
2024-01-17 07:55:44 +00:00
|
|
|
|
}
|