Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
4.3 kB
2
Indexable
Never
package com.example.projectcando;

import android.provider.ContactsContract;

import androidx.annotation.NonNull;

import com.google.firebase.FirebaseCommonRegistrar;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class UserHelper {
    private String Username;
    private String ID;
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference ref = db.getReference("Users");

    private int totalScore;


    public UserHelper() {
    }

    public UserHelper(String _id)
    {
        this.ID = _id;
    }

    public UserHelper(String username, String ID, int totalScore) {
        db = FirebaseDatabase.getInstance();
        ref = db.getReference("Users");
        Username = username;
        this.ID = ID;
        totalScore = totalScore;
    }

    public interface getUserTotalScore
    {
        void totalScoreLoaded(int totalScore);
    }

    public interface getUserEmail
    {
        void userEmailLoaded(String userEmail);
    }

    public void getScore(getUserTotalScore getUserTotalScore)
    {
       DatabaseReference scoreRef = ref.child(ID).child("totalScore").getRef();
       scoreRef.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               int score = snapshot.getValue(Integer.class);

               getUserTotalScore.totalScoreLoaded(score);
           }

           @Override
           public void onCancelled(@NonNull DatabaseError error) {

           }
       });

    }

    public void getEmail(getUserEmail getUserEmail)
    {
        FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
        DatabaseReference mRef = mDatabase.getReference("User");
        DatabaseReference emailRef = mRef.child(ID).child("Username").getRef();

        emailRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String userEmail = snapshot.getValue(String.class);

                getUserEmail.userEmailLoaded(userEmail);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    public void addToDB()
    {



        //Load User Count from database and generate next ID
       ref.addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {

               //New UserHelper created with the values of this class to fill in database
               UserHelper u = new UserHelper(Username,ID,0);
               ref.child(ID).setValue(u);
           }

           @Override
           public void onCancelled(@NonNull DatabaseError error) {

           }
       });



    }

    public String  getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public long getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(int totalScore) {
        this.totalScore = totalScore;
    }

    public void updateTotalScore(int scoreToAdd)
    {
        DatabaseReference  scoreRef = ref.child(ID).child("totalScore").getRef();

        scoreRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                int curScore = snapshot.getValue(Integer.class);

                curScore += scoreToAdd;

                scoreRef.setValue(curScore);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    }