diff --git a/app/src/main/java/com/example/childguard/HomeFragment.java b/app/src/main/java/com/example/childguard/HomeFragment.java index 732633c..881d7d5 100644 --- a/app/src/main/java/com/example/childguard/HomeFragment.java +++ b/app/src/main/java/com/example/childguard/HomeFragment.java @@ -39,7 +39,6 @@ import java.util.Objects; * create an instance of this fragment. */ public class HomeFragment extends Fragment implements OnEventListener{ - FirebaseFirestore db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -95,71 +94,7 @@ public class HomeFragment extends Fragment implements OnEventListener{ Button bt1 = view.findViewById(R.id.QRprinting); bt1.setOnClickListener(v -> { //初回起動かを保存する変数 - boolean alreadySaved = sharedPreferences.getBoolean("alreadySaved", false); - //ボタン変数の宣言 - Button parent = view.findViewById(R.id.QRprinting); - Button born = view.findViewById(R.id.QRprinting); - //falseのときにFirebaseへの登録 - if (alreadySaved) { - Log.d("HomeFragment", "already printed"); - //画面遷移&ID受け渡し - Toast.makeText(getActivity(), "再印刷", Toast.LENGTH_SHORT).show(); - QrUtils qrUtils = new QrUtils(); - PrintHelper printHelper = new PrintHelper(requireContext()); - printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); - printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(sharedPreferences.getString("ID", "placeholder")), new PrintHelper.OnPrintFinishCallback() { - @Override - public void onFinish() { - Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); - } - }); - return; - } else { - String valueParent = parent.getText().toString();//変数に文字列を代入 - String valueBorn = born.getText().toString();//変数に文字列を代入 - Map user = new HashMap<>();//mapの宣言 - Log.d("HomeFragment", "onClick is called"); - - //mapに入れる - user.put("parent", valueParent); - user.put("born", valueBorn); - //新しいドキュメントにIDを作って追加 - db.collection("users") - .add(user) - .addOnSuccessListener(new OnSuccessListener() { - @Override - public void onSuccess(DocumentReference documentReference) { - //成功したら - //documentReference.getId()でID取得 - Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); - SharedPreferences.Editor e = sharedPreferences.edit(); - // キー"alreadySaved"の値をtrueにする - e.putBoolean("alreadySaved", true); - //確定処理 - e.apply(); - //画面遷移&ID受け渡し - str_key = "" + documentReference.getId(); - Toast.makeText(getActivity(), "初回登録", Toast.LENGTH_SHORT).show(); - QrUtils qrUtils = new QrUtils(); - PrintHelper printHelper = new PrintHelper(requireContext()); - printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); - printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(documentReference.getId()), new PrintHelper.OnPrintFinishCallback() { - @Override - public void onFinish() { - Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); - } - }); - } - }) - .addOnFailureListener(new OnFailureListener() { - @Override - public void onFailure(@NonNull Exception e) { - //失敗したら - Log.w(TAG, "Error adding document", e); - } - }); - } }); //bluetooth設定ボタンの処理 Button bt2 = view.findViewById(R.id.Bluetooth_setup); diff --git a/app/src/main/java/com/example/childguard/SettingFragment.java b/app/src/main/java/com/example/childguard/SettingFragment.java index c56e580..fd65f32 100644 --- a/app/src/main/java/com/example/childguard/SettingFragment.java +++ b/app/src/main/java/com/example/childguard/SettingFragment.java @@ -1,12 +1,29 @@ package com.example.childguard; +import static android.content.ContentValues.TAG; +import static android.content.Context.MODE_PRIVATE; + +import android.content.SharedPreferences; import android.os.Bundle; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; +import androidx.print.PrintHelper; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; +import android.widget.Toast; + +import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.tasks.OnSuccessListener; +import com.google.firebase.firestore.DocumentReference; +import com.google.firebase.firestore.FirebaseFirestore; + +import java.util.HashMap; +import java.util.Map; /** * A simple {@link Fragment} subclass. @@ -14,6 +31,8 @@ import android.view.ViewGroup; * create an instance of this fragment. */ public class SettingFragment extends Fragment { + + FirebaseFirestore db; public SettingFragment() { // Required empty public constructor } @@ -41,9 +60,68 @@ public class SettingFragment extends Fragment { public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + db = FirebaseFirestore.getInstance();//Firebaseとの紐づけ + view.findViewById(R.id.button_bluetooth_setting).setOnClickListener( v -> { - getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, bluetooth_setupFragment.newInstance("test", "test")).commit(); + getParentFragmentManager().beginTransaction().replace(R.id.fragmentContainerView, bluetooth_setupFragment.newInstance("test", "test")).addToBackStack(null).commit(); }); + view.findViewById(R.id.button_print_qr).setOnClickListener( v -> { + 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(); + QrUtils qrUtils = new QrUtils(); + PrintHelper printHelper = new PrintHelper(requireContext()); + printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); + printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(sharedPreferences.getString("ID", "placeholder")), () -> Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show()); + } else { + String valueParent = "placeholder"; + String valueBorn = "placeholder"; + Map user = new HashMap<>();//mapの宣言 + + Log.d("HomeFragment", "onClick is called"); + + //mapに入れる + user.put("parent", valueParent); + user.put("born", valueBorn); + //新しいドキュメントにIDを作って追加 + db.collection("users") + .add(user) + .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); + //確定処理 + 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(); + QrUtils qrUtils = new QrUtils(); + PrintHelper printHelper = new PrintHelper(requireContext()); + printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); + printHelper.printBitmap("QRコード", qrUtils.setContext(getContext()).getBitmap(documentReference.getId()), new PrintHelper.OnPrintFinishCallback() { + @Override + public void onFinish() { + Toast.makeText(getContext(), "印刷完了", Toast.LENGTH_SHORT).show(); + } + }); + }) + .addOnFailureListener(e -> { + //失敗したら + Log.w(TAG, "Error adding document", e); + }); + } + }); } } \ No newline at end of file