Skip to content

Commit

Permalink
Add medium widget layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sruusk committed Dec 13, 2024
1 parent 530c2a2 commit 12ea09b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Sähkön tuntihinnat Suomessa.
- [ ] Laske hintaan ALV ja myyjän marginaali

## Ympäristömuuttujat
Voit asettaa ympäristömuuttujat ajaessa flutter run/build komentoja käyttämällä `--dart-define key=value` parametria.
- `ENTSOE_TOKEN` - ENTSO-E Web API Security Token

## Kuvakaappaus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ class WidgetProvider : HomeWidgetProvider() {
setTextViewText(R.id.text_time, time)
}

val mediumView = RemoteViews(context.packageName, R.layout.widget_medium).apply {
val prices = Array<String>(3) { i -> widgetData.getString("price$i", "0.00").toString() }
val time = widgetData.getString("time", "12.12.").toString()
val times = Array<String>(3) { i -> widgetData.getString("time$i", "25-26").toString() }
setTextViewText(R.id.text_price1, prices[0])
setTextViewText(R.id.text_price2, prices[1])
setTextViewText(R.id.text_time, time.split(" ")[0]) // Only date
setTextViewText(R.id.text_time1, times[0])
setTextViewText(R.id.text_time2, times[1])
}

val wideView = RemoteViews(context.packageName, R.layout.widget_wide).apply {
val prices = Array<String>(3) { i -> widgetData.getString("price$i", "0.00").toString() }
val time = widgetData.getString("time", "12.12.").toString()
Expand All @@ -41,7 +52,8 @@ class WidgetProvider : HomeWidgetProvider() {

val viewMapping: Map<SizeF, RemoteViews> = mapOf(
SizeF(40f, 100f) to narrowView,
SizeF(100f, 100f) to wideView
SizeF(120f, 100f) to mediumView,
SizeF(180f, 100f) to wideView
)

val remoteViews = RemoteViews(viewMapping)
Expand Down
87 changes: 87 additions & 0 deletions android/app/src/main/res/layout/widget_medium.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#111c2e"
android:gravity="top"
android:orientation="vertical"
android:padding="8dp"
android:minWidth="40dp"
android:backgroundTintMode="add" android:dividerPadding="1dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="bottom|end">

<TextView
android:text="Sähkön hinta"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView"
android:textAppearance="@android:style/TextAppearance.Material.Small"
android:textColor="#E8E8E8" android:layout_gravity="bottom"/>
<TextView
android:id="@+id/text_time"
android:text="12.12."
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="12sp" android:textColor="#E8E8E8"
android:gravity="end" android:layout_gravity="bottom|end" android:layout_marginBottom="1dp"/>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginTop="5dp">
<TextView
android:text="07-08"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/text_time1" android:textColor="#FFFFFF"
android:layout_weight="1" android:textSize="12sp" android:gravity="center"/>
<TextView
android:text="07-08"
android:layout_width="0dp"
android:layout_height="wrap_content" android:id="@+id/text_time2" android:textColor="#FFFFFF"
android:layout_weight="1" android:textSize="12sp" android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_price1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="23.45"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textAllCaps="false"
android:gravity="center"
android:textColor="#E8E8E8"
android:textSize="16sp"
android:textStyle="bold" android:layout_weight="1"/>
<TextView
android:id="@+id/text_price2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="23.45"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textAllCaps="false"
android:gravity="center"
android:textColor="#E8E8E8"
android:textSize="16sp"
android:textStyle="bold" android:layout_weight="1"/>
</LinearLayout>
<TextView
android:text="snt/kWh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="end"
android:clickable="false"
android:autoText="false"
android:width="45dip"
android:textColor="#E8E8E8"
android:textSize="10sp"
android:layout_gravity="bottom"
android:textStyle="bold"
android:textAlignment="viewEnd"/>
</LinearLayout>
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/widget_wide.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:textColor="#E8E8E8" android:layout_gravity="bottom"/>
<TextView
android:id="@+id/text_time"
android:text="12.12. 17-18"
android:text="12.12."
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="12sp" android:textColor="#E8E8E8"
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/xml/widget.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget"
android:minWidth="80dp"
android:minWidth="40dp"
android:minHeight="40dp"
android:targetCellHeight="1"
android:targetCellWidth="2"
Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:home_widget/home_widget.dart';
import 'package:sahkohinta/pages/home_page.dart';
import 'package:sahkohinta/utils/preferences.dart';
Expand All @@ -10,7 +9,6 @@ import 'package:sahkohinta/utils/home_widget.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized(); // Ensure the binding is initialized
await dotenv.load(fileName: ".env");
await AndroidAlarmManager.initialize();
await AndroidAlarmManager.oneShot(const Duration(seconds: 5), 1, updateWidget);
HomeWidget.updateWidget(name: 'WidgetProvider');
Expand Down
5 changes: 2 additions & 3 deletions lib/utils/api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xml/xml.dart';
Expand All @@ -10,7 +9,8 @@ String getApiUrl() {
DateTime tomorrow = now.add(const Duration(days: 2));
final String start = "${startTime.year}${startTime.month}${startTime.day}0000";
final String end = "${tomorrow.year}${tomorrow.month}${tomorrow.day}2359";
return "https://web-api.tp.entsoe.eu/api?documentType=A44&out_Domain=10YFI-1--------U&in_Domain=10YFI-1--------U&periodStart=$start&periodEnd=$end&securityToken=${dotenv.env['ENTSOE_TOKEN']}";
const String token = String.fromEnvironment('ENTSOE_TOKEN');
return "https://web-api.tp.entsoe.eu/api?documentType=A44&out_Domain=10YFI-1--------U&in_Domain=10YFI-1--------U&periodStart=$start&periodEnd=$end&securityToken=$token";
}

class ElectricityApi {
Expand Down Expand Up @@ -42,7 +42,6 @@ class ElectricityApi {

print('Fetching prices');

if(!dotenv.isInitialized) await dotenv.load(fileName: ".env");
final response = await http.get(Uri.parse(getApiUrl()));
final List<ElectricityPrice> prices = [];
final document = XmlDocument.parse(response.body);
Expand Down
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_dotenv:
dependency: "direct main"
description:
name: flutter_dotenv
sha256: b7c7be5cd9f6ef7a78429cabd2774d3c4af50e79cb2b7593e3d5d763ef95c61b
url: "https://pub.dev"
source: hosted
version: "5.2.1"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
2 changes: 0 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ dependencies:
mrx_charts: ^0.1.3
home_widget: ^0.7.0
android_alarm_manager_plus: ^4.0.4
flutter_dotenv: ^5.2.1

dev_dependencies:
flutter_test:
Expand All @@ -68,7 +67,6 @@ flutter:
# To add assets to your application, add an assets section, like this:
assets:
- assets/lang/
- .env

# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/to/resolution-aware-images
Expand Down

0 comments on commit 12ea09b

Please sign in to comment.