Skip to content

Commit d4013cb

Browse files
BlackDizelAlexandr MotorinAlexandrradomir9720
authored
chore: migrate to new battery block packets format (#77)
* feat(hardware count): now the GetHardwareCountBloc will contain the logic to obtain and to store the hardware count * fix(web): do not import flutter_libserialport when building for web * refactor(DataSourceIncomingPackage): reduce number of identical models by using multiple parameter ids in one model * fix(NavigatorScreen): error when EnableFastAccessDialog is popped because of unspecified return type * chore: renamed battery level to battery percent everywhere in the project for consistency * fix(overlay): overlay enabled state not loaded from storage * build(android): migrated to declarative graddle pluggins apply * refactor(DemoDataSource): made packet handling more declarative * test: update battery power packet id --------- Co-authored-by: Alexandr Motorin <[email protected]> Co-authored-by: Alexandr <[email protected]> Co-authored-by: Radomir Epur <[email protected]>
1 parent 082e4d1 commit d4013cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1744
-2439
lines changed

android/app/build.gradle

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
plugins {
2+
id "com.android.application"
3+
id "kotlin-android"
4+
id "dev.flutter.flutter-gradle-plugin"
5+
id "com.google.gms.google-services"
6+
id "com.google.firebase.crashlytics"
7+
}
8+
19
def localProperties = new Properties()
210
def localPropertiesFile = rootProject.file('local.properties')
311
if (localPropertiesFile.exists()) {
@@ -6,11 +14,6 @@ if (localPropertiesFile.exists()) {
614
}
715
}
816

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
13-
1417
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1518
if (flutterVersionCode == null) {
1619
flutterVersionCode = '1'
@@ -27,14 +30,6 @@ if (keystorePropertiesFile.exists()) {
2730
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
2831
}
2932

30-
apply plugin: 'com.android.application'
31-
// START: FlutterFire Configuration
32-
apply plugin: 'com.google.gms.google-services'
33-
apply plugin: 'com.google.firebase.crashlytics'
34-
// END: FlutterFire Configuration
35-
apply plugin: 'kotlin-android'
36-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
37-
3833
android {
3934
compileSdkVersion 34
4035

@@ -116,7 +111,3 @@ android {
116111
flutter {
117112
source '../..'
118113
}
119-
120-
dependencies {
121-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
122-
}

android/build.gradle

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
buildscript {
2-
ext.kotlin_version = '1.9.10'
3-
repositories {
4-
google()
5-
mavenCentral()
6-
}
7-
8-
dependencies {
9-
classpath 'com.android.tools.build:gradle:7.1.2'
10-
// START: FlutterFire Configuration
11-
classpath 'com.google.gms:google-services:4.3.10'
12-
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
13-
// END: FlutterFire Configuration
14-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15-
}
16-
}
17-
181
allprojects {
192
repositories {
203
google()

android/settings.gradle

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
4-
def properties = new Properties()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
511

6-
assert localPropertiesFile.exists()
7-
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
17+
}
818

9-
def flutterSdkPath = properties.getProperty("flutter.sdk")
10-
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
11-
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
19+
plugins {
20+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21+
id "com.android.application" version "7.1.2" apply false
22+
id "org.jetbrains.kotlin.android" version "1.9.10" apply false
23+
id "com.google.gms.google-services" version "4.3.10" apply false
24+
id "com.google.firebase.crashlytics" version "2.8.1" apply false
25+
}
26+
27+
include ":app"

lib/app/helpers/platform.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'dart:io' as io;
2+
3+
import 'package:flutter/foundation.dart';
4+
5+
final class Platform {
6+
static bool get isWeb => kIsWeb;
7+
8+
static bool get isAndroid => !isWeb && io.Platform.isAndroid;
9+
10+
static bool get isIOS => !isWeb && io.Platform.isIOS;
11+
12+
static bool get isMacOS => !isWeb && io.Platform.isMacOS;
13+
14+
static bool get isWindows => !isWeb && io.Platform.isWindows;
15+
16+
static bool get isLinux => !isWeb && io.Platform.isLinux;
17+
18+
static bool get isFuchsia => !isWeb && io.Platform.isFuchsia;
19+
}

lib/app/overlay.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class _OverlayGeneralStatisticsState extends State<OverlayGeneralStatistics> {
2424
late final StreamSubscription<void> sub;
2525

2626
final notifier = ValueNotifier<GeneralDataState>(
27-
const GeneralDataState.initial(),
27+
// initial state does not play any role here
28+
GeneralDataState.initial(hardwareCount: HardwareCount.all(1)),
2829
);
2930

3031
@override
@@ -101,8 +102,8 @@ class _OverlayGeneralStatisticsState extends State<OverlayGeneralStatistics> {
101102
child: ValueListenableBuilder<GeneralDataState>(
102103
valueListenable: notifier,
103104
builder: (context, value, child) {
104-
final power = value.power;
105-
final batteryLevel = value.batteryLevel;
105+
final power = value.mergedPower;
106+
final batteryPercent = value.mergedBatteryPercent;
106107
final odometer = value.odometer;
107108

108109
return Column(
@@ -171,7 +172,9 @@ class _OverlayGeneralStatisticsState extends State<OverlayGeneralStatistics> {
171172
mainAxisAlignment:
172173
MainAxisAlignment.spaceBetween,
173174
children: [
174-
BatteryLevelStatisticItem(item: batteryLevel),
175+
BatteryPercentStatisticItem(
176+
item: batteryPercent,
177+
),
175178
OdometerStatisticItem(item: odometer),
176179
PowerStatisticItem(item: power),
177180
],
@@ -197,6 +200,7 @@ class OverlayManager extends StatefulWidget {
197200

198201
@protected
199202
final Widget child;
203+
200204
@override
201205
State<OverlayManager> createState() => _OverlayManagerState();
202206
}

lib/app/scopes/flows/main_scope.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ class MainScope extends SingleChildStatelessWidget {
4646
),
4747

4848
BlocProvider(
49-
create: (context) => OverlayBloc(storage: GetIt.I()),
49+
create: (context) {
50+
return OverlayBloc(storage: GetIt.I())
51+
..add(const OverlayEvent.load());
52+
},
5053
lazy: false,
5154
),
5255

@@ -57,6 +60,7 @@ class MainScope extends SingleChildStatelessWidget {
5760
BlocProvider(
5861
create: (context) => LEDConfigsCubit(storage: context.read()),
5962
),
63+
BlocProvider(create: (context) => GetHardwareCountBloc()),
6064

6165
if (GetIt.I.get<Environment>().isDev) ...[
6266
BlocProvider(

lib/app/scopes/flows/select_data_source_scope.dart

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import 'package:auto_route/auto_route.dart';
2+
import 'package:flutter/foundation.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter_bloc/flutter_bloc.dart';
45
import 'package:get_it/get_it.dart';
56
import 'package:pixel_app_flutter/bootstrap.dart';
67
import 'package:pixel_app_flutter/data/services/data_source/bluetooth_data_source.dart';
78
import 'package:pixel_app_flutter/data/services/data_source/demo_data_source.dart';
8-
import 'package:pixel_app_flutter/data/services/data_source/usb_data_source.dart';
9+
import 'package:pixel_app_flutter/data/services/data_source/usb_data_source.dart'
10+
if (dart.library.js) 'package:pixel_app_flutter/data/services/data_source/stubs/usb_data_source.dart';
911
import 'package:pixel_app_flutter/data/services/data_source/usb_data_source_android.dart';
1012
import 'package:pixel_app_flutter/domain/data_source/data_source.dart';
1113
import 'package:pixel_app_flutter/l10n/l10n.dart';
1214
import 'package:pixel_app_flutter/presentation/app/icons.dart';
1315
import 'package:pixel_app_flutter/presentation/routes/main_router.dart';
1416
import 'package:provider/provider.dart';
17+
import 'package:re_seedwork/re_seedwork.dart';
1518
import 'package:re_widgets/re_widgets.dart';
1619

1720
@RoutePage(name: 'SelectDataSourceFlow')
@@ -26,16 +29,20 @@ class SelectDataSourceScope extends StatelessWidget
2629
final authorizationState =
2730
context.watch<DataSourceAuthorizationCubit>().state;
2831
final connectState = context.watch<DataSourceConnectBloc>().state;
32+
final hardwareCountState = context.watch<GetHardwareCountBloc>().state;
2933

3034
return AutoRouter.declarative(
3135
routes: (handler) {
3236
return [
3337
const SelectDataSourceGeneralFlow(),
34-
if (authorizationState.isFailure || connectState.isFailure)
38+
if (authorizationState.isFailure ||
39+
connectState.isFailure ||
40+
hardwareCountState.isFailure)
3541
...[]
3642
else if (isInitial ||
3743
authorizationState.isLoading ||
38-
connectState.isLoading)
44+
connectState.isLoading ||
45+
hardwareCountState.isLoading)
3946
const NonPopableLoadingRoute()
4047
else
4148
...authorizationState.maybeWhen(
@@ -65,6 +72,7 @@ class SelectDataSourceScope extends StatelessWidget
6572
create: (context) {
6673
final devToolsParamsStorage =
6774
context.read<DeveloperToolsParametersStorage>();
75+
final getHardwareCountBloc = context.read<GetHardwareCountBloc>();
6876
final env = context.read<Environment>();
6977

7078
return [
@@ -87,7 +95,7 @@ class SelectDataSourceScope extends StatelessWidget
8795
permissionRequestCallback: GetIt.I(),
8896
),
8997
),
90-
] else if (!platform.isIos)
98+
] else if (!platform.isIos && !kIsWeb)
9199
DataSourceEntity(
92100
key: USBDataSource.kKey,
93101
title: platform.isMacOS
@@ -114,6 +122,12 @@ class SelectDataSourceScope extends StatelessWidget
114122
updatePeriodMillis: () {
115123
return devToolsParamsStorage.data.requestsPeriodInMillis;
116124
},
125+
hardwareCount: () {
126+
return getHardwareCountBloc.state.value.toNullable
127+
// When this callback is called,
128+
// the hardware count should be already fetched
129+
.checkNotNull('hardwareCount');
130+
},
117131
);
118132
},
119133
),
@@ -145,6 +159,17 @@ class SelectDataSourceScope extends StatelessWidget
145159
create: (context) => DataSourceAuthorizationCubit(
146160
dataSourceStorage: context.read(),
147161
serialNumberStorage: context.read(),
162+
shouldWriteDataSourceCallback: () {
163+
context.read<GetHardwareCountBloc>().add(
164+
const GetHardwareCountEvent.get(),
165+
);
166+
return context
167+
.read<GetHardwareCountBloc>()
168+
.stream
169+
.where((state) => state.isExecuted)
170+
.map<bool>((state) => state.isSuccess)
171+
.first;
172+
},
148173
),
149174
),
150175

@@ -182,6 +207,12 @@ class SelectDataSourceScope extends StatelessWidget
182207
);
183208
},
184209
),
210+
BlocListener<GetHardwareCountBloc, GetHardwareCountState>(
211+
listenWhen: (previous, current) => current.isFailure,
212+
listener: (context, state) {
213+
context.showSnackBar(l10n.errorGettingHardwareCountMessage);
214+
},
215+
),
185216
],
186217
child: this,
187218
);

lib/app/scopes/flows/selected_data_source_scope.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:pixel_app_flutter/l10n/l10n.dart';
1515
import 'package:pixel_app_flutter/presentation/screens/common/loading_screen.dart';
1616
import 'package:pixel_app_flutter/presentation/widgets/app/atoms/gradient_scaffold.dart';
1717
import 'package:provider/provider.dart';
18+
import 'package:re_seedwork/re_seedwork.dart';
1819
import 'package:re_widgets/re_widgets.dart';
1920

2021
@RoutePage(name: 'SelectedDataSourceFlow')
@@ -37,10 +38,7 @@ class SelectedDataSourceScope extends AutoRouter {
3738

3839
return BlocBuilder<DataSourceCubit, DataSourceState>(
3940
builder: (context, state) {
40-
final dswa = state.ds.when(
41-
presented: (d) => d,
42-
undefined: () => null,
43-
);
41+
final dswa = state.ds.toNullable;
4442

4543
if (dswa == null) {
4644
return GradientScaffold(body: const SizedBox.shrink());
@@ -50,6 +48,16 @@ class SelectedDataSourceScope extends AutoRouter {
5048
key: ValueKey('${dswa.dataSource.key}_${dswa.address}'),
5149
providers: [
5250
Provider<DataSource>.value(value: dswa.dataSource),
51+
Provider<HardwareCount>.value(
52+
value: context
53+
.read<GetHardwareCountBloc>()
54+
.state
55+
.value
56+
.toNullable
57+
// should not be null at this point
58+
// higher level should handle this case
59+
.checkNotNull('Hardware count'),
60+
),
5361
Provider<AppsService>(create: (context) => GetIt.I()),
5462

5563
// storages
@@ -167,6 +175,7 @@ class SelectedDataSourceScope extends AutoRouter {
167175
GeneralDataCubit.kDefaultSubscribeParameters,
168176
);
169177
return GeneralDataCubit(
178+
hardwareCount: context.read<HardwareCount>(),
170179
dataSource: context.read(),
171180
);
172181
},

lib/app/scopes/screens/charging_screen_wrapper.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ChargingScreenWrapper extends StatelessWidget
2626
.subscribeTo(BatteryDataCubit.kDefaultSubscribeParameters);
2727
return BatteryDataCubit(
2828
dataSource: context.read(),
29+
hardwareCount: context.read<HardwareCount>(),
2930
);
3031
},
3132
),

lib/bootstrap.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
// https://opensource.org/licenses/MIT.
77

88
import 'dart:async';
9-
import 'dart:io';
109

1110
import 'package:bloc/bloc.dart';
1211
import 'package:firebase_analytics/firebase_analytics.dart';
1312
import 'package:firebase_core/firebase_core.dart';
1413
import 'package:flutter/foundation.dart';
1514
import 'package:flutter/material.dart';
1615
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
17-
import 'package:flutter_libserialport/flutter_libserialport.dart';
16+
import 'package:flutter_libserialport/flutter_libserialport.dart'
17+
if (dart.library.js) 'package:pixel_app_flutter/data/services/data_source/stubs/usb_data_source.dart';
1818
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
1919
import 'package:get_it/get_it.dart';
2020
import 'package:injectable/injectable.dart';
@@ -24,12 +24,14 @@ import 'package:permission_handler/permission_handler.dart';
2424
import 'package:pixel_app_flutter/app/helpers/app_bloc_observer.dart';
2525
import 'package:pixel_app_flutter/app/helpers/crashlytics_helper.dart';
2626
import 'package:pixel_app_flutter/app/helpers/logger_route_observer.dart';
27+
import 'package:pixel_app_flutter/app/helpers/platform.dart';
2728
import 'package:pixel_app_flutter/app/overlay.dart';
2829
import 'package:pixel_app_flutter/app/scopes/flows/main_scope.dart';
2930
import 'package:pixel_app_flutter/bootstrap.config.dart';
3031
import 'package:pixel_app_flutter/data/services/apps_service.dart';
3132
import 'package:pixel_app_flutter/data/services/data_source/bluetooth_data_source.dart';
32-
import 'package:pixel_app_flutter/data/services/data_source/usb_data_source.dart';
33+
import 'package:pixel_app_flutter/data/services/data_source/stubs/usb_data_source.dart'
34+
show ListUsbPortsCallback;
3335
import 'package:pixel_app_flutter/data/services/data_source/usb_data_source_android.dart';
3436
import 'package:pixel_app_flutter/data/services/installed_apps_mock.dart';
3537
import 'package:pixel_app_flutter/data/storages/logger_storage.dart';

0 commit comments

Comments
 (0)