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

feat(Mobile-Android): Paint android safe areas #924

Merged
merged 4 commits into from
Dec 6, 2024
Merged
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
10 changes: 9 additions & 1 deletion android/app/src/main/java/com/uplink/app/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.uplink.app;

import com.getcapacitor.BridgeActivity;
import com.uplink.plugins.SafeAreaColorPlugin;
import android.os.Bundle;

public class MainActivity extends BridgeActivity {}
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(SafeAreaColorPlugin.class);
super.onCreate(savedInstanceState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.uplink.plugins;

import android.app.Activity;
import android.os.Build;
import android.view.View;
import android.view.Window;
import androidx.annotation.NonNull;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@CapacitorPlugin(name = "SafeAreaColorPlugin")
public class SafeAreaColorPlugin extends Plugin {

@PluginMethod
public void setStatusBarColor(PluginCall call) {
String color = call.getString("color");

if (color == null) {
call.reject("Color must be provided");
return;
}
getActivity().runOnUiThread(() -> {
try {
Window window = getActivity().getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(android.graphics.Color.parseColor(color));
}
call.resolve();
} catch (Exception e) {
call.reject("Failed to set status bar color", e);
}
});
}

@PluginMethod
public void setNavigationBarColor(PluginCall call) {
String color = call.getString("color");

if (color == null) {
call.reject("Color must be provided");
return;
}

getActivity().runOnUiThread(() -> {
try {
Window window = getActivity().getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
window.setNavigationBarColor(android.graphics.Color.parseColor(color));
}
call.resolve();
} catch (Exception e) {
call.reject("Failed to set navigation bar color", e);
}
});
}
}
32 changes: 32 additions & 0 deletions src/lib/plugins/safeAreaColorAndroid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { log } from "$lib/utils/Logger"
import { registerPlugin } from "@capacitor/core"

interface ISafeAreaColorPlugin {
setStatusBarColor(options: { color: string }): Promise<{ value: string }>
setNavigationBarColor(options: { color: string }): Promise<{ value: string }>
}

const SafeAreaColorPlugin = registerPlugin<ISafeAreaColorPlugin>("SafeAreaColorPlugin")

async function setStatusBarColor(color: string) {
try {
log.info("Calling native android function to change status bar color")
await SafeAreaColorPlugin.setStatusBarColor({ color })
} catch (error) {
log.error("Error setting status bar color:", error)
}
}

async function setNavigationBarColor(color: string) {
try {
log.info("Calling native android function to change navigation bar color")
await SafeAreaColorPlugin.setNavigationBarColor({ color })
} catch (error) {
log.error("Error setting navigation bar color:", error)
}
}

export function changeSafeAreaColorsOnAndroid(color: string) {
setStatusBarColor(color)
setNavigationBarColor(color)
}
8 changes: 8 additions & 0 deletions src/lib/utils/Mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ export function isAndroidOriOS(): boolean {
}
return platform === "ios" || platform === "android"
}

export function isAndroid(): boolean {
if (platform === null) {
log.warn("Platform info not yet loaded. Assuming 'false'.")
return false
}
return platform === "android"
}
15 changes: 14 additions & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import Market from "$lib/components/market/Market.svelte"
import { swipe } from "$lib/components/ui/Swipe"
import { ScreenOrientation } from "@capacitor/screen-orientation"
import { fetchDeviceInfo, isAndroidOriOS } from "$lib/utils/Mobile"
import { fetchDeviceInfo, isAndroid, isAndroidOriOS } from "$lib/utils/Mobile"
import { changeSafeAreaColorsOnAndroid } from "$lib/plugins/safeAreaColorAndroid"

log.debug("Initializing app, layout routes page.")

Expand Down Expand Up @@ -224,8 +225,19 @@
UIStore.state.theme.subscribe(f => {
theme = f
style = buildStyle()
changeSafeAreaColors()
})

function changeSafeAreaColors() {
setTimeout(() => {
if (isAndroid()) {
const rootStyles = getComputedStyle(document.documentElement)
let mainBgColor = rootStyles.getPropertyValue("--background").trim()
changeSafeAreaColorsOnAndroid(mainBgColor)
}
}, 1000)
}

SettingsStore.state.subscribe(settings => {
keybinds = settings.keybinds
devmode = settings.devmode
Expand Down Expand Up @@ -284,6 +296,7 @@
await checkIfUserIsLogged($page.route.id)
await initializeLocale()
buildStyle()
changeSafeAreaColors()
})
</script>

Expand Down
Loading