<commit_before>package edu.usc.parknpay.seeker;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import edu.usc.parknpay.R;
import edu.usc.parknpay.utility.TemplateActivity;
import edu.usc.parknpay.database.Transaction;
import edu.usc.parknpay.database.User;

public class HistoryActivity extends TemplateActivity {

    private ListView historyList;
    private ArrayList<Transaction> history;
    private HistoryListAdapter historyListAdapter;
    private DatabaseReference Ref;

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

        setUpToolbar("History");

        // Add adapter to ListView
        historyList = (ListView) findViewById(R.id.history_list);
        history = new ArrayList<Transaction>();
        historyListAdapter = new HistoryListAdapter(HistoryActivity.this, history);
        historyList.setAdapter(historyListAdapter);

        String userId = User.getInstance().getId();
        DatabaseReference Ref = FirebaseDatabase.getInstance().getReference();
        Ref.child("Transactions").orderByChild("seekerId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Map<String, Object> spots = (Map<String,Object>)dataSnapshot.getValue();
                if (spots == null) {return;}
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Transaction t = snapshot.getValue(Transaction.class);
                    processTransaction(t);
                    historyListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Transaction t = history.get(position);

                // Get current date and time
                Date today = Calendar.getInstance().getTime();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String currTime = dateFormat.format(today);

                if(!t.isRated() && currTime.compareTo(t.getEndTime()) >= 0) {
                    Intent intent = new Intent(getApplicationContext(), edu.usc.parknpay.seeker.RateActivity.class);
                    intent.putExtra("transaction", t);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
            }
        });
    }

    private void processTransaction(Transaction t) {
        for (int i = 0; i < history.size(); ++i) {
            // If item exists, replace it
            if (history.get(i).getTransactionId().equals(t.getTransactionId()))
            {
                history.set(i, t);
                return;
            }
        }
        // transaction was not part of array
        history.add(t);
    }

    protected class HistoryListAdapter extends ArrayAdapter<Transaction> {

        public HistoryListAdapter(Context context, ArrayList<Transaction> results) {
            super(context, 0, results);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.seeker_history_item, parent, false);
            }
            // Get data item
            Transaction transaction = getItem(position);

            // Set image
            ImageView spotImageView = (ImageView) convertView.findViewById(R.id.history_image);<commit_msg>fix seeker HistoryActivity to refresh<commit_after>package edu.usc.parknpay.seeker;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import edu.usc.parknpay.R;
import edu.usc.parknpay.utility.TemplateActivity;
import edu.usc.parknpay.database.Transaction;
import edu.usc.parknpay.database.User;

public class HistoryActivity extends TemplateActivity {

    private ListView historyList;
    private ArrayList<Transaction> history;
    private HistoryListAdapter historyListAdapter;
    private DatabaseReference Ref;

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

        setUpToolbar("History");

        // Add adapter to ListView
        historyList = (ListView) findViewById(R.id.history_list);
        history = new ArrayList<Transaction>();
        historyListAdapter = new HistoryListAdapter(HistoryActivity.this, history);
        historyList.setAdapter(historyListAdapter);

        String userId = User.getInstance().getId();
        DatabaseReference Ref = FirebaseDatabase.getInstance().getReference();
        Ref.child("Transactions").orderByChild("seekerId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Map<String, Object> spots = (Map<String,Object>)dataSnapshot.getValue();
                if (spots == null) {return;}
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Transaction t = snapshot.getValue(Transaction.class);
                    processTransaction(t);
                    historyListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        historyList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Transaction t = history.get(position);

                // Get current date and time
                Date today = Calendar.getInstance().getTime();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String currTime = dateFormat.format(today);

                if(!t.isRated() && currTime.compareTo(t.getEndTime()) >= 0) {
                    Intent intent = new Intent(getApplicationContext(), edu.usc.parknpay.seeker.RateActivity.class);
                    intent.putExtra("transaction", t);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(intent);
                }
            }
        });
    }

    private void processTransaction(Transaction t) {
        for (int i = 0; i < history.size(); ++i) {
            // If item exists, replace it
            if (history.get(i).getTransactionId().equals(t.getTransactionId()))
            {
                history.set(i, t);
                return;
            }
        }
        // transaction was not part of array
        history.add(t);
    }

    @Override
    protected void onResume() {
        super.onResume();

        String userId = User.getInstance().getId();
        DatabaseReference Ref = FirebaseDatabase.getInstance().getReference();
        Ref.child("Transactions").orderByChild("seekerId").equalTo(userId).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Map<String, Object> spots = (Map<String,Object>)dataSnapshot.getValue();
                if (spots == null) {return;}
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Transaction t = snapshot.getValue(Transaction.class);
                    processTransaction(t);
                    historyListAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });
    }

    protected class HistoryListAdapter extends ArrayAdapter<Transaction> {

        public HistoryListAdapter(Context context, ArrayList<Transaction> results) {
            super(context, 0, results);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.seeker_history_item, parent, false);
            }
            // Get data item
            Transaction transaction = getItem(position);

            // Set image
            ImageView spotImageView = (ImageView) convertView.findViewById(R.id.history_image);
{'fraction_non_alphanumeric': 0.09629990262901655, 'fraction_numerical': 0.0005842259006815969, 'mean_word_length': 2.6203736341205497, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5324218', 'n_tokens_mistral': 2604, 'n_tokens_neox': 2498, 'n_words': 528}