Android Open Source - BusTicketer Buy Tickets Fragment






From Project

Back to project page BusTicketer.

License

The source code is released under:

Copyright (c) 2013, Nelspike All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Red...

If you think the Android project BusTicketer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package bus.ticketer.fragments;
/*from   ww  w.  ja  v  a 2  s. co m*/
import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import bus.ticketer.adapters.DialogAdapter;
import bus.ticketer.connection.ConnectionThread;
import bus.ticketer.listeners.TicketPurchaseListener;
import bus.ticketer.objects.Ticket;
import bus.ticketer.passenger.BusTicketer;
import bus.ticketer.passenger.R;
import bus.ticketer.utils.BusUtils;
import bus.ticketer.utils.FileHandler;
import bus.ticketer.utils.Method;
import bus.ticketer.utils.RESTFunction;

public class BuyTicketsFragment extends Fragment {
  private View rootView;
  private RESTFunction currentFunction;
  private SparseArray<ArrayList<Ticket>> tickets;
  private int transactionCost;
  private int[] boughtTickets = new int[3];
  private String confirmationToken = "", IPAddress = "";
  private BusTicketer app;

  @SuppressLint("HandlerLeak")
  private Handler threadConnectionHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (currentFunction) {
        case BUY_CLIENT_TICKETS:
          tickets = app.getTickets();
          break;      
        case BUY_CLIENT_TICKETS_CLICK:
          handlePurchase(msg);
          break;
        case BUY_CONFIRMATION_CLIENT:
          handleBuyPayload(msg);
          break;
        default:
          break;
      }
    }
  };

  @Override
  public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {
    
    setRetainInstance(true);
    rootView = inflater.inflate(R.layout.fragment_buy_tickets,
          container, false);

    app = (BusTicketer) getActivity().getApplication();
    IPAddress = app.getIPAddress();
    getTicketInfo();
    buyTicketsHandler();

    return rootView;
  }
  
  public void refresh() {
    app = (BusTicketer) getActivity().getApplication();
    getTicketInfo();
    buyTicketsHandler();  
  }
  
  public void getTicketInfo() {
    FileHandler fHandler = new FileHandler(app.getClientFilename(), "");
    ArrayList<String> fileContents = fHandler.readFromFile();

    
    if(!app.isNetworkAvailable()) {
      SparseArray<ArrayList<Ticket>> tickets = FileHandler.getTicketCount();
      app.setTickets(tickets);
      quantityHandler();      
    }
    else {
      currentFunction = RESTFunction.BUY_CLIENT_TICKETS;
      
      ConnectionThread dataThread = new ConnectionThread(
          IPAddress+"list/" + fileContents.get(2),
          Method.GET, null, threadConnectionHandler, null,
          currentFunction, rootView, getActivity());
      dataThread.start();
    }
  }

  public void buyTicketsHandler() {

    Button t1Minus = (Button) rootView.findViewById(R.id.t1_ticket_minus);
    Button t2Minus = (Button) rootView.findViewById(R.id.t2_ticket_minus);
    Button t3Minus = (Button) rootView.findViewById(R.id.t3_ticket_minus);
    Button t1Plus = (Button) rootView.findViewById(R.id.t1_ticket_plus);
    Button t2Plus = (Button) rootView.findViewById(R.id.t2_ticket_plus);
    Button t3Plus = (Button) rootView.findViewById(R.id.t3_ticket_plus);

    Button buyTickets = (Button) rootView
        .findViewById(R.id.buy_tickets_button);

    final TextView t1Tickets = (TextView) rootView
        .findViewById(R.id.t1_ticket_quantity_buy);
    final TextView t2Tickets = (TextView) rootView
        .findViewById(R.id.t2_ticket_quantity_buy);
    final TextView t3Tickets = (TextView) rootView
        .findViewById(R.id.t3_ticket_quantity_buy);
    
    t1Tickets.setText("0");
    t2Tickets.setText("0");
    t3Tickets.setText("0");

    t1Minus.setOnClickListener(new TicketPurchaseListener("Minus", "T1", t1Tickets, app));
    t2Minus.setOnClickListener(new TicketPurchaseListener("Minus", "T2", t2Tickets, app));
    t3Minus.setOnClickListener(new TicketPurchaseListener("Minus", "T3", t3Tickets, app));
    t1Plus.setOnClickListener(new TicketPurchaseListener("Plus", "T1", t1Tickets, app));
    t2Plus.setOnClickListener(new TicketPurchaseListener("Plus", "T2", t2Tickets, app));
    t3Plus.setOnClickListener(new TicketPurchaseListener("Plus", "T3", t3Tickets, app));

    buyTickets.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {

        FileHandler fHandler = new FileHandler(app.getClientFilename(), "");
        ArrayList<String> fileContents = fHandler.readFromFile();
        
        final ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("cid", fileContents.get(2)));
        params.add(new BasicNameValuePair("t1", t1Tickets.getText().toString()));
        params.add(new BasicNameValuePair("t2", t2Tickets.getText().toString()));
        params.add(new BasicNameValuePair("t3", t3Tickets.getText().toString()));

        ProgressDialog progDialog = ProgressDialog.show(getActivity(),
            "", "Loading, please wait!",
            true);

        progDialog.setOnDismissListener(new OnDismissListener() {

          @Override
          public void onDismiss(DialogInterface dialog) {
            
            ProgressDialog pDiag = ProgressDialog.show(getActivity(),
                "", "Loading, please wait!",
                true);

            pDiag.setOnDismissListener(new OnDismissListener() {

              @Override
              public void onDismiss(DialogInterface dialog) {
                BusUtils.purchaseProcess(getActivity());
                DialogAdapter.purchaseSuccess(boughtTickets[0], boughtTickets[1], boughtTickets[2], transactionCost, getActivity());
              }
            });
            
            int[] data = {boughtTickets[0], boughtTickets[1], boughtTickets[2], transactionCost};
            currentFunction = RESTFunction.BUY_CONFIRMATION_CLIENT;
            DialogAdapter.confirmPurchase(params, pDiag, data, getActivity(), confirmationToken, threadConnectionHandler, rootView);
          }
        });
        
        currentFunction = RESTFunction.BUY_CLIENT_TICKETS_CLICK;
        ConnectionThread dataThread = new ConnectionThread(
            IPAddress+"buy/", Method.POST,
            params, threadConnectionHandler, progDialog,
            currentFunction, rootView, getActivity());
        
        dataThread.start();
      }

    });
    
    if(app.getTickets().get(1).size() >= 10) {
      t1Minus.setEnabled(false);
      t1Plus.setEnabled(false);
    }
    else {
      t1Minus.setEnabled(true);
      t1Plus.setEnabled(true);      
    }
    
    if(app.getTickets().get(2).size() >= 10) {
      t2Minus.setEnabled(false);
      t2Plus.setEnabled(false);
    }
    else {
      t2Minus.setEnabled(true);
      t2Plus.setEnabled(true);    
    }
    
    if(app.getTickets().get(3).size() >= 10) {
      t3Minus.setEnabled(false);
      t3Plus.setEnabled(false);
    }
    else {
      t3Minus.setEnabled(true);
      t3Plus.setEnabled(true);      
    }
    
    if(app.isTimerOn() || !app.isNetworkAvailable() || app.isWaitingValidation()) {
      t1Minus.setEnabled(false);
      t2Minus.setEnabled(false);
      t3Minus.setEnabled(false);
      t1Plus.setEnabled(false);
      t2Plus.setEnabled(false);
      t3Plus.setEnabled(false);
      buyTickets.setEnabled(false);
    }
    else
      buyTickets.setEnabled(true);
  }
  
  private void handleBuyPayload(Message msg) {
        JSONObject ticketListing = (JSONObject) msg.obj;
        try {
          for(int i = 1; i <= 3; i++) {
            JSONArray ticketsArray = ticketListing.getJSONArray("t"+i);
            for(int t = 0; t < ticketsArray.length(); t++)
              tickets.get(i).add(new Ticket(ticketsArray.getInt(t)));
            
            boughtTickets[i-1] = ticketsArray.length();
          }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        app.setTickets(tickets);
  }
  
  private void handlePurchase(Message msg) {
        JSONObject ticketListing = (JSONObject) msg.obj;
        try {
          for(int i = 0; i < 3; i++)
            boughtTickets[i] = ticketListing.getInt("t"+(i+1));
          
            transactionCost = ticketListing.getInt("cost");
            confirmationToken = ticketListing.getString("token");
        } catch (JSONException e) {
            e.printStackTrace();
        }
  }
  
  private void quantityHandler() {
    TextView t1TicketsQuantity = (TextView) rootView
        .findViewById(R.id.t1_ticket_quantity);
    TextView t2TicketsQuantity = (TextView) rootView
        .findViewById(R.id.t2_ticket_quantity);
    TextView t3TicketsQuantity = (TextView) rootView
        .findViewById(R.id.t3_ticket_quantity);

    SparseArray<ArrayList<Ticket>> tickets = app.getTickets();
    
    t1TicketsQuantity.setText(tickets.get(1).size() + "");
    t2TicketsQuantity.setText(tickets.get(2).size() + "");
    t3TicketsQuantity.setText(tickets.get(3).size() + "");
  }  
}




Java Source Code List

bus.ticketer.adapters.CentralPagerAdapter.java
bus.ticketer.adapters.DialogAdapter.java
bus.ticketer.adapters.SpinnerAdapter.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.filters.MonthInputFilter.java
bus.ticketer.fragments.BuyTicketsFragment.java
bus.ticketer.fragments.ShowTicketsFragment.java
bus.ticketer.inspector.BusInspector.java
bus.ticketer.inspector.MainActivity.java
bus.ticketer.inspector.ResultActivity.java
bus.ticketer.inspector.ScanActivity.java
bus.ticketer.listeners.BusTabListener.java
bus.ticketer.listeners.PurchaseListener.java
bus.ticketer.listeners.RadioGroupListener.java
bus.ticketer.listeners.SwipeListener.java
bus.ticketer.listeners.TicketPurchaseListener.java
bus.ticketer.listeners.ValidationListener.java
bus.ticketer.objects.Ticket.java
bus.ticketer.passenger.BeamActivity.java
bus.ticketer.passenger.BusTicketer.java
bus.ticketer.passenger.CentralActivity.java
bus.ticketer.passenger.MainActivity.java
bus.ticketer.runnable.BuyRunnable.java
bus.ticketer.runnable.ShowRunnable.java
bus.ticketer.runnable.TimerRunnable.java
bus.ticketer.runnable.TimerThread.java
bus.ticketer.utils.BusTimer.java
bus.ticketer.utils.BusUtils.java
bus.ticketer.utils.FileHandler.java
bus.ticketer.utils.FileWriter.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.SpinnerAdapter.java
bus.ticketer.validation.MainActivity.java
bus.ticketer.validation.ResultActivity.java