Skip to content

feat: flutter surveys for iOS #188

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

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.posthog.flutter

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import com.posthog.PersonProfiles
Expand Down Expand Up @@ -72,21 +74,42 @@ class PosthogFlutterPlugin :
}
}

override fun onMethodCall(
call: MethodCall,
result: Result,
) {
when (call.method) {
"setup" -> {
setup(call, result)
}
"identify" -> {
identify(call, result)
}
override fun onMethodCall(call: MethodCall, result: Result) {
try {
when (call.method) {
"openUrl" -> {
val url = call.arguments as? String
if (url != null) {
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
applicationContext.startActivity(intent)
result.success(null)
} catch (e: Exception) {
result.error(
"InvalidURL",
"Cannot open URL",
"The URL $url cannot be opened: ${e.message}"
)
}
} else {
result.error(
"InvalidArguments",
"Invalid URL",
"The URL provided is invalid"
)
}
}
"setup" -> {
setup(call, result)
}
"identify" -> {
identify(call, result)
}

"capture" -> {
capture(call, result)
}
"capture" -> {
capture(call, result)
}

"screen" -> {
screen(call, result)
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ target 'Runner' do
use_frameworks!
use_modular_headers!

pod 'PostHog', :path => '../../../posthog-ios'

flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
target 'RunnerTests' do
inherit! :search_paths
Expand Down
135 changes: 64 additions & 71 deletions example/ios/Runner.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'package:flutter/material.dart';

import 'package:posthog_flutter/posthog_flutter.dart';

Future<void> main() async {
// // init WidgetsFlutterBinding if not yet

WidgetsFlutterBinding.ensureInitialized();
final config =
PostHogConfig('phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D');
PostHogConfig('phc_WKfvDfedaJEDCoUmt9pVa3OWtbbUP1W2ctxwXkt3A3n');
config.debug = true;
config.captureApplicationLifecycleEvents = false;
config.host = 'https://us.i.posthog.com';
config.surveys = true;
config.sessionReplay = true;
config.sessionReplayConfig.maskAllTexts = false;
config.sessionReplayConfig.maskAllImages = false;
Expand Down Expand Up @@ -40,20 +40,20 @@ class _MyAppState extends State<MyApp> {
child: MaterialApp(
navigatorObservers: [PosthogObserver()],
title: 'Flutter App',
home: const _InitialScreen(),
home: const InitialScreen(),
),
);
}
}

class _InitialScreen extends StatefulWidget {
const _InitialScreen({Key? key}) : super(key: key);
class InitialScreen extends StatefulWidget {
const InitialScreen({Key? key}) : super(key: key);

@override
_InitialScreenState createState() => _InitialScreenState();
}

class _InitialScreenState extends State<_InitialScreen> {
class _InitialScreenState extends State<InitialScreen> {
final _posthogFlutterPlugin = Posthog();
dynamic _result = "";

Expand All @@ -79,7 +79,7 @@ class _InitialScreenState extends State<_InitialScreen> {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const _SecondRoute(),
builder: (context) => const SecondRoute(),
settings: const RouteSettings(name: 'second_route')),
);
},
Expand Down Expand Up @@ -282,15 +282,14 @@ class _InitialScreenState extends State<_InitialScreen> {
}
}

class _SecondRoute extends StatefulWidget {
const _SecondRoute();
class SecondRoute extends StatefulWidget {
const SecondRoute({super.key});

@override
_SecondRouteState createState() => _SecondRouteState();
}

class _SecondRouteState extends State<_SecondRoute>
with WidgetsBindingObserver {
class _SecondRouteState extends State<SecondRoute> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
Expand Down
102 changes: 102 additions & 0 deletions ios/Classes/PostHogDisplaySurvey+Dict.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#if os(iOS)
import Foundation
import PostHog

extension PostHogDisplaySurvey {
func toDict() -> [String: Any] {
var dict: [String: Any] = [
"id": id,
"name": name,
"questions": questions.map { question -> [String: Any] in
var questionDict: [String: Any] = [
"question": question.question,
"isOptional": question.isOptional,
]
if let desc = question.questionDescription {
questionDict["questionDescription"] = desc
}
if let buttonText = question.buttonText {
questionDict["buttonText"] = buttonText
}

// Add type-specific properties
switch question {
case let linkQuestion as PostHogDisplayLinkQuestion:
questionDict["type"] = "link"
questionDict["link"] = linkQuestion.link
case let ratingQuestion as PostHogDisplayRatingQuestion:
questionDict["type"] = "rating"
questionDict["ratingType"] = ratingQuestion.ratingType.rawValue
questionDict["scaleLowerBound"] = ratingQuestion.scaleLowerBound
questionDict["scaleUpperBound"] = ratingQuestion.scaleUpperBound
questionDict["lowerBoundLabel"] = ratingQuestion.lowerBoundLabel
questionDict["upperBoundLabel"] = ratingQuestion.upperBoundLabel
case let choiceQuestion as PostHogDisplayChoiceQuestion:
questionDict["type"] = choiceQuestion.isMultipleChoice ? "multiple_choice" : "single_choice"
questionDict["choices"] = choiceQuestion.choices
questionDict["hasOpenChoice"] = choiceQuestion.hasOpenChoice
questionDict["shuffleOptions"] = choiceQuestion.shuffleOptions
default:
questionDict["type"] = "open"
}

return questionDict
},
]

if let appearance = appearance {
var appearanceDict: [String: Any] = [:]
if let fontFamily = appearance.fontFamily {
appearanceDict["fontFamily"] = fontFamily
}
if let backgroundColor = appearance.backgroundColor {
appearanceDict["backgroundColor"] = backgroundColor
}
if let borderColor = appearance.borderColor {
appearanceDict["borderColor"] = borderColor
}
if let submitButtonColor = appearance.submitButtonColor {
appearanceDict["submitButtonColor"] = submitButtonColor
}
if let submitButtonText = appearance.submitButtonText {
appearanceDict["submitButtonText"] = submitButtonText
}
if let submitButtonTextColor = appearance.submitButtonTextColor {
appearanceDict["submitButtonTextColor"] = submitButtonTextColor
}
if let descriptionTextColor = appearance.descriptionTextColor {
appearanceDict["descriptionTextColor"] = descriptionTextColor
}
if let ratingButtonColor = appearance.ratingButtonColor {
appearanceDict["ratingButtonColor"] = ratingButtonColor
}
if let ratingButtonActiveColor = appearance.ratingButtonActiveColor {
appearanceDict["ratingButtonActiveColor"] = ratingButtonActiveColor
}
if let placeholder = appearance.placeholder {
appearanceDict["placeholder"] = placeholder
}
appearanceDict["displayThankYouMessage"] = appearance.displayThankYouMessage
if let thankYouMessageHeader = appearance.thankYouMessageHeader {
appearanceDict["thankYouMessageHeader"] = thankYouMessageHeader
}
if let thankYouMessageDescription = appearance.thankYouMessageDescription {
appearanceDict["thankYouMessageDescription"] = thankYouMessageDescription
}
if let thankYouMessageCloseButtonText = appearance.thankYouMessageCloseButtonText {
appearanceDict["thankYouMessageCloseButtonText"] = thankYouMessageCloseButtonText
}
dict["appearance"] = appearanceDict
}

if let startDate = startDate {
dict["startDate"] = Int64(startDate.timeIntervalSince1970 * 1000)
}
if let endDate = endDate {
dict["endDate"] = Int64(endDate.timeIntervalSince1970 * 1000)
}

return dict
}
}
#endif
Loading
Loading