Skip to content

Commit 1b2f98e

Browse files
it-samsparmar
andauthored
Feature/null safety (#16)
* Delete android plugin file. * Package update. * Dart 2.0 migration. * test case update * Add build runner code. * Add generated test in git ignore. * Add generated test in git ignore * Make platform data optional. * Update test cases * Try to fix travis ci -1 * Rename files. * Delete old files. * Bump the version to 1.0.5 * Version bump 2.0.0 * Apply proper casing on App dialog file. * Make os variable required. * Init manUpData with blank state. * Update test cases * Add os param optional and fallback to operating system. Co-authored-by: sparmar <sparmar@nextfaze.com>
1 parent 531dbb5 commit 1b2f98e

20 files changed

+499
-315
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ios/Runner/GeneratedPluginRegistrant.*
1111
android/local.properties
1212
.vscode
1313
coverage
14+
test/mandatory_update_test.mocks.dart
1415

1516
# Flutter/Dart/Pub related
1617
**/doc/api/

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ before_script:
1414
- ./.flutter/bin/flutter doctor
1515

1616
script:
17+
- ./.flutter/bin/flutter pub get
18+
- ./.flutter/bin/flutter pub run build_runner build
1719
- ./.flutter/bin/flutter analyze
1820
- ./.flutter/bin/flutter test --coverage
1921
after_success:

CHANGELOG.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
1+
# manUp
2+
3+
## [2.0.1]
4+
5+
Minor code changes.
6+
Making some variables required.
7+
Continue renaming some more files.
8+
9+
## [2.0.0]
10+
11+
Migrate to null safety.
12+
**Breaking changes** Renaming some files.
13+
14+
## [1.0.4]
15+
16+
Make get setting method visible
17+
118
## [1.0.3]
19+
220
Add support for desktop platforms
21+
322
## [1.0.2]
23+
424
pub spec upgrade
25+
526
Stop throwing reading file exception.
27+
628
## [1.0.1]
29+
730
Add capability to store and retrieve man up config.
31+
832
## [1.0.0]
33+
934
### First major release
10-
Added Delegate support with `ManupDelegate`
35+
36+
Added Delegate support with `ManUpDelegate`
37+
1138
Added Helper Widget with `ManUpWidget`
39+
1240
Check `README` file for full details.
41+
1342
## [0.0.3]
43+
1444
First manup release

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ service.close();
5151

5252
### Using the Service with Delegate
5353

54-
Implement `ManupDelegate` or use `ManupDelegateMixin` mixin which has default implementation.
54+
Implement `ManUpDelegate` or use `ManUpDelegateMixin` mixin which has default implementation.
5555

5656
- `manUpConfigUpdateStarting()` : will be called before starting to validate
57-
- `manupStatusChanged(ManUpStatus status)` : will be called every time status changes
57+
- `manUpStatusChanged(ManUpStatus status)` : will be called every time status changes
5858
- `manUpUpdateAvailable()` : will be called when ManUpStatus changes to supported
5959
- `manUpUpdateRequired()` : will be called when ManUpStatus changes to unsupported
6060
- `manUpMaintenanceMode()`: will be called when ManUpStatus changes to disabled
@@ -70,7 +70,7 @@ class ManUpExample extends StatefulWidget {
7070
}
7171
7272
class _ManUpExampleState extends State<ManUpExample>
73-
with ManupDelegate, ManupDelegateMixin, DialogMixin {
73+
with ManUpDelegate, ManUpDelegateMixin, DialogMixin {
7474
ManUpService service;
7575
@override
7676
void initState() {
@@ -89,7 +89,7 @@ class _ManUpExampleState extends State<ManUpExample>
8989
@override
9090
void manUpStatusChanged(ManUpStatus status) {
9191
// handle status or show default dialog
92-
showManupDialog(status, service.getMessage(forStatus: status),
92+
showManUpDialog(status, service.getMessage(forStatus: status),
9393
service.configData.updateUrl);
9494
}
9595
@@ -102,7 +102,9 @@ class _ManUpExampleState extends State<ManUpExample>
102102
```
103103

104104
### Using the Service with Helper Widget
105-
Wrap your widget with `ManUpWidget` to automaticaly handle every thing.
105+
106+
Wrap your widget with `ManUpWidget` to automatically handle every thing.
107+
106108
```dart
107109
@override
108110
Widget build(BuildContext context) {
@@ -116,7 +118,8 @@ Wrap your widget with `ManUpWidget` to automaticaly handle every thing.
116118
);
117119
}
118120
```
121+
119122
### Exception Handling
120123

121124
`validate` will throw a `ManUpException` if the lookup failed for any reason. Most likely, this will be caused
122-
by the device being offline and unable to retreive the metadata. It is up to you how you want to handle this in your app. Some apps, where a supported version is critical, should probably not run unless the version was validated successfully. However, for other apps, there's probably no problem and the app should continue running.
125+
by the device being offline and unable to retrieve the metadata. It is up to you how you want to handle this in your app. Some apps, where a supported version is critical, should probably not run unless the version was validated successfully. However, for other apps, there's probably no problem and the app should continue running.

android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/manup.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import 'package:url_launcher/url_launcher.dart';
1212

1313
part 'src/exception.dart';
1414
part 'src/metadata.dart';
15-
part 'src/manup_status.dart';
16-
part 'src/manup_service.dart';
15+
part 'src/man_up_status.dart';
16+
part 'src/man_up_service.dart';
1717
part 'src/package_info_provider.dart';
18-
part 'src/manup_delegate.dart';
19-
part 'src/ui/manup_app_dialog.dart';
20-
part 'src/ui/manup_widget.dart';
21-
part 'src/mixin/manup_delegate_mixin.dart';
22-
part 'src/mixin/manup_dialog_mixin.dart';
18+
part 'src/man_up_delegate.dart';
19+
part 'src/ui/man_up_app_dialog.dart';
20+
part 'src/ui/man_up_widget.dart';
21+
part 'src/mixin/man_up_delegate_mixin.dart';
22+
part 'src/mixin/man_up_dialog_mixin.dart';
2323
part 'src/config_storage.dart';

lib/src/config_storage.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
part of manup;
22

33
class ConfigStorage {
4-
static const String _manupFile = "manup_config.json";
4+
const ConfigStorage();
5+
static const String _manUpFile = "man_up_config.json";
56
//
6-
Future<bool> storeFile({String filename = _manupFile, String fileData}) {
7+
Future<bool> storeFile(
8+
{String filename = _manUpFile, required String fileData}) {
79
return getApplicationDocumentsDirectory().then((directory) {
810
final File file = File('${directory.path}/$filename');
911
return file.writeAsString(fileData).then((_) => Future.value(true));
1012
});
1113
}
1214

13-
Future<String> readfile({String filename = _manupFile}) {
15+
Future<String> readFile({String filename = _manUpFile}) async {
1416
return getApplicationDocumentsDirectory().then((directory) {
1517
final File file = File('${directory.path}/$filename');
1618
return file.exists().then((isExist) =>

lib/src/exception.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
part of manup;
22

33
class ManUpException implements Exception {
4-
final String msg;
4+
final String? msg;
55
const ManUpException([this.msg]);
66

77
@override
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of manup;
22

3-
/// `ManupDelegate` class has required methods.
4-
/// Default implemetation is in `ManupDelegateMixin` file.
5-
abstract class ManupDelegate {
3+
/// `ManUpDelegate` class has required methods.
4+
/// Default implementation is in `ManUpDelegateMixin` file.
5+
abstract class ManUpDelegate {
66
void manUpStatusChanged(ManUpStatus status);
77
void manUpConfigUpdateStarting();
88
void manUpUpdateRequired();
Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,50 @@ part of manup;
33
class ManUpService {
44
final String url;
55
final PackageInfoProvider packageInfoProvider;
6-
@visibleForTesting
7-
ConfigStorage fileStorage = ConfigStorage();
86

9-
String os;
10-
Metadata _manupData;
7+
final ConfigStorage fileStorage;
8+
9+
final String os;
10+
Metadata _manUpData = Metadata();
1111
// read platform data
12-
PlatformData get configData => this.getPlatformData(os, _manupData);
13-
ManupDelegate delegate;
12+
PlatformData? get configData => this.getPlatformData(os, _manUpData);
13+
ManUpDelegate? delegate;
1414

15-
http.Client _client;
15+
final http.Client _client;
1616

1717
///
1818
ManUpService(
1919
this.url, {
2020
this.packageInfoProvider = const DefaultPackageInfoProvider(),
21-
this.os,
22-
http.Client http,
23-
}) : _client = http;
21+
String? os,
22+
required http.Client http,
23+
ConfigStorage storage = const ConfigStorage(),
24+
}) : _client = http,
25+
fileStorage = storage,
26+
this.os = os ?? Platform.operatingSystem;
2427

2528
Future<ManUpStatus> validate() async {
26-
delegate?.manUpConfigUpdateStarting?.call();
29+
delegate?.manUpConfigUpdateStarting();
2730
try {
2831
ManUpStatus status = await _validate();
2932
this._handleManUpStatus(status);
3033
return status;
3134
} catch (e) {
3235
throw e;
3336
} finally {
34-
_storeManupFile();
37+
await _storeManUpFile();
3538
}
3639
}
3740

3841
Future<ManUpStatus> _validate() async {
3942
PackageInfo info = await this.packageInfoProvider.getInfo();
40-
_manupData = await this.getMetadata();
41-
42-
PlatformData platformData = configData;
43+
final metadata = await this.getMetadata();
44+
_manUpData = metadata;
45+
PlatformData? platformData = configData;
46+
//
47+
if (platformData == null) {
48+
return ManUpStatus.unsupported;
49+
}
4350
if (!platformData.enabled) {
4451
return ManUpStatus.disabled;
4552
}
@@ -61,13 +68,10 @@ class ManUpService {
6168
}
6269
}
6370

64-
T setting<T>({String key}) => _manupData?.setting(key: key) ?? null;
71+
T? setting<T>({String? key}) => _manUpData.setting<T>(key: key) ?? null;
6572

6673
@visibleForTesting
67-
PlatformData getPlatformData(String os, Metadata data) {
68-
if (data == null) {
69-
throw ManUpException('No data, validate must be called first.');
70-
}
74+
PlatformData? getPlatformData(String os, Metadata data) {
7175
if (os == 'ios') {
7276
return data.ios;
7377
} else if (os == 'android') {
@@ -87,75 +91,71 @@ class ManUpService {
8791
try {
8892
final uri = Uri.parse(this.url);
8993
var data = await _client.get(uri);
90-
Map<String, dynamic> json = jsonDecode(data.body);
94+
Map<String, dynamic>? json = jsonDecode(data.body);
9195
return Metadata(data: json);
9296
} catch (exception) {
9397
try {
94-
var metadata = await _readManupFile();
98+
var metadata = await _readManUpFile();
9599
return metadata;
96100
} catch (e) {
97101
throw ManUpException(exception.toString());
98102
}
99103
}
100104
}
101105

102-
// manup status validation
103-
_handleManUpStatus(ManUpStatus status) {
104-
this.delegate?.manUpStatusChanged?.call(status);
106+
// manUp status validation
107+
void _handleManUpStatus(ManUpStatus status) {
108+
this.delegate?.manUpStatusChanged(status);
105109
switch (status) {
106110
case ManUpStatus.supported:
107-
this.delegate?.manUpUpdateAvailable?.call();
111+
this.delegate?.manUpUpdateAvailable();
108112
break;
109113
case ManUpStatus.unsupported:
110-
this.delegate?.manUpUpdateRequired?.call();
114+
this.delegate?.manUpUpdateRequired();
111115
break;
112116
case ManUpStatus.disabled:
113-
this.delegate?.manUpMaintenanceMode?.call();
117+
this.delegate?.manUpMaintenanceMode();
114118
break;
115119
default:
116120
break;
117121
}
118122
}
119123

120-
String getMessage({ManUpStatus forStatus}) {
124+
String getMessage({required ManUpStatus forStatus}) {
121125
switch (forStatus) {
122126
case ManUpStatus.supported:
123-
return _manupData?.supportedMessage;
127+
return _manUpData.supportedMessage;
124128
case ManUpStatus.unsupported:
125-
return _manupData?.unsupportedMessage;
126-
break;
129+
return _manUpData.unsupportedMessage;
127130
case ManUpStatus.disabled:
128-
return _manupData?.disabledMessage;
129-
break;
130-
default:
131+
return _manUpData.disabledMessage;
132+
case ManUpStatus.latest:
131133
return "";
132134
}
133135
}
134136

135-
/// manup file storage
136-
void _storeManupFile() async {
137+
/// manUp file storage
138+
Future<void> _storeManUpFile() async {
137139
try {
138-
if (_manupData == null || _manupData._data == null) {
140+
if (_manUpData._data == null) {
139141
return;
140142
}
141-
String json = jsonEncode(_manupData._data);
142-
fileStorage.storeFile(fileData: json);
143+
String json = jsonEncode(_manUpData._data);
144+
await fileStorage.storeFile(fileData: json);
143145
} catch (e) {
144146
print("cannot store file. $e");
145147
}
146148
}
147149

148-
Future<Metadata> _readManupFile() async {
149-
var data = await fileStorage.readfile();
150-
Map<String, dynamic> json = jsonDecode(data);
150+
Future<Metadata> _readManUpFile() async {
151+
var data = await fileStorage.readFile();
152+
Map<String, dynamic>? json = jsonDecode(data);
151153
return Metadata(data: json);
152154
}
153155

154156
//call this on dispose.
155157
void close() {
156-
_client?.close();
157-
_client = null;
158-
fileStorage = null;
158+
_client.close();
159159
this.delegate = null;
160160
}
161161
}

0 commit comments

Comments
 (0)