Skip to content

Commit

Permalink
ref: relative imports and moved non exported files to src folder (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Jan 5, 2024
1 parent 454c396 commit 1bf8e42
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 107 deletions.
6 changes: 4 additions & 2 deletions lib/posthog_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export 'package:posthog_flutter/src/posthog.dart';
export 'package:posthog_flutter/src/posthog_observer.dart';
library posthog_flutter;

export 'src/posthog.dart';
export 'src/posthog_observer.dart';
94 changes: 4 additions & 90 deletions lib/posthog_flutter_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import 'dart:js';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

import 'posthog_flutter_platform_interface.dart';
import 'src/posthog_flutter_platform_interface.dart';
import 'src/posthog_flutter_web_handler.dart';

/// A web implementation of the PosthogFlutterPlatform of the PosthogFlutter plugin.
class PosthogFlutterWeb extends PosthogFlutterPlatform {
class PosthogFlutterWeb extends PosthogFlutterPlatformInterface {
/// Constructs a PosthogFlutterWeb
PosthogFlutterWeb();

Expand All @@ -24,93 +25,6 @@ class PosthogFlutterWeb extends PosthogFlutterPlatform {
}

Future<dynamic> handleMethodCall(MethodCall call) async {
final analytics = JsObject.fromBrowserObject(context['posthog']);
switch (call.method) {
case 'identify':
final userProperties = call.arguments['userProperties'];
final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'];
analytics.callMethod('identify', [
call.arguments['userId'],
JsObject.jsify(userProperties),
JsObject.jsify(userPropertiesSetOnce),
]);
break;
case 'capture':
analytics.callMethod('capture', [
call.arguments['eventName'],
JsObject.jsify(call.arguments['properties']),
]);
break;
case 'screen':
final properties = call.arguments['properties'] ?? {};
final screenName = call.arguments['screenName'];
if (screenName != null) {
properties['\$screen_name'] = screenName;
}
analytics.callMethod('capture', [
'\$screen',
JsObject.jsify(properties),
]);
break;
case 'alias':
analytics.callMethod('alias', [
call.arguments['alias'],
]);
break;
case 'distinctId':
final distinctId = analytics.callMethod('get_distinct_id');
return distinctId;
case 'reset':
analytics.callMethod('reset');
break;
case 'debug':
analytics.callMethod('debug', [
call.arguments['debug'],
]);
break;
case 'isFeatureEnabled':
final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [
call.arguments['key'],
]);
return isFeatureEnabled;
case 'group':
analytics.callMethod('group', [
call.arguments['groupType'],
call.arguments['groupKey'],
JsObject.jsify(call.arguments['groupProperties']),
]);
break;
case 'reloadFeatureFlags':
analytics.callMethod('reloadFeatureFlags');
break;
case 'enable':
analytics.callMethod('opt_in_capturing');
break;
case 'disable':
analytics.callMethod('opt_out_capturing');
break;
case 'getFeatureFlag':
analytics.callMethod('getFeatureFlag', [
call.arguments['key'],
]);
break;
case 'getFeatureFlagPayload':
analytics.callMethod('getFeatureFlagPayload', [
call.arguments['key'],
]);
break;
case 'register':
final properties = {call.arguments['key']: call.arguments['value']};
analytics.callMethod('register', [
properties,
]);
break;
default:
throw PlatformException(
code: 'Unimplemented',
details:
"The posthog plugin for web doesn't implement the method '${call.method}'",
);
}
handleWebMethodCall(call, context);
}
}
6 changes: 3 additions & 3 deletions lib/src/posthog.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:posthog_flutter/posthog_flutter_platform_interface.dart';
export 'package:posthog_flutter/src/posthog_observer.dart';
import 'posthog_flutter_platform_interface.dart';

class Posthog {
static PosthogFlutterPlatform get _posthog => PosthogFlutterPlatform.instance;
static PosthogFlutterPlatformInterface get _posthog =>
PosthogFlutterPlatformInterface.instance;

static final Posthog _instance = Posthog._internal();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:flutter/services.dart';

import 'posthog_flutter_platform_interface.dart';

/// An implementation of [PosthogFlutterPlatform] that uses method channels.
class MethodChannelPosthogFlutter extends PosthogFlutterPlatform {
/// An implementation of [PosthogFlutterPlatformInterface] that uses method channels.
class PosthogFlutterIO extends PosthogFlutterPlatformInterface {
/// The method channel used to interact with the native platform.
final _methodChannel = const MethodChannel('posthog_flutter');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'posthog_flutter_method_channel.dart';
import 'posthog_flutter_io.dart';

abstract class PosthogFlutterPlatform extends PlatformInterface {
abstract class PosthogFlutterPlatformInterface extends PlatformInterface {
/// Constructs a PosthogFlutterPlatform.
PosthogFlutterPlatform() : super(token: _token);
PosthogFlutterPlatformInterface() : super(token: _token);

static final Object _token = Object();

static PosthogFlutterPlatform _instance = MethodChannelPosthogFlutter();
static PosthogFlutterPlatformInterface _instance = PosthogFlutterIO();

/// The default instance of [PosthogFlutterPlatform] to use.
/// The default instance of [PosthogFlutterPlatformInterface] to use.
///
/// Defaults to [MethodChannelPosthogFlutter].
static PosthogFlutterPlatform get instance => _instance;
/// Defaults to [PosthogFlutterIO].
static PosthogFlutterPlatformInterface get instance => _instance;

/// Platform-specific implementations should set this with their own
/// platform-specific class that extends [PosthogFlutterPlatform] when
/// platform-specific class that extends [PosthogFlutterPlatformInterface] when
/// they register themselves.
static set instance(PosthogFlutterPlatform instance) {
static set instance(PosthogFlutterPlatformInterface instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
Expand Down
94 changes: 94 additions & 0 deletions lib/src/posthog_flutter_web_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'dart:js';

import 'package:flutter/services.dart';

Future<dynamic> handleWebMethodCall(MethodCall call, JsObject context) async {
final analytics = JsObject.fromBrowserObject(context['posthog']);
switch (call.method) {
case 'identify':
final userProperties = call.arguments['userProperties'];
final userPropertiesSetOnce = call.arguments['userPropertiesSetOnce'];
analytics.callMethod('identify', [
call.arguments['userId'],
JsObject.jsify(userProperties),
JsObject.jsify(userPropertiesSetOnce),
]);
break;
case 'capture':
analytics.callMethod('capture', [
call.arguments['eventName'],
JsObject.jsify(call.arguments['properties']),
]);
break;
case 'screen':
final properties = call.arguments['properties'] ?? {};
final screenName = call.arguments['screenName'];
if (screenName != null) {
properties['\$screen_name'] = screenName;
}
analytics.callMethod('capture', [
'\$screen',
JsObject.jsify(properties),
]);
break;
case 'alias':
analytics.callMethod('alias', [
call.arguments['alias'],
]);
break;
case 'distinctId':
final distinctId = analytics.callMethod('get_distinct_id');
return distinctId;
case 'reset':
analytics.callMethod('reset');
break;
case 'debug':
analytics.callMethod('debug', [
call.arguments['debug'],
]);
break;
case 'isFeatureEnabled':
final isFeatureEnabled = analytics.callMethod('isFeatureEnabled', [
call.arguments['key'],
]);
return isFeatureEnabled;
case 'group':
analytics.callMethod('group', [
call.arguments['groupType'],
call.arguments['groupKey'],
JsObject.jsify(call.arguments['groupProperties']),
]);
break;
case 'reloadFeatureFlags':
analytics.callMethod('reloadFeatureFlags');
break;
case 'enable':
analytics.callMethod('opt_in_capturing');
break;
case 'disable':
analytics.callMethod('opt_out_capturing');
break;
case 'getFeatureFlag':
final featureFlag = analytics.callMethod('getFeatureFlag', [
call.arguments['key'],
]);
return featureFlag;
case 'getFeatureFlagPayload':
final featureFlag = analytics.callMethod('getFeatureFlagPayload', [
call.arguments['key'],
]);
return featureFlag;
case 'register':
final properties = {call.arguments['key']: call.arguments['value']};
analytics.callMethod('register', [
properties,
]);
break;
default:
throw PlatformException(
code: 'Unimplemented',
details:
"The posthog plugin for web doesn't implement the method '${call.method}'",
);
}
}
3 changes: 2 additions & 1 deletion lib/src/posthog_observer.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart';
import 'package:posthog_flutter/posthog_flutter.dart';

import 'posthog.dart';

typedef ScreenNameExtractor = String? Function(RouteSettings settings);

Expand Down

0 comments on commit 1bf8e42

Please sign in to comment.