Skip to content

Commit

Permalink
Merge pull request #73 from CSC207-2022F-UofT/asynchronously_update_bill
Browse files Browse the repository at this point in the history
[*] Made bill update run asynchronously and added supports for bill splitter.
  • Loading branch information
ScottCTD authored Dec 3, 2022
2 parents a550ddd + bc2776e commit c8b9851
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/main/java/billgates/database/MySQLDatabaseGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MySQLDatabaseGateway() {
this.columnToDatabaseColumn.put("From", "from");
this.columnToDatabaseColumn.put("To", "to");
this.columnToDatabaseColumn.put("Location", "location");
this.columnToDatabaseColumn.put("Splitter", "splitter");
this.columnToDatabaseColumn.put("Splitter", "split_bill_id");
this.columnToDatabaseColumn.put("Payee", "payee");
this.columnToDatabaseColumn.put("Paid Back", "paid_back");
}
Expand Down Expand Up @@ -630,7 +630,7 @@ public void createBillTable(int billId) {
Statement statement = connection.createStatement();

String query = String.format("""
CREATE TABLE bill_%d
CREATE TABLE IF NOT EXISTS bill_%d
(
entry_id INT AUTO_INCREMENT
PRIMARY KEY,
Expand Down Expand Up @@ -658,7 +658,7 @@ public void createSplitBillTable(int billId) {
Statement statement = connection.createStatement();

String query = String.format("""
CREATE TABLE bill_%d_%d
CREATE TABLE IF NOT EXISTS bill_%d_%d
(
entry_id INT AUTO_INCREMENT
PRIMARY KEY,
Expand Down Expand Up @@ -699,7 +699,7 @@ public void createUsersTable() {


String query = """
CREATE TABLE users
CREATE TABLE IF NOT EXISTS users
(
user_id INT AUTO_INCREMENT
PRIMARY KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,30 @@ public void updateBill(int billId) {
case -2 -> billId = user.getBillId();
}
user.setCurrentBillID(billId);
// get all entries of the current bill
List<Entry> result = this.gateway.getBillData(user.getCurrentBillID()).getEntries()
.stream().map(d -> d.toEntryBuilder().buildEntry()).toList();
List<List<Object>> list = result.stream().map(Entry::toObjects).toList();
// if the current bill id is not the same as the bill id, then the current bill is a splitter bill
this.presenter.updateBill(new BillUpdateResponseModel(list, user.getCurrentBillID() != user.getBillId()));
// we query the database asynchronously to make the program run smoother
Thread thread = new Thread(() -> {
List<Entry> result;
if (user.getBillId() != user.getCurrentBillID()) {
// if we are updating the splitter bill, then we create the splitter bill if
// not exist
this.gateway.createSplitBillTable(user.getCurrentBillID());
// modify the split_bill_id column
this.gateway.modifyEntry(user.getBillId(), user.getCurrentBillID(),
"Splitter", String.valueOf(user.getCurrentBillID()));
// get all entries of the current bill
result = this.gateway.getSplitBillData(user.getCurrentBillID()).getEntries()
.stream().map(d -> d.toEntryBuilder().buildEntry()).toList();
} else {
result = this.gateway.getBillData(user.getCurrentBillID()).getEntries()
.stream().map(d -> d.toEntryBuilder().buildEntry()).toList();
}
// transform all entries to lists of raw objects like int, ZonedDateTime, ...
List<List<Object>> list = result.stream().map(Entry::toObjects).toList();
// if the current bill id is not the same as the bill id,
// then the current bill is a splitter bill
this.presenter.updateBill(new BillUpdateResponseModel(list,
user.getCurrentBillID() != user.getBillId()));
});
thread.start();
}
}
6 changes: 4 additions & 2 deletions src/main/java/billgates/view/gui/ActionPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void initButtonTextArea() {
this.backButton.setAlignmentX(CENTER_ALIGNMENT);
this.add(Box.createRigidArea(new Dimension(0, VERTICAL_GAP)));
// Back from splitting bills event
this.addEntryButton.addActionListener((e -> this.backFromSplit()));
this.backButton.addActionListener((e -> this.backFromSplit()));
// backButton should be disabled at the beginning
this.backButton.setEnabled(false);

Expand Down Expand Up @@ -196,6 +196,7 @@ private void signOut() {
this.signOutButton.setEnabled(false);
this.addEntryButton.setEnabled(false);
this.deleteEntryButton.setEnabled(false);
this.backButton.setEnabled(false);

// The usernameField and passwordField should be editable after signing out
this.usernameField.setEditable(true);
Expand All @@ -210,7 +211,7 @@ private void signOut() {
topMenuBar.getFileMenu().setEnabled(false);

// Disable the billTable
BillTable billTable = (BillTable) this.mainFrame.getBillPanel().getBillTable();
BillTable billTable = this.mainFrame.getBillPanel().getBillTable();
billTable.setEnabled(false);
billTable.setVisible(false);
}
Expand Down Expand Up @@ -292,6 +293,7 @@ public void view(UserJoinViewModel viewModel) {
this.signInButton.setEnabled(false);
this.signOutButton.setEnabled(true);
this.addEntryButton.setEnabled(true);
this.backButton.setEnabled(true);

// the usernameField and passwordField shouldn't be editable
this.usernameField.setEditable(false);
Expand Down
28 changes: 12 additions & 16 deletions src/main/java/billgates/view/gui/BillPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ public void changeFont(String f){

@Override
public void update(BillUpdateViewModel viewModel) {
String[] columns = viewModel.getColumns();
List<List<Object>> entries = viewModel.getEntries();
BillTableModel model = this.getBillTable().getModel();
model.setColumnNames(columns);
model.setData(entries);
this.getBillTable().updateUI();
// we use invoke later here because this method is called from other threads.
SwingUtilities.invokeLater(() -> {
String[] columns = viewModel.getColumns();
List<List<Object>> entries = viewModel.getEntries();
BillTableModel model = this.getBillTable().getModel();
model.setColumnNames(columns);
model.setData(entries);
this.getBillTable().updateUI();
});
}

/**
Expand All @@ -105,27 +108,20 @@ public void mouseClicked(MouseEvent e) {
@Override
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println(2);
// trigger to splitter bill
Point point = new Point(e.getX(), e.getY());
int row = BillPanel.this.billTable.rowAtPoint(point);
int column = BillPanel.this.billTable.columnAtPoint(point);
if (row == -1 || column == -1)
return;
String name = BillPanel.this.billTable.getColumnName(column);
if ("Splitter".equals(name))
if (!"Splitter".equals(name))
return;
String splitter = (String) BillPanel.this.billTable.getModel().getValueAt(row, column);
if ("No".equals(splitter)) {
// TODO: call create splitter bill use case
}
// get the entry id
int entryId = (int) BillPanel.this.getBillTable().getModel().getValueAt(row, 0);
// for debugging TODO: delete it
System.out.println(entryId);
// call the bill update use case on the entryId
// TODO: uncomment the line below. I commented because I don't have create splitter use case now.
// SwingUtilities.invokeLater(() -> this.mainFrame.getBillUpdateController().update(entryId));
SwingUtilities.invokeLater(() ->
BillPanel.this.mainFrame.getBillUpdateController().update(entryId));
}
}
}
Expand Down

0 comments on commit c8b9851

Please sign in to comment.