how to store textClock in Firebase FireStore

I want to store textClock on Firebase FireStore, been trying for almost a week on how to solve them but still couldn’t get. I am doing an attendance system for my final year project, this code is supposed to return and store values (user ID, name, time and location) in Firebase FireStore once user has clicked the button from previous activity

Here’s the code that I have done

public class DetailsKehadiran extends AppCompatActivity {

TextView tvNama, tvID, tvWaktu, tvLocation;
FusedLocationProviderClient fusedLocationProviderClient;
FirebaseAuth firebaseAuth;
FirebaseFirestore fStore;
TextClock textClock;
String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_kehadiran);

    firebaseAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();
    userId = firebaseAuth.getCurrentUser().getUid();

    tvID = findViewById(R.id.tvID);
    tvNama = findViewById(R.id.tvNama);
    tvWaktu = findViewById(R.id.tvWaktuMasuk);
    tvLocation = findViewById(R.id.tvLocation);
    textClock = findViewById(R.id.textClock);

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    if (ActivityCompat.checkSelfPermission(DetailsKehadiran.this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(DetailsKehadiran
    .this,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        getLocation();
    } else {
        //when permission is denied
        //request permission
        ActivityCompat.requestPermissions(DetailsKehadiran.this, new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}
                ,100);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //check condition
    if (requestCode == 100 && grantResults.length > 0 && (grantResults[0] + grantResults[1]
    == PackageManager.PERMISSION_GRANTED)){
        getLocation();
    }else{
        Toast.makeText(getApplicationContext(), "Permintaan ditolak.",Toast.LENGTH_SHORT).show();
    }
}

@SuppressLint("MissingPermission")
private void getLocation() {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //check condition
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

        fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                Location location = task.getResult();
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (location != null) {

                    try {
                        //initialize geocoder
                        Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                        //initialize address list
                        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        //set address on TextView
                        //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));

                        DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                        Map<String,Object> userInfo = new HashMap<>();
                        userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                        //userInfo.put("WaktuMasuk", Timestamp.now());
                        documentReference.update(userInfo);

                        documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                            @Override
                            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                tvNama.setText(documentSnapshot.getString("FullName"));
                                tvLocation.setText(documentSnapshot.getString("Location"));
                                tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else{
                    LocationRequest locationRequest= new LocationRequest()
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setInterval(10000)
                            .setFastestInterval(1000)
                            .setNumUpdates(1);

                    LocationCallback locationCallback = new LocationCallback() {
                        @Override
                        public void onLocationResult(LocationResult locationResult) {

                            Location location1 = locationResult.getLastLocation();
                            try {
                                //initialize geocoder
                                Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                                //initialize address list
                                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                                DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                                //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));
                                Map<String,Object> userInfo = new HashMap<>();
                                userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                                //userInfo.put("WaktuMasuk",Timestamp.now());
                                documentReference.update(userInfo);

                                documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                                    @Override
                                    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                        tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                        tvNama.setText(documentSnapshot.getString("FullName"));
                                        tvLocation.setText(documentSnapshot.getString("Location"));
                                        tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                                    }
                                });

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    fusedLocationProviderClient.requestLocationUpdates(locationRequest
                    ,locationCallback, Looper.myLooper());
                }
            }
        });
    }

    else{
        //when location service is not enabled
        //open location settings
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

I’m doing an attendance function btw, thank you in advance!

Source: Android Questions

LEAVE A COMMENT