Skip to content

Replace provider with watch_it, update deps and re-organize #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: [master]

env:
FLUTTER_VERSION: '3.16.x'
FLUTTER_VERSION: '3.19.x'

jobs:
analyze:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ app.*.map.json
/android/app/release

.vscode/settings.json
assets/apikey.json
33 changes: 8 additions & 25 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
avoid_print: false # Uncomment to disable the `avoid_print` rule
prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
avoid_print: true
cancel_subscriptions: true
prefer_single_quotes: true
prefer_const_constructors: true
prefer_const_declarations: true
require_trailing_commas: true

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
use_super_parameters: true
close_sinks: true
prefer_relative_imports: true
2 changes: 1 addition & 1 deletion assets/apikey.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"apiKey": "YOUR_OPEN_WEATHER_API_KEY_HERE"
"apiKey": ""
}
73 changes: 23 additions & 50 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import 'dart:convert';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:pulse/app/weather_model.dart';
import 'package:pulse/app/weather_page.dart';
import 'package:geocoding_resolver/geocoding_resolver.dart';
import 'package:open_weather_client/open_weather.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/yaru.dart';
import 'package:yaru_widgets/yaru_widgets.dart';

import 'src/app/app.dart';
import 'src/weather/weather_model.dart';

Future<void> main() async {
await YaruWindowTitleBar.ensureInitialized();

WidgetsFlutterBinding.ensureInitialized();

final apiKey = await loadApiKey();

if (apiKey != null) {
if (apiKey != null && apiKey.isNotEmpty) {
di.registerSingleton(OpenWeather(apiKey: apiKey));
di.registerSingleton(GeoCoder());
final weatherModel =
WeatherModel(openWeather: di<OpenWeather>(), geoCoder: di<GeoCoder>());
await weatherModel.init();
di.registerSingleton(weatherModel);

runApp(const App());
} else {
runApp(
MyApp.create(
apiKey,
MaterialApp(
theme: yaruLight,
home: const Scaffold(
body: Center(
child: Text('NO VALI API KEY FOUND'),
),
),
),
);
}
Expand All @@ -30,41 +41,3 @@ Future<String?> loadApiKey() async {
final json = jsonDecode(source);
return json['apiKey'] as String;
}

class MyApp extends StatefulWidget {
const MyApp({super.key, required this.apiKey});

final String apiKey;

static Widget create(String apiKey) {
return ChangeNotifierProvider<WeatherModel>(
create: (context) => WeatherModel(apiKey)..init(),
child: MyApp(apiKey: apiKey),
);
}

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Weather',
debugShowCheckedModeBanner: false,
theme: yaruLight,
darkTheme: yaruDark,
home: const WeatherPage(),
scrollBehavior: const MaterialScrollBehavior().copyWith(
dragDevices: {
PointerDeviceKind.mouse,
PointerDeviceKind.touch,
PointerDeviceKind.stylus,
PointerDeviceKind.unknown,
PointerDeviceKind.trackpad,
},
),
);
}
}
30 changes: 30 additions & 0 deletions lib/src/app/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:yaru/yaru.dart';

import '../../weather.dart';

class App extends StatelessWidget {
const App({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Weather',
debugShowCheckedModeBanner: false,
theme: yaruLight,
darkTheme: yaruDark,
home: const WeatherPage(),
scrollBehavior: const MaterialScrollBehavior().copyWith(
dragDevices: {
PointerDeviceKind.mouse,
PointerDeviceKind.touch,
PointerDeviceKind.stylus,
PointerDeviceKind.unknown,
PointerDeviceKind.trackpad,
},
),
);
}
}
4 changes: 2 additions & 2 deletions lib/app/utils.dart → lib/src/weather/utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_weather_bg_null_safety/utils/weather_type.dart';
import 'package:open_weather_client/models/weather_data.dart';
import 'package:pulse/weather_data_x.dart';
import 'package:yaru_icons/yaru_icons.dart';
import 'weather_data_x.dart';
import 'package:yaru/icons.dart';

Color getColor(WeatherData weatherData) {
final hour = DateTime.fromMillisecondsSinceEpoch(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:pulse/app/weather_model.dart';
import '../weather_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:yaru_icons/yaru_icons.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/icons.dart';

class CitySearchField extends StatefulWidget {
const CitySearchField({
Expand Down Expand Up @@ -29,7 +29,7 @@ class _CitySearchFieldState extends State<CitySearchField> {

@override
Widget build(BuildContext context) {
final model = context.watch<WeatherModel>();
final model = di<WeatherModel>();
var textField = TextField(
onSubmitted: (value) => model.init(cityName: _controller.text),
controller: _controller,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_weather_bg_null_safety/bg/weather_bg.dart';
import 'package:flutter_weather_bg_null_safety/flutter_weather_bg.dart';
import 'package:open_weather_client/models/weather_data.dart';
import 'package:pulse/app/utils.dart';
import 'package:pulse/string_x.dart';
import 'package:pulse/weather_data_x.dart';
import '../utils.dart';
import '../../../string_x.dart';
import '../weather_data_x.dart';

class ForecastTile extends StatefulWidget {
final List<WeatherData> data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_weather_bg_null_safety/bg/weather_bg.dart';
import 'package:flutter_weather_bg_null_safety/flutter_weather_bg.dart';
import 'package:open_weather_client/models/weather_data.dart';
import 'package:pulse/app/utils.dart';
import 'package:pulse/string_x.dart';
import 'package:pulse/weather_data_x.dart';
import '../utils.dart';
import '../../../string_x.dart';
import '../weather_data_x.dart';

class TodayTile extends StatelessWidget {
final WeatherData data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:open_weather_client/models/weather_data.dart';
import 'package:pulse/weekday.dart';
import 'weekday.dart';

extension WeatherDataX on WeatherData {
String get currentTemperature => '${temperature.currentTemperature} °C';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:geocoding_resolver/geocoding_resolver.dart';
import 'package:geolocator/geolocator.dart';
import 'package:open_weather_client/open_weather.dart';
import 'package:pulse/weather_data_x.dart';
import 'package:pulse/weekday.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

class WeatherModel extends SafeChangeNotifier {
WeatherModel(String apiKey)
: _openWeather = OpenWeather(apiKey: apiKey),
geoCoder = GeoCoder();
import 'weather_data_x.dart';
import 'weekday.dart';

GeoCoder geoCoder;
class WeatherModel extends SafeChangeNotifier {
WeatherModel({required OpenWeather openWeather, required GeoCoder geoCoder})
: _openWeather = openWeather,
_geoCoder = geoCoder;

final GeoCoder _geoCoder;
final OpenWeather _openWeather;
final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;

Expand All @@ -26,7 +26,7 @@ class WeatherModel extends SafeChangeNotifier {
return '';
}

Address address = await geoCoder.getAddressFromLatLng(
Address address = await _geoCoder.getAddressFromLatLng(
latitude: _position!.latitude,
longitude: _position!.longitude,
);
Expand Down
20 changes: 10 additions & 10 deletions lib/app/weather_page.dart → lib/src/weather/weather_page.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:flutter/material.dart';
import 'package:open_weather_client/models/weather_data.dart';
import 'package:provider/provider.dart';
import 'package:pulse/app/city_search_field.dart';
import 'package:pulse/app/forecast_tile.dart';
import 'package:pulse/app/today_tile.dart';
import 'package:pulse/app/utils.dart';
import 'package:pulse/app/weather_model.dart';
import 'package:pulse/weather_data_x.dart';
import 'package:yaru_widgets/yaru_widgets.dart';
import 'view/city_search_field.dart';
import 'view/forecast_tile.dart';
import 'view/today_tile.dart';
import 'utils.dart';
import 'weather_model.dart';
import 'weather_data_x.dart';
import 'package:watch_it/watch_it.dart';
import 'package:yaru/widgets.dart';

class WeatherPage extends StatelessWidget {
class WeatherPage extends StatelessWidget with WatchItMixin {
const WeatherPage({super.key});

@override
Widget build(BuildContext context) {
final model = context.watch<WeatherModel>();
final model = watchIt<WeatherModel>();
final mq = MediaQuery.of(context);
final theme = Theme.of(context);
final light = theme.brightness == Brightness.light;
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions lib/weather.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/weather/weather_page.dart';
Loading