示例#1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_summary);
    server = new SimpleTCPServer(TCP_PORT);
    server.setOnDataReceivedListener(
        new SimpleTCPServer.OnDataReceivedListener() {
          @Override
          public void onDataReceived(String message, String ip) {
            JsonFunction jsonFunction = new JsonFunction(getApplicationContext());
            JsonFunction.Message jsonMessage = JsonFunction.acceptMessage(message);
            jsonFunction.decideWhatToDo(jsonMessage); // It's update data to database.
            // if message is ORDER_STATUS_MESSAGE let update immediately.
            if (jsonMessage
                .getMessageType()
                .equals(JsonFunction.Message.Type.ORDER_STATUS_MESSAGE)) {
              JSONObject body = jsonMessage.getJsonBody();
              try {
                int updateItemId = body.getInt("pre_id");
                for (int i = 0; i < preOrderItemDetails.size(); i++) {
                  OrderItemDetail preOrderItemDetail = preOrderItemDetails.get(i);
                  if (preOrderItemDetail.getPreOderId() == updateItemId) {
                    preOrderItemDetail.setServed(body.getInt("served") == 1);
                    int statusValue = body.getInt("status");
                    preOrderItemDetail.setStatus(
                        (statusValue == 1) ? PreOrderItem.Status.DONE : PreOrderItem.Status.UNDONE);
                    // refresh LisView to display new data
                    preOrderItemDetails.remove(i);
                    preOrderItemDetails.add(i, preOrderItemDetail);
                    orderItemListAdapter.notifyDataSetChanged();
                    break;
                  }
                }
              } catch (JSONException e) {
                e.printStackTrace();
              }
            } else if (jsonMessage
                .getMessageType()
                .equals(JsonFunction.Message.Type.EDIT_ORDER_MESSAGE)) {
              JSONObject body = jsonMessage.getJsonBody();
              try {
                int editItemId = body.getInt("pre_id");
                for (int i = 0; i < preOrderItemDetails.size(); i++) {
                  OrderItemDetail preOrderItemDetail = preOrderItemDetails.get(i);
                  if (preOrderItemDetail.getPreOderId() == editItemId) {
                    preOrderItemDetail.setQuantity(body.getInt(PreOrderTable.Columns._QUANTITY));
                    preOrderItemDetail.setOption(body.getString(PreOrderTable.Columns._OPTION));
                    // refresh LisView to display new data
                    preOrderItemDetails.remove(i);
                    preOrderItemDetails.add(i, preOrderItemDetail);
                    orderItemListAdapter.notifyDataSetChanged();
                    // re-calculate total price
                    tvTotalPrice.setText(String.valueOf(findTotalPrice(preOrderItemDetails)));
                    break;
                  }
                }
              } catch (JSONException e) {
                e.printStackTrace();
              }

            } else if (jsonMessage
                .getMessageType()
                .equals(JsonFunction.Message.Type.PAY_CONFIRM_MESSAGE)) {
              preOrderItemDetails.clear();
              orderItemListAdapter.notifyDataSetChanged();
              tvTotalPrice.setText("0.0");

              final Dialog dialogPaymentSuccessful = new Dialog(SummaryActivity.this);
              dialogPaymentSuccessful.requestWindowFeature(Window.FEATURE_NO_TITLE);
              dialogPaymentSuccessful.setCancelable(true);
              dialogPaymentSuccessful.setContentView(R.layout.dialog_payment_successful);
              dialogPaymentSuccessful.show();
            }
          }
        });
    databaseDao = new ResterDao(SummaryActivity.this);
    databaseDao.open();

    preOrderItemDetails = databaseDao.getPreOrderDetail();

    ListView lvSummary = (ListView) findViewById(R.id.lvSummary);
    orderItemListAdapter = new OrderItemListAdapter(SummaryActivity.this, preOrderItemDetails);
    lvSummary.setAdapter(orderItemListAdapter);
    lvSummary.setOnItemClickListener(summaryOnItemClickListener);

    tvTake = (TextView) findViewById(R.id.textViewTake);
    String take = "*" + getResources().getString(R.string.text_take_here);
    if (new AppPreference(SummaryActivity.this).getTakeOrder().equals(Order.Take.HOME)) {
      take = "*" + getResources().getString(R.string.text_take_home);
    }
    tvTake.setText(take);

    tvTotalPrice = (TextView) findViewById(R.id.tvTotalPrice);
    tvTotalPrice.setText(String.valueOf(findTotalPrice(preOrderItemDetails)));

    btnConfirm = (Button) findViewById(R.id.btnConfirm);
    btnConfirm.setEnabled(false);
    if ((preOrderItemDetails.size() > 0)) {
      if (!preOrderItemDetails.get(0).isOrdered()) {
        // if first item is not ordered indicate it have items that are not ordered.
        btnConfirm.setEnabled(true);
        btnConfirm.setOnClickListener(confirmOnItemClickListener);
      }
      if (!preOrderItemDetails.get(preOrderItemDetails.size() - 1).isOrdered()) {
        // first time to order food
        showChooseTakeOrderDialog();
      }
    }
  }
示例#2
0
 @Override
 protected void onResume() {
   databaseDao.open();
   super.onResume();
   server.start();
 }