Skip to content
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

Migrate to flutter 2 #10

Merged
merged 3 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0

- Migrate to flutter 2

## 1.11.2

- Bump and pin version for Android lib to 1.1.1 because of bug
Expand Down
15 changes: 15 additions & 0 deletions example/lib/generated_plugin_registrant.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Generated file. Do not edit.
//

// ignore_for_file: lines_longer_than_80_chars

import 'package:posthog_flutter/src/posthog_web.dart';

import 'package:flutter_web_plugins/flutter_web_plugins.dart';

// ignore: public_member_api_docs
void registerPlugins(Registrar registrar) {
PosthogWeb.registerWith(registrar);
registrar.registerMessageHandler();
}
35 changes: 19 additions & 16 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
///
/// Aside from this special use case, any other context property that needs
/// to be defined (or re-defined) can be done.
Posthog.setContext({
Posthog().setContext({
'device': {
'token': 'testing',
}
Expand All @@ -33,7 +33,7 @@ void main() {
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Posthog.screen(
Posthog().screen(
screenName: 'Example Screen',
);
return MaterialApp(
Expand All @@ -45,10 +45,10 @@ class MyApp extends StatelessWidget {
children: <Widget>[
Spacer(),
Center(
child: FlatButton(
child: TextButton(
child: Text('CAPTURE ACTION WITH POSTHOG'),
onPressed: () {
Posthog.capture(
Posthog().capture(
eventName: 'ButtonClicked',
properties: {
'foo': 'bar',
Expand All @@ -61,49 +61,52 @@ class MyApp extends StatelessWidget {
),
Spacer(),
Center(
child: FlatButton(
child: TextButton(
child: Text('Update Context'),
onPressed: () {
Posthog.setContext({'custom': 123});
Posthog().setContext({'custom': 123});
},
),
),
Spacer(),
Center(
child: FlatButton(
child: TextButton(
child: Text('Clear Context'),
onPressed: () {
Posthog.setContext({});
Posthog().setContext({});
},
),
),
Spacer(),
Center(
child: FlatButton(
child: TextButton(
child: Text('Disable'),
onPressed: () async {
await Posthog.disable();
Posthog.capture(eventName: 'This event will not be logged');
await Posthog().disable();
Posthog().capture(
eventName: 'This event will not be logged',
properties: {});
},
),
),
Spacer(),
Center(
child: FlatButton(
child: TextButton(
child: Text('Enable'),
onPressed: () async {
await Posthog.enable();
Posthog.capture(eventName: 'Enabled capturing events!');
await Posthog().enable();
Posthog().capture(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this standard Dart indentation? (I don't know so I'm truly asking)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's pretty odd for those who are used to other languages, but it's the default, I even used dart format in this file before saving the changes

eventName: 'Enabled capturing events!', properties: {});
},
),
),
Spacer(),
Platform.isIOS
? Center(
child: FlatButton(
child: TextButton(
child: Text('Debug mode'),
onPressed: () {
Posthog.debug(true);
Posthog().debug(true);
},
),
)
Expand Down
28 changes: 13 additions & 15 deletions lib/src/posthog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:io';

import 'package:meta/meta.dart';
import 'package:posthog_flutter/src/posthog_platform_interface.dart';

export 'package:posthog_flutter/src/posthog_observer.dart';
Expand All @@ -15,12 +13,12 @@ class Posthog {
return _instance;
}

String currentScreen;
String? currentScreen;

Future<void> identify({
@required userId,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
return _posthog.identify(
userId: userId,
Expand All @@ -30,9 +28,9 @@ class Posthog {
}

Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String eventName,
required Map<String, dynamic> properties,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to have diverged from lib/src/posthog_method_channel.dart where properties are correctly assigned as optional

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'm going to make it nullable instead of required

Map<String, dynamic>? options,
}) {
if (!properties.containsKey('\$screen_name')) {
properties['\$screen_name'] = this.currentScreen;
Expand All @@ -45,9 +43,9 @@ class Posthog {
}

Future<void> screen({
@required String screenName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String screenName,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
if (screenName != '/') {
this.currentScreen = screenName;
Expand All @@ -60,16 +58,16 @@ class Posthog {
}

Future<void> alias({
@required String alias,
Map<String, dynamic> options,
required String alias,
Map<String, dynamic>? options,
}) {
return _posthog.alias(
alias: alias,
options: options,
);
}

Future<String> get getAnonymousId {
Future<String?> get getAnonymousId {
return _posthog.getAnonymousId;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/posthog_default_options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class PosthogDefaultOptions {
Map<String, dynamic> options;
Map<String, dynamic>? options;

/// Singleton of [PosthogDefaultOptions].
static final PosthogDefaultOptions instance = PosthogDefaultOptions._();
Expand Down
25 changes: 12 additions & 13 deletions lib/src/posthog_method_channel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:meta/meta.dart';
import 'package:flutter/services.dart';
import 'package:posthog_flutter/src/posthog_default_options.dart';
import 'package:posthog_flutter/src/posthog_platform_interface.dart';
Expand All @@ -7,9 +6,9 @@ const MethodChannel _channel = MethodChannel('posthogflutter');

class PosthogMethodChannel extends PosthogPlatform {
Future<void> identify({
@required userId,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) async {
try {
await _channel.invokeMethod('identify', {
Expand All @@ -23,9 +22,9 @@ class PosthogMethodChannel extends PosthogPlatform {
}

Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String eventName,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) async {
try {
await _channel.invokeMethod('capture', {
Expand All @@ -39,9 +38,9 @@ class PosthogMethodChannel extends PosthogPlatform {
}

Future<void> screen({
@required String screenName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String screenName,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) async {
try {
await _channel.invokeMethod('screen', {
Expand All @@ -55,8 +54,8 @@ class PosthogMethodChannel extends PosthogPlatform {
}

Future<void> alias({
@required String alias,
Map<String, dynamic> options,
required String alias,
Map<String, dynamic>? options,
}) async {
try {
await _channel.invokeMethod('alias', {
Expand All @@ -68,7 +67,7 @@ class PosthogMethodChannel extends PosthogPlatform {
}
}

Future<String> get getAnonymousId async {
Future<String?> get getAnonymousId async {
return await _channel.invokeMethod('getAnonymousId');
}

Expand Down
12 changes: 6 additions & 6 deletions lib/src/posthog_observer.dart
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import 'package:flutter/widgets.dart';
import 'package:posthog_flutter/posthog_flutter.dart';

typedef String ScreenNameExtractor(RouteSettings settings);
typedef String? ScreenNameExtractor(RouteSettings settings);

String defaultNameExtractor(RouteSettings settings) => settings.name;
String? defaultNameExtractor(RouteSettings settings) => settings.name;

class PosthogObserver extends RouteObserver<PageRoute<dynamic>> {
PosthogObserver({this.nameExtractor = defaultNameExtractor});

final ScreenNameExtractor nameExtractor;

void _sendScreenView(PageRoute<dynamic> route) {
final String screenName = nameExtractor(route.settings);
final String? screenName = nameExtractor(route.settings);
if (screenName != null) {
Posthog().screen(screenName: screenName);
}
}

@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute) {
_sendScreenView(route);
}
}

@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (newRoute is PageRoute) {
_sendScreenView(newRoute);
}
}

@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);
if (previousRoute is PageRoute && route is PageRoute) {
_sendScreenView(previousRoute);
Expand Down
25 changes: 12 additions & 13 deletions lib/src/posthog_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:meta/meta.dart';
import 'package:posthog_flutter/src/posthog_method_channel.dart';

abstract class PosthogPlatform {
Expand All @@ -12,37 +11,37 @@ abstract class PosthogPlatform {
static PosthogPlatform instance = PosthogMethodChannel();

Future<void> identify({
@required userId,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required userId,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
throw UnimplementedError('identify() has not been implemented.');
}

Future<void> capture({
@required String eventName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String eventName,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
throw UnimplementedError('capture() has not been implemented.');
}

Future<void> screen({
@required String screenName,
Map<String, dynamic> properties,
Map<String, dynamic> options,
required String screenName,
Map<String, dynamic>? properties,
Map<String, dynamic>? options,
}) {
throw UnimplementedError('screen() has not been implemented.');
}

Future<void> alias({
@required String alias,
Map<String, dynamic> options,
required String alias,
Map<String, dynamic>? options,
}) {
throw UnimplementedError('alias() has not been implemented.');
}

Future<String> get getAnonymousId {
Future<String?> get getAnonymousId {
throw UnimplementedError('getAnonymousId() has not been implemented.');
}

Expand Down
2 changes: 0 additions & 2 deletions lib/src/posthog_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class PosthogWeb {
final MethodChannel channel = MethodChannel(
'posthog_flutter',
const StandardMethodCodec(),
registrar.messenger,
);
final PosthogWeb instance = PosthogWeb();
channel.setMethodCallHandler(instance.handleMethodCall);
Expand Down Expand Up @@ -44,7 +43,6 @@ class PosthogWeb {
final user = analytics.callMethod('user');
final anonymousId = user.callMethod('anonymousId');
return anonymousId;
break;
case 'reset':
analytics.callMethod('reset');
break;
Expand Down
Loading