Skip to content

Commit

Permalink
it's officialllll GROUPED OBJECTS
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMC2610 committed Mar 3, 2024
1 parent 54a72e6 commit 216ac6f
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class HistoryFragment extends Fragment
{
Expand Down Expand Up @@ -87,25 +90,37 @@ private void createCards()
hint.setText(getString(R.string.text_view_click_cards_message_empty));

// Group objects by year and month
Map<String, List<FuelFillRecord>> groupedRecords = new TreeMap<>();
Map<YearMonth, List<FuelFillRecord>> groupedRecords = new TreeMap<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy", Locale.getDefault());

for (int i = DataHolder.getInstance().records.size() - 1; i >= 0; i--)
{
FuelFillRecord record = DataHolder.getInstance().records.get(i);

String key = record.getDate().format(formatter);
YearMonth key = YearMonth.from(record.getDate());
groupedRecords.computeIfAbsent(key, k -> new ArrayList<>()).add(record);
}

for (String key : groupedRecords.keySet())
for (int i = groupedRecords.keySet().size() - 1; i >= 0; i--)
{
YearMonth key = new ArrayList<>(groupedRecords.keySet()).get(i);

List<FuelFillRecord> recordsInMonthYear = groupedRecords.get(key);
String stringYearMonth = key.format(formatter);

for (FuelFillRecord record : recordsInMonthYear)
{
// inflate the card view for the fuel fill records.
View v = inflater.inflate(R.layout.cardview_fill, null);
View v;

if (recordsInMonthYear.indexOf(record) == 0)
{
v = inflater.inflate(R.layout.cardview_fill_with_legend, null);
TextView legend = v.findViewById(R.id.card_year_month_legend);
legend.setText(stringYearMonth);
}
else
v = inflater.inflate(R.layout.cardview_fill, null);

// get the card's views.
TextView petrolType = v.findViewById(R.id.card_filled_petrol);
Expand Down
133 changes: 133 additions & 0 deletions app/src/main/res/layout/cardview_fill_with_legend.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- YEAR MONTH LEGEND -->
<TextView
android:id="@+id/card_year_month_legend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="8dp"
android:text="mmmm YYYY"
style="@style/textColor"
android:textSize="24sp"
android:textStyle="bold"/>

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="105dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
style="@style/cardMainBackground"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- ID -->
<TextView
android:id="@+id/card_hidden_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="60sp"
android:text="id"
style="@style/textColorSpecial"
android:textSize="12sp"
android:visibility="gone" />

<!-- DATE -->
<TextView
android:id="@+id/card_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="7dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="8dp"
android:ellipsize="end"
android:lines="1"
android:scrollHorizontally="true"
android:text="(Timestamp)"
style="@style/textColor"
android:textSize="20sp"
android:textStyle="bold"/>

<!-- COST -->
<TextView
android:id="@+id/card_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="36sp"
android:text="(Cost)"
style="@style/textColor"
android:textSize="16sp"/>

<!-- FUEL TYPE -->
<TextView
android:id="@+id/card_filled_petrol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="60sp"
android:text="(Petrol Filled, Gas Station)"
style="@style/textColorSpecial"
android:textSize="12sp"/>

<!-- LT PER 100 -->
<TextView
android:id="@+id/card_lt_per_100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="80sp"
android:text="(x per 100 km)"
style="@style/textColorSpecial"
android:textSize="12sp"/>

<!-- BUTTONS -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/card_buttonDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="65dp"
android:layout_marginBottom="21dp"
android:clickable="true"
android:scaleX="0.8"
android:scaleY="0.8"
app:backgroundTint="#FF0000"
app:srcCompat="@android:drawable/ic_menu_delete" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/card_buttonEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="9dp"
android:layout_marginBottom="21dp"
android:clickable="true"
android:scaleX="0.8"
android:scaleY="0.8"
app:backgroundTint="#3B89FF"
app:srcCompat="@android:drawable/ic_menu_edit" />

</RelativeLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
android:id="@+id/historyFragment_linearLayoutScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
android:orientation="vertical">
</LinearLayout>

</ScrollView>

<TextView
Expand Down

0 comments on commit 216ac6f

Please sign in to comment.