Skip to content

Commit

Permalink
Merge pull request #33 from virresh/deleteProposalLab
Browse files Browse the repository at this point in the history
Delete expired proposal
  • Loading branch information
virresh authored Nov 6, 2019
2 parents 8dd9517 + e636e58 commit a09a177
Showing 1 changed file with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@
import android.content.Context;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TableLayout;

import androidx.annotation.NonNull;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
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 com.google.gson.internal.bind.DateTypeAdapter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.mobilecomputing.sahayak.Fragments.proposalShowFragment.TAG;


public class ProposalLab {
private static ProposalLab sproposalLab;
private List<Proposal> mProposals = new ArrayList<>();
private DatabaseReference mDatabase;
private ProgressBar progressBar;
DatabaseReference mRef;
FirebaseDatabase mFirebaseDatabase;

private ProposalLab(final Context context) {
mDatabase = FirebaseDatabase.getInstance().getReference("active_proposals");
Expand All @@ -32,8 +42,12 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mProposals.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
Proposal p = ds.getValue(Proposal.class);
mProposals.add(p);
Log.d(TAG,p.getMentorName()+"----"+ currentUser.getEmail());
if(!p.getMentorName().equals(currentUser.getEmail())){
mProposals.add(p);
}
}
}
}
Expand Down Expand Up @@ -62,9 +76,33 @@ public void AddProposal(Proposal proposal) {
// TODO: Handle Duplicates
dref.setValue(proposal);
}
public void deleteProposals(){
mDatabase = FirebaseDatabase.getInstance().getReference("active_proposals");
mDatabase.orderByChild("startDate").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot proposalSnapshot: dataSnapshot.getChildren()) {
Proposal proposal=proposalSnapshot.getValue(Proposal.class);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(proposal.getStartDate());
endCalendar.add(Calendar.MINUTE, proposal.getDuration());
Log.d(TAG,new Date().toString()+ " -----"+ endCalendar.getTime());
if(endCalendar.getTime().before(new Date())){
Log.d(TAG,"before");
proposalSnapshot.getRef().removeValue();
}
}
} @Override
public void onCancelled(DatabaseError databaseError) {

}

});

}
public List<Proposal> getProposals() {
Log.d("ProposalLab", "Getting " + mProposals.size() + " Proposals " + mProposals.getClass().getSimpleName());
this.deleteProposals();
return mProposals;
}

Expand Down

0 comments on commit a09a177

Please sign in to comment.