Skip to content

Commit

Permalink
- Fixes the 'cancel' feature: it wasn't working for all implementatio…
Browse files Browse the repository at this point in the history
…ns of the {N} view hierarchy (and modals).

- Also fixes toasts on iPad modals; they should be positioned at the bottom of the screen, not at the bottom of the modal.
  • Loading branch information
EddyVerbruggen committed May 18, 2019
1 parent 85095fa commit 387a67c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ demo/platforms
demo/node_modules
node_modules
.vscode
.ideas
.idea

.DS_Store

Expand Down
8 changes: 8 additions & 0 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ export function positionToast() {
toast.duration = ToastDuration.SHORT;
toast.show();
}

export function cancelToast() {
const toast = new Toasty('Canceling after 1 sec');
toast.show();
setTimeout(() => {
toast.cancel();
}, 1000);
}
3 changes: 3 additions & 0 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

<Label text="You can chain the toast methods for setting position and duration." class="h3 m-5" textWrap="true" />
<Button text="Chained Toast" tap="chainedToast" class="btn btn-pink" />

<Label text="You can cancel a Toast (hide it)." class="h3 m-5" textWrap="true" />
<Button text="Cancel Toast" tap="cancelToast" class="btn btn-pink" />
</StackLayout>
</ScrollView>
</Page>
36 changes: 21 additions & 15 deletions src/toasty.ios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./typings/objc!Toast.d.ts" />

import * as app from 'tns-core-modules/application';
import { device } from 'tns-core-modules/platform';
import { DeviceType } from 'tns-core-modules/ui/enums';
import * as frameModule from 'tns-core-modules/ui/frame';
import { ToastDuration, ToastPosition } from './toast.common';
export * from './toast.common';
Expand Down Expand Up @@ -49,24 +50,12 @@ export class Toasty {
if (!this._text) {
throw new Error('Text is not set');
} else {
if (!frameModule.topmost()) {
throw new Error('There is no topmost');
} else {
let viewController = frameModule.topmost().viewController;
let view = viewController.view;
if (viewController.presentedViewController) {
while (viewController.presentedViewController) {
viewController = viewController.presentedViewController;
}
view = viewController.view;
}
view.makeToast(this._text);
}
Toasty.getView().makeToast(this._text);
}
}

cancel() {
app.ios.rootController.view.hideToasts();
Toasty.getView().hideToasts();
}

setToastPosition(value: ToastPosition) {
Expand Down Expand Up @@ -103,4 +92,21 @@ export class Toasty {

return this;
}

private static getView(): any {
if (!frameModule.topmost()) {
throw new Error('There is no topmost');
} else {
let viewController = frameModule.topmost().viewController;
if (viewController.presentedViewController) {
// on iPad, we don't want to show the toast in the modal, but on iPhone we do
if (device.deviceType !== DeviceType.Tablet) {
while (viewController.presentedViewController) {
viewController = viewController.presentedViewController;
}
}
}
return viewController.view;
}
}
}

0 comments on commit 387a67c

Please sign in to comment.