Skip to content

Commit

Permalink
feat(ios/android): allow to position Toast (#2337)
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce authored Jan 30, 2020
1 parent 9836d05 commit a1384d5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
12 changes: 12 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/plugin/Toast.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.getcapacitor.plugin;

import android.view.Gravity;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand All @@ -8,6 +9,9 @@
@NativePlugin()
public class Toast extends Plugin {

private static final int GRAVITY_TOP = Gravity.TOP|Gravity.CENTER_HORIZONTAL;
private static final int GRAVITY_CENTER = Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL;

@PluginMethod()
public void show(PluginCall call) {
CharSequence text = call.getString("text");
Expand All @@ -24,6 +28,14 @@ public void show(PluginCall call) {
}

android.widget.Toast toast = android.widget.Toast.makeText(getContext(), text, duration);

String position = call.getString("position", "bottom");
if("top".equals(position)) {
toast.setGravity(GRAVITY_TOP, 0, 40);
} else if("center".equals(position)) {
toast.setGravity(GRAVITY_CENTER, 0, 0);
}

toast.show();
call.success();
}
Expand Down
1 change: 1 addition & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ export interface ToastPlugin extends Plugin {
export interface ToastShowOptions {
text: string;
duration?: 'short' | 'long';
position?: 'top' | 'center' | 'bottom';
}

export interface WebViewPlugin extends Plugin {
Expand Down
4 changes: 3 additions & 1 deletion example/src/pages/toast/toast.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
</ion-header>

<ion-content padding>
<button (click)="show()" ion-button>Show</button>
<button (click)="show('top')" ion-button>Show (top)</button>
<button (click)="show('center')" ion-button>Show (center)</button>
<button (click)="show('bottom')" ion-button>Show (bottom)</button>
</ion-content>
5 changes: 3 additions & 2 deletions example/src/pages/toast/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export class ToastPage {
console.log('ionViewDidLoad ToastPage');
}

async show() {
async show(position: 'top' | 'center' | 'bottom') {
await Toast.show({
text: 'Hello!'
text: 'Hello!',
position,
});
}
}
16 changes: 14 additions & 2 deletions ios/Capacitor/Capacitor/Plugins/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CAPToastPlugin : CAPPlugin {
}
let durationStyle = call.get("durationStyle", String.self, "long")!
let duration = durationStyle == "short" ? 1500 : 3000
let position = call.get("position", String.self, "bottom")

DispatchQueue.main.async {
let vc = self.bridge!.viewController
Expand All @@ -34,11 +35,22 @@ public class CAPToastPlugin : CAPPlugin {
let minWidth = min(maxSizeTitle.width, expectedSizeTitle.width)
let minHeight = min(maxSizeTitle.height, expectedSizeTitle.height)
expectedSizeTitle = CGSize(width: minWidth, height: minHeight)

let height = expectedSizeTitle.height+32
let y: CGFloat
if (position == "top") {
y = 40
} else if (position == "center") {
y = (vc.view.bounds.size.height/2) - (height/2)
} else {
y = vc.view.bounds.size.height - height - (height/2)
}

lb.frame = CGRect(
x: ((vc.view.bounds.size.width)/2) - ((expectedSizeTitle.width+32)/2),
y: (vc.view.bounds.size.height-(expectedSizeTitle.height+32)) - ((expectedSizeTitle.height+32)/2),
y: y,
width: expectedSizeTitle.width+32,
height: expectedSizeTitle.height+32)
height: height)

lb.padding = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
self.toast = lb
Expand Down

0 comments on commit a1384d5

Please sign in to comment.