Skip to content

Commit

Permalink
Sprint2: Implement calculate and display result (#44)
Browse files Browse the repository at this point in the history
* Sprint2: Implement calculate and display result

* Sprint2: Fix asynchrony bug in model.UserDatabase.setVacation

* Sprint2: Fix data check bug in addDestination

---------

Co-authored-by: Tianrui Qi <[email protected]>
  • Loading branch information
yuwenellie and tianrui-qi authored Oct 21, 2024
1 parent 4bbe027 commit a0f83ba
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void setUsernameCurr(String username) {

public void addDestination(
String travelLocation, String startDate, String endDate, String duration,
MainModel.BoolCallback callback
MainModel.CallbackBool callback
) {
DatabaseReference refer = this.destDatabase.child(this.usernameCurr).child(travelLocation);
refer.addListenerForSingleValueEvent(new ValueEventListener() {
Expand All @@ -49,7 +49,7 @@ public void onCancelled(@NonNull DatabaseError error) {
});
}

public void getDestinations(MainModel.DestCallback callback) {
public void getDestinations(MainModel.CallbackDestination callback) {
DatabaseReference refer = this.destDatabase.child(this.usernameCurr);
refer.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public static synchronized MainModel getInstance() {

/* Main Features */

public void userSignUp(String username, String password, BoolCallback callback) {
public void userSignUp(String username, String password, CallbackBool callback) {
this.userDatabase.userSignUp(username, password, callback);
}

public void userSignIn(String username, String password, BoolCallback callback) {
public void userSignIn(String username, String password, CallbackBool callback) {
this.userDatabase.userSignIn(username, password, success -> {
if (success) {
this.destDatabase.setUsernameCurr(this.userDatabase.getUsernameCurr());
Expand All @@ -39,26 +39,35 @@ public void userSignIn(String username, String password, BoolCallback callback)

public void addDestination(
String travelLocation, String startDate, String endDate, String duration,
MainModel.BoolCallback callback
CallbackBool callback
) {
this.destDatabase.addDestination(travelLocation, startDate, endDate, duration, callback);
}

public void getDestinations(DestCallback callback) {
public void getDestinations(CallbackDestination callback) {
this.destDatabase.getDestinations(callback);
}

public void setVacation(String startDate, String endDate, String duration) {
this.userDatabase.setVacation(startDate, endDate, duration);
public void setVacation(
String startDate, String endDate, String duration, CallbackBool callback) {
this.userDatabase.setVacation(startDate, endDate, duration, callback);
}

public void getVacation(CallbackVacation callback) {
this.userDatabase.getVacation(callback);
}

/* Callbacks */

public interface BoolCallback {
void onResult(boolean success);
public interface CallbackBool {
void onResult(boolean callback);
}

public interface CallbackDestination {
void onResult(HashMap<String, HashMap<String, String>> callback);
}

public interface DestCallback {
void onResult(HashMap<String, HashMap<String, String>> dest);
public interface CallbackVacation {
void onResult(HashMap<String, String> callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public String getUsernameCurr() {
return this.usernameCurr;
}

public void userSignUp(String username, String password, MainModel.BoolCallback callback) {
public void userSignUp(String username, String password, MainModel.CallbackBool callback) {
DatabaseReference refer = this.userDatabase.child(username);
refer.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
Expand All @@ -44,7 +44,7 @@ public void onCancelled(@NonNull DatabaseError error) {
});
}

public void userSignIn(String username, String password, MainModel.BoolCallback callback) {
public void userSignIn(String username, String password, MainModel.CallbackBool callback) {
DatabaseReference refer = this.userDatabase.child(username);
refer.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
Expand All @@ -65,21 +65,55 @@ public void onCancelled(@NonNull DatabaseError error) {
});
}

public void setVacation(String startDate, String endDate, String duration) {
public void setVacation(
String startDate, String endDate, String duration, MainModel.CallbackBool callback
) {
DatabaseReference refer = this.userDatabase.child(this.usernameCurr).child("vacation");
refer.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
HashMap<String, String> value = new HashMap<>();
value.put("startDate", startDate);
value.put("endDate", endDate);
value.put("duration", duration);
refer.setValue(value);
HashMap<String, String> vacationData = new HashMap<>();
vacationData.put("startDate", startDate);
vacationData.put("endDate", endDate);
vacationData.put("duration", duration);
refer.setValue(vacationData);
callback.onResult(true);
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onResult(false);
}
});
}

public void getVacation(MainModel.CallbackVacation callback) {
DatabaseReference refer = this.userDatabase.child(this.usernameCurr).child("vacation");
refer.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// Prepare a HashMap to store the vacation data
HashMap<String, String> vacationData = new HashMap<>();
// Extract startDate, endDate, and duration from the snapshot
for (DataSnapshot child : dataSnapshot.getChildren()) {
String key = child.getKey();
String value = child.getValue(String.class);
if (key != null && value != null) {
vacationData.put(key, value);
}
}
// Pass the result to the callback
callback.onResult(vacationData);
} else {
// If no vacation data exists, return an empty HashMap
callback.onResult(null);
}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
callback.onResult(null);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.view.LayoutInflater;

import com.example.sprint1.R;
import com.example.sprint1.viewmodel.MainViewModel;
import java.util.HashMap;
Expand All @@ -26,14 +27,19 @@ protected void onCreate(Bundle savedInstanceState) {
this.buttonCancel();
this.buttonCalculate();
this.formCalculate();
this.buttonBack();
this.listDestination();
this.buttonNavigationBar();
}

private void buttonLogTravel() {
View formLogTravel = findViewById(R.id.HomeDestination_LogTravel_Form);
View formCalculate = findViewById(R.id.HomeDestination_Calculate_Form);
View resultCalculate = findViewById(R.id.HomeDestination_Calculate_Result);

findViewById(R.id.HomeDestination_LogTravel).setOnClickListener(v -> {
formCalculate.setVisibility(View.GONE);
resultCalculate.setVisibility(View.GONE);
if (formLogTravel.getVisibility() == View.VISIBLE) {
formLogTravel.setVisibility(View.GONE);
} else {
Expand Down Expand Up @@ -73,9 +79,13 @@ private void buttonCancel() {
}

private void buttonCalculate() {
View formLogTravel = findViewById(R.id.HomeDestination_LogTravel_Form);
View formCalculate = findViewById(R.id.HomeDestination_Calculate_Form);
View resultCalculate = findViewById(R.id.HomeDestination_Calculate_Result);

findViewById(R.id.HomeDestination_Calculate).setOnClickListener(v -> {
formLogTravel.setVisibility(View.GONE);
resultCalculate.setVisibility(View.GONE);
if (formCalculate.getVisibility() == View.VISIBLE) {
formCalculate.setVisibility(View.GONE);
} else {
Expand All @@ -88,23 +98,50 @@ private void formCalculate() {
EditText startDate = findViewById(R.id.HomeDestination_Calculate_StartDate);
EditText endDate = findViewById(R.id.HomeDestination_Calculate_EndDate);
EditText duration = findViewById(R.id.HomeDestination_Calculate_Duration);
View formCalculate = findViewById(R.id.HomeDestination_Calculate_Form);
View resultCalculate = findViewById(R.id.HomeDestination_Calculate_Result);
TextView resultView = findViewById(R.id.HomeDestination_Calculate_Message);

findViewById(R.id.HomeDestination_Calculate_Calculate).setOnClickListener(
v -> mainViewModel.setVacation(
startDate.getText().toString().trim(),
endDate.getText().toString().trim(),
duration.getText().toString().trim(),
success -> {
if (success) {
startDate.setText("");
endDate.setText("");
duration.setText("");
v -> mainViewModel.setVacation(
startDate.getText().toString().trim(),
endDate.getText().toString().trim(),
duration.getText().toString().trim(),
success -> {
if (success) {
startDate.setText("");
endDate.setText("");
duration.setText("");
mainViewModel.calVacation(
occupiedDays -> {
// Update the result view with the occupied days
if (occupiedDays != null) {
resultView.setText(getString(
R.string.HomeDestination_Calculate_Message, occupiedDays
));
} else {
resultView.setText(R.string.HomeDestination_Calculate_Error);
}
}
}
)
);
formCalculate.setVisibility(View.GONE);
resultCalculate.setVisibility(View.VISIBLE);
}
}
)
);
}

private void buttonBack() {
View formCalculate = findViewById(R.id.HomeDestination_Calculate_Form);
View resultCalculate = findViewById(R.id.HomeDestination_Calculate_Result);

findViewById(R.id.HomeDestination_Calculate_Back).setOnClickListener(v -> {
resultCalculate.setVisibility(View.GONE);
formCalculate.setVisibility(View.VISIBLE);
});
}

private void listDestination() {
LinearLayout container = findViewById(R.id.log_travel_records_container);
container.removeAllViews(); // Clear any previous views
Expand Down
Loading

0 comments on commit a0f83ba

Please sign in to comment.