Skip to content

Commit

Permalink
feat: add top method
Browse files Browse the repository at this point in the history
  • Loading branch information
F0rsaken committed Sep 15, 2022
1 parent 48d03d0 commit c7f7bf3
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 46 deletions.
47 changes: 37 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,64 @@
# capacitor-plugin-android-insets

Capacitro plugin for retrieving proper top offset of Android status bar
Capacitor plugin for retrieving proper top offset of Android status bar.

This repo is published version with changed name of https://github.com/jorisbertomeu/capacitor-insets-v2, which was based on archived https://github.com/igorcd/capacitor-insets-plugin/. It should work perfectly fine in `Capacitor@4`.

## Install

```bash
npm install capacitor-plugin-android-insets
# with npm
npm install --save capacitor-plugin-android-insets
# with yarn
yarn add capacitor-plugin-android-insets
# after any install
npx cap sync
```

## Why?

This plugin is required only on Android when using `StatusBar.setOverlaysWebView({ overlay: true })`. There is a problem with top offset counting, when status bar is transparent. Related issue: https://github.com/ionic-team/capacitor/issues/2840.

## Usage

```ts
import { AndroidInsets } from 'capacitor-plugin-android-insets'

const { value } = await AndroidInsets.top();
```

## API

<docgen-index>

* [`echo(...)`](#echo)
* [`top()`](#top)
* [Interfaces](#interfaces)

</docgen-index>

<docgen-api>
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->

### echo(...)
### top()

Get top offset of status bar

```typescript
echo(options: { value: string; }) => Promise<{ value: string; }>
top() => Promise<TopReturn>
```

| Param | Type |
| ------------- | ------------------------------- |
| **`options`** | <code>{ value: string; }</code> |

**Returns:** <code>Promise&lt;{ value: string; }&gt;</code>
**Returns:** <code>Promise&lt;<a href="#topreturn">TopReturn</a>&gt;</code>

--------------------


### Interfaces


#### TopReturn

| Prop | Type |
| ----------- | ------------------- |
| **`value`** | <code>number</code> |

</docgen-api>
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package com.owlsdepartment.plugin.android.insets;

import android.util.Log;
import android.util.DisplayMetrics;
import androidx.appcompat.app.AppCompatActivity;

public class AndroidInsets {

public String echo(String value) {
Log.i("Echo", value);
return value;
private AppCompatActivity activity;

public AndroidInsets(AppCompatActivity activity) {
this.activity = activity;
}

public float getTop() {
DisplayMetrics metrics = this.activity.getResources().getDisplayMetrics();
int resourceId = this.activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
float titleBarHeight = 0;

if (resourceId > 0) {
titleBarHeight = this.activity.getResources().getDimensionPixelSize(resourceId);
}

return titleBarHeight / metrics.density;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.owlsdepartment.plugin.android.insets;

import android.app.Activity;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand All @@ -9,14 +10,19 @@
@CapacitorPlugin(name = "AndroidInsets")
public class AndroidInsetsPlugin extends Plugin {

private AndroidInsets implementation = new AndroidInsets();
private AndroidInsets implementation;

@PluginMethod
public void echo(PluginCall call) {
String value = call.getString("value");
@Override
public void load() {
this.implementation = new AndroidInsets(getActivity());
}

@PluginMethod
public void top(PluginCall call) {
float statusBarHeight = implementation.getTop();
JSObject ret = new JSObject();
ret.put("value", implementation.echo(value));

ret.put("value", statusBarHeight);
call.resolve(ret);
}
}
34 changes: 18 additions & 16 deletions example/src/js/capacitor-welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,26 @@ window.customElements.define(
connectedCallback() {
const self = this;

self.shadowRoot.querySelector('#take-photo').addEventListener('click', async function (e) {
try {
const photo = await Camera.getPhoto({
resultType: 'uri',
});
self.shadowRoot
.querySelector('#take-photo')
.addEventListener('click', async function (e) {
try {
const photo = await Camera.getPhoto({
resultType: 'uri',
});

const image = self.shadowRoot.querySelector('#image');
if (!image) {
return;
}
const image = self.shadowRoot.querySelector('#image');
if (!image) {
return;
}

image.src = photo.webPath;
} catch (e) {
console.warn('User cancelled', e);
}
});
image.src = photo.webPath;
} catch (e) {
console.warn('User cancelled', e);
}
});
}
}
},
);

window.customElements.define(
Expand Down Expand Up @@ -138,5 +140,5 @@ window.customElements.define(
<slot></slot>
`;
}
}
},
);
10 changes: 10 additions & 0 deletions ios/Plugin.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions ios/Plugin/AndroidInsets.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Foundation

@objc public class AndroidInsets: NSObject {
@objc public func echo(_ value: String) -> String {
print(value)
return value
@objc public func top() -> Float {
return 0
}
}
5 changes: 2 additions & 3 deletions ios/Plugin/AndroidInsetsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import Capacitor
public class AndroidInsetsPlugin: CAPPlugin {
private let implementation = AndroidInsets()

@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
@objc func top(_ call: CAPPluginCall) {
call.resolve([
"value": implementation.echo(value)
"value": implementation.top()
])
}
}
6 changes: 5 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface AndroidInsetsPlugin {
echo(options: { value: string }): Promise<{ value: string }>;
top(): Promise<TopReturn>;
}

export interface TopReturn {
value: number;
}
7 changes: 3 additions & 4 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { WebPlugin } from '@capacitor/core';

import type { AndroidInsetsPlugin } from './definitions';
import type { AndroidInsetsPlugin, TopReturn } from './definitions';

export class AndroidInsetsWeb extends WebPlugin implements AndroidInsetsPlugin {
async echo(options: { value: string }): Promise<{ value: string }> {
console.log('ECHO', options);
return options;
async top(): Promise<TopReturn> {
return { value: 0 };
}
}

0 comments on commit c7f7bf3

Please sign in to comment.