This commit is contained in:
Hailey
2024-07-01 18:28:52 -07:00
parent 665cbcdace
commit 73585cbd17
3 changed files with 34 additions and 36 deletions
@@ -32,19 +32,23 @@ class ExpoBlueskyReferrerModule : Module() {
// Some apps explicitly set a referrer, like Chrome. In these cases, we prefer this since // Some apps explicitly set a referrer, like Chrome. In these cases, we prefer this since
// it's the actual website that the user came from rather than the app. // it's the actual website that the user came from rather than the app.
if (intentReferrer is Uri) { if (intentReferrer is Uri) {
return@AsyncFunction mapOf( val res = mapOf(
"referrer" to intentReferrer.toString(), "referrer" to intentReferrer.toString(),
"hostname" to intentReferrer.host, "hostname" to intentReferrer.host,
) )
intent = null
return@AsyncFunction res
} }
// In all other cases, we'll just record the app that sent the intent. // In all other cases, we'll just record the app that sent the intent.
if (activityReferrer != null) { if (activityReferrer != null) {
// referrer could become null here. `.toString()` though can be called on null // referrer could become null here. `.toString()` though can be called on null
return@AsyncFunction mapOf( val res = mapOf(
"referrer" to activityReferrer.toString(), "referrer" to activityReferrer.toString(),
"hostname" to (activityReferrer?.host ?: ""), "hostname" to (activityReferrer?.host ?: ""),
) )
activityReferrer = null
return@AsyncFunction res
} }
return@AsyncFunction null return@AsyncFunction null
@@ -3,34 +3,5 @@ import ExpoModulesCore
public class ExpoBlueskyReferrerModule: Module { public class ExpoBlueskyReferrerModule: Module {
public func definition() -> ModuleDefinition { public func definition() -> ModuleDefinition {
Name("ExpoBlueskyReferrer") Name("ExpoBlueskyReferrer")
AsyncFunction("getReferrerInfoAsync") { (promise: Promise) in
let defaults = UserDefaults.standard
let referrerUrlString = defaults.string(forKey: "referrer")
let referrerApp = defaults.string(forKey: "referrerApp")
if let referrerUrlString = defaults.string(forKey: "referrer"),
let url = URL(string: referrerUrlString)
{
if #available(iOS 16.0, *) {
promise.resolve([
"referrer": url.absoluteString,
"hostname": url.host() ?? ""
])
} else {
promise.resolve([
"referrer": url.absoluteString,
"hostname": url.host ?? ""
])
}
} else if let referrerApp = defaults.string(forKey: "referrerApp") {
promise.resolve([
"referrer": referrerApp,
"hostname": referrerApp,
])
} else {
promise.resolve(nil)
}
}
} }
} }
@@ -1,14 +1,37 @@
import {requireNativeModule} from 'expo' import {SharedPrefs} from '../../index'
import {NotImplementedError} from '../NotImplemented' import {NotImplementedError} from '../NotImplemented'
import {GooglePlayReferrerInfo, ReferrerInfo} from './types' import {GooglePlayReferrerInfo, ReferrerInfo} from './types'
export const NativeModule = requireNativeModule('ExpoBlueskyReferrer')
export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> { export function getGooglePlayReferrerInfoAsync(): Promise<GooglePlayReferrerInfo> {
throw new NotImplementedError() throw new NotImplementedError()
} }
export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> { export function getReferrerInfoAsync(): Promise<ReferrerInfo | null> {
return NativeModule.getReferrerInfoAsync() const referrer = SharedPrefs.getString('referrer')
if (referrer) {
SharedPrefs.removeValue('referrer')
try {
const url = new URL(referrer)
return {
referrer,
hostname: url.hostname,
}
} catch (e) {
return {
referrer,
hostname: undefined,
}
}
}
const referrerApp = SharedPrefs.getString('referrerApp')
if (referrerApp) {
SharedPrefs.removeValue('referrerApp')
return {
referrer: referrerApp,
hostname: referrerApp,
}
}
return null
} }