Skip to content

Commit f3dca49

Browse files
committed
refactor: stocks view
1 parent 1cf0f91 commit f3dca49

2 files changed

Lines changed: 99 additions & 67 deletions

File tree

app/src/main/java/com/zekecode/akira_financialtracker/ui/viewmodels/StocksViewModel.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import com.zekecode.akira_financialtracker.data.remote.models.DailyData
1414
import dagger.hilt.android.lifecycle.HiltViewModel
1515
import kotlinx.coroutines.flow.MutableStateFlow
1616
import kotlinx.coroutines.launch
17+
import java.time.LocalDate
18+
import java.time.LocalDateTime
19+
import java.time.format.DateTimeFormatter
1720
import javax.inject.Inject
1821

1922
@HiltViewModel
@@ -82,14 +85,23 @@ class StocksViewModel @Inject constructor(
8285
}
8386

8487
private fun extractChartData(timeSeries: Map<String, DailyData>): List<Pair<String, Double>> {
88+
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
89+
val outputFormatter = DateTimeFormatter.ofPattern("dd/MM")
90+
8591
return timeSeries.entries
8692
.sortedBy { it.key }
8793
.mapNotNull { entry ->
88-
val date = entry.key
89-
val closePrice = entry.value.close.toDoubleOrNull()
90-
if (closePrice != null) {
91-
date to closePrice
92-
} else null
94+
try {
95+
val date = LocalDate.parse(entry.key, dateFormatter)
96+
val formattedDate = date.format(outputFormatter)
97+
val closePrice = entry.value.close.toDoubleOrNull()
98+
if (closePrice != null) {
99+
formattedDate to closePrice
100+
} else null
101+
} catch (e: Exception) {
102+
Log.e("StocksViewModel", "Error parsing date: ${entry.key}", e)
103+
null
104+
}
93105
}
94106
}
95107

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<FrameLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
45
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
xmlns:app="http://schemas.android.com/apk/res-auto">
6+
android:layout_height="match_parent">
77

88
<!-- Scrollable content -->
99
<ScrollView
1010
android:layout_width="match_parent"
11-
android:layout_height="match_parent">
11+
android:layout_height="match_parent"
12+
android:fillViewport="true">
1213

1314
<!-- Main content layout -->
1415
<LinearLayout
@@ -17,8 +18,8 @@
1718
android:layout_height="wrap_content"
1819
android:orientation="vertical">
1920

20-
<!-- Header -->
21-
<TextView
21+
<!-- Header (outside of padding) -->
22+
<com.google.android.material.textview.MaterialTextView
2223
android:layout_width="match_parent"
2324
android:layout_height="wrap_content"
2425
android:text="@string/stocks_header_text"
@@ -29,74 +30,93 @@
2930
android:background="@color/secondary_background"
3031
android:gravity="center" />
3132

32-
<!-- Search Bar -->
33-
<com.google.android.material.textfield.TextInputLayout
34-
android:layout_width="match_parent"
35-
android:layout_height="55dp"
36-
android:layout_marginTop="@dimen/spacing_m"
37-
android:layout_marginHorizontal="@dimen/spacing_m"
38-
app:boxBackgroundColor="@color/secondary_background"
39-
android:textColorHint="@color/secondary_text">
40-
41-
<AutoCompleteTextView
42-
android:id="@+id/stock_search"
43-
android:layout_width="match_parent"
44-
android:layout_height="match_parent"
45-
android:inputType="text"
46-
android:textSize="@dimen/text_s"
47-
android:drawableStart="@drawable/ic_search"
48-
android:drawableTint="@color/search_symbol"
49-
android:drawablePadding="@dimen/spacing_s"
50-
android:paddingStart="@dimen/spacing_m"
51-
android:paddingEnd="@dimen/spacing_m"
52-
android:hint="@string/search_hint"
53-
android:completionThreshold="1" />
54-
</com.google.android.material.textfield.TextInputLayout>
55-
56-
<!-- Stock Information -->
33+
<!-- Content container with padding -->
5734
<LinearLayout
5835
android:layout_width="match_parent"
5936
android:layout_height="wrap_content"
6037
android:orientation="vertical"
61-
android:padding="@dimen/spacing_m"
62-
android:layout_marginTop="@dimen/spacing_m"
63-
android:elevation="4dp">
38+
android:padding="@dimen/spacing_m">
6439

65-
<!-- Stock Name and Symbol -->
66-
<TextView
67-
android:id="@+id/stock_header"
68-
android:layout_width="wrap_content"
69-
android:layout_height="wrap_content"
70-
android:text=""
71-
android:textColor="@color/primary_text"
72-
android:textSize="@dimen/text_m"
73-
android:textStyle="bold"
74-
android:layout_gravity="center_horizontal" />
40+
<!-- Search Bar -->
41+
<com.google.android.material.textfield.TextInputLayout
42+
android:layout_width="match_parent"
43+
android:layout_height="55dp"
44+
android:layout_marginTop="@dimen/spacing_m"
45+
android:layout_marginBottom="@dimen/spacing_m"
46+
app:boxBackgroundColor="@color/secondary_background"
47+
android:textColorHint="@color/secondary_text">
7548

76-
<!-- Stock Price and Change -->
77-
<TextView
78-
android:id="@+id/stock_price"
79-
android:layout_width="wrap_content"
49+
<AutoCompleteTextView
50+
android:id="@+id/stock_search"
51+
android:layout_width="match_parent"
52+
android:layout_height="match_parent"
53+
android:inputType="text"
54+
android:textSize="@dimen/text_s"
55+
android:drawableStart="@drawable/ic_search"
56+
android:drawableTint="@color/search_symbol"
57+
android:drawablePadding="@dimen/spacing_s"
58+
android:paddingStart="@dimen/spacing_m"
59+
android:paddingEnd="@dimen/spacing_m"
60+
android:hint="@string/search_hint"
61+
android:completionThreshold="1" />
62+
</com.google.android.material.textfield.TextInputLayout>
63+
64+
<!-- Stock Information Card -->
65+
<com.google.android.material.card.MaterialCardView
66+
android:layout_width="match_parent"
8067
android:layout_height="wrap_content"
81-
android:text=""
82-
android:textColor="@color/accent_green"
83-
android:textSize="16sp"
84-
android:layout_gravity="center_horizontal"
85-
android:paddingTop="@dimen/spacing_s" />
86-
</LinearLayout>
68+
android:layout_marginTop="@dimen/spacing_s"
69+
android:layout_marginBottom="@dimen/spacing_l"
70+
app:cardElevation="4dp"
71+
app:strokeColor="@color/tertiary_background"
72+
app:cardCornerRadius="8dp"
73+
app:cardBackgroundColor="@color/secondary_background">
8774

88-
<!-- Stock Graph -->
89-
<com.patrykandpatrick.vico.views.cartesian.CartesianChartView
90-
android:id="@+id/stocks_chart_view"
91-
android:layout_width="match_parent"
92-
android:layout_height="220dp"
93-
app:scrollEnabled="true"
94-
app:chartStyle="@style/StocksChartStyle" />
75+
<LinearLayout
76+
android:layout_width="match_parent"
77+
android:layout_height="wrap_content"
78+
android:orientation="vertical">
9579

80+
<!-- Stock Name and Symbol -->
81+
<TextView
82+
android:id="@+id/stock_header"
83+
android:layout_width="match_parent"
84+
android:layout_height="wrap_content"
85+
android:text=""
86+
android:layout_marginTop="@dimen/spacing_s"
87+
android:textColor="@color/primary_text"
88+
android:textSize="@dimen/text_s"
89+
android:textStyle="bold"
90+
android:gravity="center_horizontal"
91+
android:layout_marginBottom="@dimen/spacing_s" />
92+
93+
<!-- Stock Price -->
94+
<TextView
95+
android:id="@+id/stock_price"
96+
android:layout_width="match_parent"
97+
android:layout_height="wrap_content"
98+
android:text=""
99+
android:textColor="@color/accent_green"
100+
android:textSize="@dimen/text_s"
101+
android:gravity="center_horizontal"
102+
android:layout_marginBottom="@dimen/spacing_m" />
103+
104+
<!-- Stock Graph -->
105+
<com.patrykandpatrick.vico.views.cartesian.CartesianChartView
106+
android:id="@+id/stocks_chart_view"
107+
android:layout_width="match_parent"
108+
android:layout_height="220dp"
109+
android:layout_gravity="center_horizontal"
110+
android:layout_marginHorizontal="@dimen/spacing_s"
111+
app:scrollEnabled="true"
112+
app:chartStyle="@style/StocksChartStyle" />
113+
</LinearLayout>
114+
</com.google.android.material.card.MaterialCardView>
115+
</LinearLayout>
96116
</LinearLayout>
97117
</ScrollView>
98118

99-
<!-- Disabled message overlay (hidden if API key is set) -->
119+
<!-- Disabled message overlay -->
100120
<TextView
101121
android:id="@+id/disabled_message"
102122
android:layout_width="match_parent"
@@ -107,4 +127,4 @@
107127
android:textSize="18sp"
108128
android:textColor="@color/primary_text"
109129
android:visibility="gone" />
110-
</FrameLayout>
130+
</FrameLayout>

0 commit comments

Comments
 (0)