Skip to content

Commit

Permalink
chore(cardwallet): ios fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
farfromrefug committed Nov 28, 2024
1 parent cea9e93 commit bde8ff5
Show file tree
Hide file tree
Showing 7 changed files with 1,336 additions and 1,293 deletions.
1 change: 1 addition & 0 deletions App_Resources/cardwallet/iOS/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugin 'cocoapods-acknowledgements'
pod "MSColorPicker"
platform :ios, '12.0'
post_install do |installer|
installer.pods_project.targets.each do |target|
Expand Down
1 change: 1 addition & 0 deletions App_Resources/documentscanner/iOS/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
plugin 'cocoapods-acknowledgements'
pod "MSColorPicker"
platform :ios, '12.0'
post_install do |installer|
installer.pods_project.targets.each do |target|
Expand Down
14 changes: 4 additions & 10 deletions app/components/widgets/CreateCard.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<script context="module" lang="ts">
import { lc } from '@nativescript-community/l';
import { DateTimePicker, DateTimePickerStyle } from '@nativescript/datetimepicker';
import { showError } from '@shared/utils/showError';
import { conditionalEvent } from '@shared/utils/svelte/ui';
import dayjs from 'dayjs';
import { ExtraFieldType } from '~/models/OCRDocument';
import { pickColor, pickDate, showPopoverMenu } from '~/utils/ui';
import { colors, fonts } from '~/variables';
import { formatDate, lang } from '~/helpers/locale';
import { SilentError } from '@shared/utils/error';
import { View } from '@nativescript/core';
import { showError } from '@shared/utils/showError';
import { FORMATS, getBarcodeFallbackString, qrcodeService } from '~/services/qrcode';
import { pickColor, showPopoverMenu } from '~/utils/ui';
import { colors } from '~/variables';
import SvgView from '../common/SVGView.svelte';
</script>

Expand All @@ -19,7 +13,7 @@
$: ({ colorOnBackground, colorOutline, colorPrimary } = $colors);
export let name = null;
export let color = null;
export let color = colorPrimary;
let code;
let codeFormat = FORMATS.QR_CODE;
let rootView;
Expand Down
24 changes: 9 additions & 15 deletions app/components/widgets/ExtraFieldPicker.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
<script context="module" lang="ts">
import { lc } from '@nativescript-community/l';
import { DateTimePicker, DateTimePickerStyle } from '@nativescript/datetimepicker';
import { SilentError } from '@shared/utils/error';
import { showError } from '@shared/utils/showError';
import { conditionalEvent } from '@shared/utils/svelte/ui';
import dayjs from 'dayjs';
import { formatDate } from '~/helpers/locale';
import { ExtraFieldType } from '~/models/OCRDocument';
import { pickDate, showPopoverMenu } from '~/utils/ui';
import { colors, fonts } from '~/variables';
import { formatDate, lang } from '~/helpers/locale';
import { SilentError } from '@shared/utils/error';
import { View } from '@nativescript/core';
import { colors } from '~/variables';
</script>

<script lang="ts">
Expand All @@ -20,7 +17,7 @@
export let name = null;
export let value = null;
export let type: ExtraFieldType = ExtraFieldType.Date;
let currentTime = dayjs(value);
let currentTime = value ? dayjs(value) : dayjs();
let currentValue = value;
let currentName = name;
let currentType = type;
Expand All @@ -29,15 +26,12 @@
async function selectDate(e) {
try {
DEV_LOG && console.log('selectDate', currentTime);
if (currentType === ExtraFieldType.Date) {
if (__IOS__) {
DateTimePicker.pickDate({ context: (e.object as View)._context, date: currentTime.toDate(), locale: lang });
} else {
const dayStart = currentTime.startOf('d');
const date = await pickDate(currentTime);
if (date && dayStart.valueOf() !== date) {
currentTime = dayjs(date);
}
const dayStart = currentTime.startOf('d');
const date = await pickDate(currentTime);
if (date && dayStart.valueOf() !== date) {
currentTime = dayjs(date);
}
}
} catch (error) {
Expand Down
128 changes: 87 additions & 41 deletions app/utils/ui/index.ios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Color, IOSHelper, View } from '@nativescript/core';
import { showSnack } from './index.common';
import { Dayjs } from 'dayjs';
import { lc } from '@nativescript-community/l';
import { DateTimePicker } from '@nativescript/datetimepicker';
import { lang } from '~/helpers/locale';

export * from './index.common';

Expand All @@ -18,33 +21,70 @@ export function checkIfCaonBackButtonnGoBack(view: View, callback) {

export function onAndroidNewItent() {}

@NativeClass
class UIPopoverPresentationControllerDelegateImpl extends NSObject implements UIPopoverPresentationControllerDelegate {
static ObjCProtocols = [UIPopoverPresentationControllerDelegate];
private _options: any;

static initWithOptions(options) {
const delegate = new UIPopoverPresentationControllerDelegateImpl();
delegate._options = options;
return delegate;
}

adaptivePresentationStyleForPresentationControllerTraitCollection(controller: UIPresentationController, traitCollection): UIModalPresentationStyle {
return UIModalPresentationStyle.None;
}

popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController): void {
if (this._options.onDismiss) {
this._options.onDismiss();
}
}

popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController): any {
return this._options?.outsideTouchable;
}
}

@NativeClass
class ColorPickerImpl extends NSObject {
onDone;
public static initWithOnDone(onDone): ColorPickerImpl {
const handler = ColorPickerImpl.new() as ColorPickerImpl;
handler.onDone = onDone;
return handler;
}

public dismissViewController(sender: any): void {
if (this.onDone) {
this.onDone(sender);
}
}

public static ObjCExposedMethods = {
dismissViewController: { returns: interop.types.void, params: [] }
};
}

export async function pickColor(color: Color | string, { alpha, anchor }: { alpha?: boolean; anchor?: View } = {}) {
return new Promise<Color>((resolve, reject) => {
try {
if (!(color instanceof Color)) {
color = new Color(color as any);
}
this._doneResolve = resolve;
//@ts-ignore
const colorSelectionController = new MSColorSelectionViewController(null);
colorSelectionController.color = color.ios;
const colorSelectionController: UIViewController = new MSColorSelectionViewController(null);
if (color) {
if (!(color instanceof Color)) {
color = new Color(color as any);
}
colorSelectionController['color'] = color.ios;
}

const controller = UINavigationController.alloc().initWithRootViewController(colorSelectionController);
controller.modalPresentationStyle = UIModalPresentationStyle.Popover;
controller.popoverPresentationController.sourceView = anchor.nativeViewProtected;
const bounds = anchor.nativeViewProtected.bounds;
const width = bounds.size.width;
const height = bounds.size.height;
const deltaX = 0;
const deltaY = 0;
controller.popoverPresentationController.sourceRect = CGRectOffset(bounds, Math.min(deltaX, width / 2), Math.min(deltaY, height / 2));
controller.popoverPresentationController.sourceRect = anchor.nativeViewProtected.bounds;
controller.preferredContentSize = colorSelectionController.view.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize);

function _onDismiss() {
//@ts-ignore
const color = controller.popoverPresentationController.delegate.color;
controller.popoverPresentationController.delegate = null;
resolve(color);
}
const delegate = (NSObject as any)
.extend(
{
Expand All @@ -54,6 +94,7 @@ export async function pickColor(color: Color | string, { alpha, anchor }: { alph
const green = Math.round(components[1] * 255);
const blue = Math.round(components[2] * 255);
const alpha = Math.round(components[3] * 255);
this.color = new Color(alpha, red, green, blue);
},
popoverPresentationControllerDidDismissPopover() {
_onDismiss();
Expand All @@ -66,15 +107,21 @@ export async function pickColor(color: Color | string, { alpha, anchor }: { alph
)
.alloc()
.init();
colorSelectionController.delegate = delegate;

// const doneBtn = UIBarButtonItem.alloc().initWithTitleStyleTargetAction(
// lc('done'),
// UIBarButtonItemStyle.Done,
// this._impl,
// 'dismissViewController'
// );
// colorSelectionController.navigationItem.rightBarButtonItem = doneBtn;
delegate.color = color;
colorSelectionController['delegate'] = delegate;

function _onDismiss() {
//@ts-ignore
const color = delegate.color;
controller.popoverPresentationController.delegate = null;
resolve(color);
}
if (!controller.popoverPresentationController.delegate) {
controller.popoverPresentationController.delegate = UIPopoverPresentationControllerDelegateImpl.initWithOptions({
outsideTouchable: true,
onDismiss: _onDismiss
});
}
let parentWithController = IOSHelper.getParentWithViewController(anchor);
if (!parentWithController) {
throw new Error('missing_parent_controller');
Expand All @@ -92,18 +139,17 @@ export async function pickColor(color: Color | string, { alpha, anchor }: { alph
});
}

export async function pickDate(currentDate: Dayjs) {
// TODO: show UIDatePicker in BottomSheet
// return new Promise<number>((resolve, reject) => {
// const datePicker = com.google.android.material.datepicker.MaterialDatePicker.Builder.datePicker().setTitleText(lc('pick_date')).setSelection(new java.lang.Long(currentDate.valueOf())).build();
// datePicker.addOnDismissListener(
// new android.content.DialogInterface.OnDismissListener({
// onDismiss: () => {
// resolve(datePicker.getSelection().longValue());
// }
// })
// );
// const parentView = Frame.topmost() || getRootView();
// datePicker.show(parentView._getRootFragmentManager(), 'datepicker');
// });
export async function pickDate(currentDate: Dayjs, context?) {
const result = await DateTimePicker.pickDate({
context,
date: currentDate.toDate(),
// minDate: currentDate.subtract(100, 'y').toDate(),
// maxDate: currentTime.add(100, 'y').toDate(),
locale: lang,
okButtonText: lc('ok'),
cancelButtonText: lc('cancel')
});
if (result) {
return result.valueOf();
}
}
9 changes: 8 additions & 1 deletion app/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ export const windowInset = writable({ top: 0, left: 0, right: 0, bottom: 0 });
export const actionBarButtonHeight = writable(0);
export const actionBarHeight = writable(0);

const startOrientation = __ANDROID__ ? Application.android['getOrientationValue'](Utils.android.getApplicationContext().getResources().getConfiguration()) : Application.orientation();
let startOrientation = __ANDROID__ ? Application.android['getOrientationValue'](Utils.android.getApplicationContext().getResources().getConfiguration()) : undefined;
if (__IOS__) {
Application.on(Application.launchEvent, async () => {
startOrientation = Application.orientation();
orientation.set(startOrientation);
isLandscape.set(startOrientation === 'landscape');
});
}
const startingInLandscape = startOrientation === 'landscape';
export const screenHeightDips = startingInLandscape ? Screen.mainScreen.widthDIPs : Screen.mainScreen.heightDIPs;
export const screenWidthDips = startingInLandscape ? Screen.mainScreen.heightDIPs : Screen.mainScreen.widthDIPs;
Expand Down
Loading

0 comments on commit bde8ff5

Please sign in to comment.