Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
update for null safety (#145)
Browse files Browse the repository at this point in the history
update for null safety
  • Loading branch information
devoncarew committed Nov 9, 2020
1 parent 9f30bd0 commit 62d73d9
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest

container:
image: google/dart:beta
image: google/dart:dev

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# https://dart.dev/guides/libraries/private-files
.packages
.dart_tool/
.idea/
pubspec.lock
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.0.0-nullsafety
- Updated to support 2.12.0 and null safety.

## 3.4.2
- A number of cleanups to improve the package health score.

Expand Down
20 changes: 10 additions & 10 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import 'dart:html';

import 'package:usage/usage_html.dart';

Analytics _analytics;
String _lastUa;
Analytics? _analytics;
String? _lastUa;
int _count = 0;

void main() {
querySelector('#foo').onClick.listen((_) => _handleFoo());
querySelector('#bar').onClick.listen((_) => _handleBar());
querySelector('#page').onClick.listen((_) => _changePage());
querySelector('#foo')!.onClick.listen((_) => _handleFoo());
querySelector('#bar')!.onClick.listen((_) => _handleBar());
querySelector('#page')!.onClick.listen((_) => _changePage());
}

String _ua() => (querySelector('#ua') as InputElement).value.trim();
String _ua() => (querySelector('#ua') as InputElement).value!.trim();

Analytics getAnalytics() {
if (_analytics == null || _lastUa != _ua()) {
_lastUa = _ua();
_analytics = AnalyticsHtml(_lastUa, 'Test app', '1.0');
_analytics.sendScreenView(window.location.pathname);
_analytics = AnalyticsHtml(_lastUa!, 'Test app', '1.0');
_analytics!.sendScreenView(window.location.pathname!);
}

return _analytics;
return _analytics!;
}

void _handleFoo() {
Expand All @@ -44,5 +44,5 @@ void _handleBar() {
void _changePage() {
var analytics = getAnalytics();
window.history.pushState(null, 'new page', '${++_count}.html');
analytics.sendScreenView(window.location.pathname);
analytics.sendScreenView(window.location.pathname!);
}
32 changes: 15 additions & 17 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ String postEncode(Map<String, dynamic> map) {
class ThrottlingBucket {
final int startingCount;
int drops;
int _lastReplenish;
late int _lastReplenish;

ThrottlingBucket(this.startingCount) {
drops = startingCount;
ThrottlingBucket(this.startingCount) : drops = startingCount {
_lastReplenish = DateTime.now().millisecondsSinceEpoch;
}

Expand Down Expand Up @@ -61,9 +60,9 @@ class AnalyticsImpl implements Analytics {
@override
final String trackingId;
@override
final String applicationName;
final String? applicationName;
@override
final String applicationVersion;
final String? applicationVersion;

final PersistentProperties properties;
final PostHandler postHandler;
Expand All @@ -76,22 +75,20 @@ class AnalyticsImpl implements Analytics {
@override
AnalyticsOpt analyticsOpt = AnalyticsOpt.optOut;

String _url;
late String _url;

final StreamController<Map<String, dynamic>> _sendController =
StreamController.broadcast(sync: true);

AnalyticsImpl(this.trackingId, this.properties, this.postHandler,
{this.applicationName, this.applicationVersion, String analyticsUrl}) {
assert(trackingId != null);

{this.applicationName, this.applicationVersion, String? analyticsUrl}) {
if (applicationName != null) setSessionValue('an', applicationName);
if (applicationVersion != null) setSessionValue('av', applicationVersion);

_url = analyticsUrl ?? _defaultAnalyticsUrl;
}

bool _firstRun;
bool? _firstRun;

@override
bool get firstRun {
Expand All @@ -103,7 +100,7 @@ class AnalyticsImpl implements Analytics {
}
}

return _firstRun;
return _firstRun!;
}

@override
Expand All @@ -120,7 +117,7 @@ class AnalyticsImpl implements Analytics {
}

@override
Future sendScreenView(String viewName, {Map<String, String> parameters}) {
Future sendScreenView(String viewName, {Map<String, String>? parameters}) {
var args = <String, dynamic>{'cd': viewName};
if (parameters != null) {
args.addAll(parameters);
Expand All @@ -130,7 +127,7 @@ class AnalyticsImpl implements Analytics {

@override
Future sendEvent(String category, String action,
{String label, int value, Map<String, String> parameters}) {
{String? label, int? value, Map<String, String>? parameters}) {
var args = <String, dynamic>{'ec': category, 'ea': action};
if (label != null) args['el'] = label;
if (value != null) args['ev'] = value;
Expand All @@ -148,7 +145,7 @@ class AnalyticsImpl implements Analytics {

@override
Future sendTiming(String variableName, int time,
{String category, String label}) {
{String? category, String? label}) {
var args = <String, dynamic>{'utv': variableName, 'utt': time};
if (label != null) args['utl'] = label;
if (category != null) args['utc'] = category;
Expand All @@ -157,12 +154,12 @@ class AnalyticsImpl implements Analytics {

@override
AnalyticsTimer startTimer(String variableName,
{String category, String label}) {
{String? category, String? label}) {
return AnalyticsTimer(this, variableName, category: category, label: label);
}

@override
Future sendException(String description, {bool fatal}) {
Future sendException(String description, {bool? fatal}) {
// We trim exceptions to a max length; google analytics will apply it's own
// truncation, likely around 150 chars or so.
const maxExceptionLength = 1000;
Expand Down Expand Up @@ -201,7 +198,7 @@ class AnalyticsImpl implements Analytics {
Stream<Map<String, dynamic>> get onSend => _sendController.stream;

@override
Future waitForLastPing({Duration timeout}) {
Future waitForLastPing({Duration? timeout}) {
Future f = Future.wait(_futures).catchError((e) => null);

if (timeout != null) {
Expand Down Expand Up @@ -268,6 +265,7 @@ abstract class PersistentProperties {
PersistentProperties(this.name);

dynamic operator [](String key);

void operator []=(String key, dynamic value);

/// Re-read settings from the backing store. This may be a no-op on some
Expand Down
21 changes: 11 additions & 10 deletions lib/src/usage_impl_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,39 @@ import 'usage_impl.dart';
class AnalyticsHtml extends AnalyticsImpl {
AnalyticsHtml(
String trackingId, String applicationName, String applicationVersion,
{String analyticsUrl})
{String? analyticsUrl})
: super(trackingId, HtmlPersistentProperties(applicationName),
HtmlPostHandler(),
applicationName: applicationName,
applicationVersion: applicationVersion,
analyticsUrl: analyticsUrl) {
var screenWidth = window.screen.width;
var screenHeight = window.screen.height;
var screenWidth = window.screen!.width;
var screenHeight = window.screen!.height;

setSessionValue('sr', '${screenWidth}x$screenHeight');
setSessionValue('sd', '${window.screen.pixelDepth}-bits');
setSessionValue('sd', '${window.screen!.pixelDepth}-bits');
setSessionValue('ul', window.navigator.language);
}
}

typedef HttpRequestor = Future<HttpRequest> Function(String url,
{String method, dynamic sendData});
{String? method, dynamic sendData});

class HtmlPostHandler extends PostHandler {
final HttpRequestor mockRequestor;
final HttpRequestor? mockRequestor;

HtmlPostHandler({this.mockRequestor});

@override
Future sendPost(String url, Map<String, dynamic> parameters) {
var viewportWidth = document.documentElement.clientWidth;
var viewportHeight = document.documentElement.clientHeight;
var viewportWidth = document.documentElement!.clientWidth;
var viewportHeight = document.documentElement!.clientHeight;

parameters['vp'] = '${viewportWidth}x$viewportHeight';

var data = postEncode(parameters);
var requestor = mockRequestor ?? HttpRequest.request;
Future<HttpRequest> Function(String, {String method, dynamic sendData})
requestor = mockRequestor ?? HttpRequest.request;
return requestor(url, method: 'POST', sendData: data).catchError((e) {
// Catch errors that can happen during a request, but that we can't do
// anything about, e.g. a missing internet connection.
Expand All @@ -58,7 +59,7 @@ class HtmlPostHandler extends PostHandler {
}

class HtmlPersistentProperties extends PersistentProperties {
Map _map;
late Map _map;

HtmlPersistentProperties(String name) : super(name) {
var str = window.localStorage[name];
Expand Down
31 changes: 14 additions & 17 deletions lib/src/usage_impl_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import 'usage_impl.dart';
class AnalyticsIO extends AnalyticsImpl {
AnalyticsIO(
String trackingId, String applicationName, String applicationVersion,
{String analyticsUrl, Directory documentDirectory})
{String? analyticsUrl, Directory? documentDirectory})
: super(
trackingId,
IOPersistentProperties(applicationName,
Expand Down Expand Up @@ -75,9 +75,9 @@ String getDartVersion() {

class IOPostHandler extends PostHandler {
final String _userAgent;
final HttpClient mockClient;
final HttpClient? mockClient;

HttpClient _client;
HttpClient? _client;

IOPostHandler({this.mockClient}) : _userAgent = _createUserAgent();

Expand All @@ -87,11 +87,11 @@ class IOPostHandler extends PostHandler {

if (_client == null) {
_client = mockClient ?? HttpClient();
_client.userAgent = _userAgent;
_client!.userAgent = _userAgent;
}

try {
var req = await _client.postUrl(Uri.parse(url));
var req = await _client!.postUrl(Uri.parse(url));
req.write(data);
var response = await req.close();
await response.drain();
Expand All @@ -108,10 +108,10 @@ class IOPostHandler extends PostHandler {
JsonEncoder _jsonEncoder = JsonEncoder.withIndent(' ');

class IOPersistentProperties extends PersistentProperties {
File _file;
Map _map;
late File _file;
late Map _map;

IOPersistentProperties(String name, {String documentDirPath}) : super(name) {
IOPersistentProperties(String name, {String? documentDirPath}) : super(name) {
var fileName = '.${name.replaceAll(' ', '_')}';
documentDirPath ??= userHomeDir();
_file = File(path.join(documentDirPath, fileName));
Expand Down Expand Up @@ -162,18 +162,15 @@ class IOPersistentProperties extends PersistentProperties {

/// Return the string for the platform's locale; return's `null` if the locale
/// can't be determined.
String getPlatformLocale() {
String? getPlatformLocale() {
var locale = Platform.localeName;
if (locale == null) return null;

if (locale != null) {
// Convert `en_US.UTF-8` to `en_US`.
var index = locale.indexOf('.');
if (index != -1) locale = locale.substring(0, index);
// Convert `en_US.UTF-8` to `en_US`.
var index = locale.indexOf('.');
if (index != -1) locale = locale.substring(0, index);

// Convert `en_US` to `en-us`.
locale = locale.replaceAll('_', '-').toLowerCase();
}
// Convert `en_US` to `en-us`.
locale = locale.replaceAll('_', '-').toLowerCase();

return locale;
}
Loading

0 comments on commit 62d73d9

Please sign in to comment.