実装を整理

This commit is contained in:
ろむねこ 2024-01-15 12:12:42 +09:00
parent 0079987075
commit f7566d9df0
No known key found for this signature in database
GPG Key ID: FA1F39A1BA37D168

View File

@ -12,6 +12,7 @@ import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
@ -91,41 +92,52 @@ public class HomeFragment extends Fragment {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
//初回起動かを保存する変数 //初回起動かを保存する変数
int i = Integer.parseInt(preferences.getString("kidoukaisuu", "1")); // int i = Integer.parseInt(preferences.getString("kidoukaisuu", "1"));
boolean alreadySaved = preferences.getBoolean("alreadySaved", false);
//ボタン変数の宣言 //ボタン変数の宣言
Button parent = view.findViewById(R.id.QRprinting); Button parent = view.findViewById(R.id.QRprinting);
Button born = view.findViewById(R.id.QRprinting); Button born = view.findViewById(R.id.QRprinting);
if (i == 1) {//QRコード印刷を初めて押したときにFireBaseへの登録を行う if (alreadySaved) {
String QRYomitorisya = parent.getText().toString();//変数に文字列を代入 Log.d("HomeFragment", "already printed");
String valueBorn = born.getText().toString();//変数に文字列を代入 return;
Map<String, String> user = new HashMap<>();//mapの宣言 } else Log.d("HomeFragment", "not printed yet"); // debug
Log.d("HomeFragment", "onClick is called"); String valueParent = parent.getText().toString();//変数に文字列を代入
String valueBorn = born.getText().toString();//変数に文字列を代入
Map<String, String> user = new HashMap<>();//mapの宣言
//mapに入れる Log.d("HomeFragment", "onClick is called");
user.put("parent", QRYomitorisya);
user.put("born", valueBorn);
//新しいドキュメントにIDを作って追加
db.collection("users")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {//エラー処理
Log.w(TAG, "Error adding document", e);
}
}); //mapに入れる
user.put("parent", valueParent);
user.put("born", valueBorn);
//新しいドキュメントにIDを作って追加
db.collection("users")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
//成功したら
Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId());
SharedPreferences.Editor e = preferences.edit();
e.putBoolean("alreadySaved", true);
e.apply();
//画面遷移
replaceFragment(new QrPrintFragment());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//失敗したら
Log.w(TAG, "Error adding document", e);
}
});
} //
SharedPreferences.Editor e = preferences.edit(); // SharedPreferences.Editor e = preferences.edit();
e.putString("kidoukiroku", "2"); // e.putString("kidoukiroku", "2");
e.apply(); // e.apply();
replaceFragment(new QrPrintFragment()); replaceFragment(new QrPrintFragment());
} }
}); });