Skip to content

Commit

Permalink
feat(iOS, Android): Rename and refactor Echo plugin to SafeAreasColor…
Browse files Browse the repository at this point in the history
…Plugin with updated methods for color handling
  • Loading branch information
lgmarchi committed Dec 6, 2024
1 parent 5fd188e commit a5b2b1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Capacitor
import UIKit

@objc(EchoPlugin)
public class EchoPlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "EchoPlugin"
public let jsName = "Echo"
@objc(SafeAreasColorPlugin)
public class SafeAreasColorPlugin: CAPPlugin, CAPBridgedPlugin {
public let identifier = "SafeAreasColorPlugin"
public let jsName = "SafeAreasColor"
public let pluginMethods: [CAPPluginMethod] = [
CAPPluginMethod(name: "echo", returnType: CAPPluginReturnPromise)
CAPPluginMethod(name: "changeSafeAreasColorOniOS", returnType: CAPPluginReturnPromise)
]

@objc func echo(_ call: CAPPluginCall) {
@objc func changeSafeAreasColorOniOS(_ call: CAPPluginCall) {
let value = call.getString("color") ?? ""
guard let uiColor = UIColor(hex: value) else {
call.reject("Invalid color format")
Expand Down
2 changes: 1 addition & 1 deletion ios/App/PluginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Capacitor

class PluginViewController: CAPBridgeViewController {
override open func capacitorDidLoad() {
bridge?.registerPluginInstance(EchoPlugin())
bridge?.registerPluginInstance(SafeAreasColorPlugin())
}

override open func viewDidLoad() {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/plugins/safeAreaColorAndroid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { log } from "$lib/utils/Logger"
import { registerPlugin } from "@capacitor/core"
import { StatusBar, Style } from "@capacitor/status-bar"

interface ISafeAreaColorPlugin {
setStatusBarColor(options: { color: string }): Promise<{ value: string }>
Expand All @@ -11,6 +12,16 @@ const SafeAreaColorPlugin = registerPlugin<ISafeAreaColorPlugin>("SafeAreaColorP
async function setStatusBarColor(color: string) {
try {
log.info("Calling native android function to change status bar color")
if (color.toLowerCase() === "white") {
color = "#FFFFFF"
log.info(`Converted color "white" to hexadecimal: ${color}`)
await StatusBar.setStyle({ style: Style.Light })
log.debug("Change status bar style to light")
} else {
await StatusBar.setStyle({ style: Style.Dark })
log.debug("Change status bar style to dark")
}

await SafeAreaColorPlugin.setStatusBarColor({ color })
} catch (error) {
log.error("Error setting status bar color:", error)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/plugins/safeAreaColoriOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { log } from "$lib/utils/Logger"
import { registerPlugin } from "@capacitor/core"
import { StatusBar, Style } from "@capacitor/status-bar"

interface IEchoPlugin {
echo(options: { color: string }): Promise<{ value: string }>
interface ISafeAreasColorPlugin {
changeSafeAreasColorOniOS(options: { color: string }): Promise<{ value: string }>
}

const Echo = registerPlugin<IEchoPlugin>("Echo")
const Echo = registerPlugin<ISafeAreasColorPlugin>("SafeAreasColor")

async function setNewSafeAreasColorOniOS(color: string) {
try {
Expand All @@ -23,7 +23,7 @@ async function setNewSafeAreasColorOniOS(color: string) {
log.debug("Change status bar style to dark")
}

await Echo.echo({ color })
await Echo.changeSafeAreasColorOniOS({ color })
} catch (error) {
log.error("Error trying to call swift function:", error)
}
Expand Down

0 comments on commit a5b2b1b

Please sign in to comment.