Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-05-07 23:28:12 +03:30
parent b2a1eb8e5b
commit ce40bd8c75
54 changed files with 260 additions and 183 deletions
+47 -29
View File
@@ -1,5 +1,4 @@
import { inject, Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { Maybe } from '../models';
import { ToastService } from './toast.service';
@@ -42,17 +41,22 @@ export class NativeBridgeService {
}
isEnabled(): boolean {
if (!this.host) return false;
const hostEnabled = this.host?.isEnabled;
if (typeof hostEnabled === 'function') {
try {
return !!hostEnabled();
} catch {
return false;
}
}
// @ts-ignore
return !!window.NativeBridge;
// if (window.NativeBridge) {
return !!environment.enableNativeBridge && !!this.host;
// }
// if (!this.host) return false;
// const hostEnabled = this.host?.isEnabled;
// if (typeof hostEnabled === 'function') {
// try {
// return !!hostEnabled();
// } catch {
// return false;
// }
// }
// return !!environment.enableNativeBridge && !!this.host;
}
canPay(): boolean {
@@ -64,22 +68,40 @@ export class NativeBridgeService {
}
pay(request: INativePayRequest): any {
this.toastService.info({ text: 'در حال پردازش پرداخت...', life: 3000 });
const fn = this.host?.pay;
if (typeof fn !== 'function') {
this.toastService.error({ text: 'متاسفانه پرداخت امکان‌پذیر نیست.', life: 3000 });
return { success: false, error: 'متاسفانه پرداخت امکان‌پذیر نیست.' };
if (request.amount <= 10_000) {
const errorMessage = 'برای مقادیر زیر ۱۰۰ هزار ریال، پرداخت ممکن نیست.';
this.toastService.error({ text: errorMessage, life: 3000 });
return {
success: false,
error: errorMessage,
};
}
this.toastService.info({ text: 'در حال پردازش پرداخت...', life: 3000 });
try {
fn(request.amount, request.id);
// @ts-ignore
window.NativeBridge.pay(request.amount, request.id || '');
return { success: true };
// return { success: true, data: parsed ?? raw };
} catch (error) {
this.toastService.info({ text: (error as Error).message, life: 30000 });
return { success: false, error: (error as Error).message };
}
// const fn = window.NativeBridge.pay(123, 'test');
// if (typeof fn !== 'function') {
// this.toastService.error({ text: 'متاسفانه پرداخت امکان‌پذیر نیست.', life: 3000 });
// return { success: false, error: 'متاسفانه پرداخت امکان‌پذیر نیست.' };
// }
// try {
// fn(123, 'test');
// // fn(request.amount, request.id || '');
// return { success: true };
// // return { success: true, data: parsed ?? raw };
// } catch (error) {
// this.toastService.info({ text: (error as Error).message, life: 30000 });
// return { success: false, error: (error as Error).message };
// }
}
print(request: INativePrintRequest): INativeBridgeResult {
@@ -87,17 +109,13 @@ export class NativeBridgeService {
}
private invokePrint(payload: INativePrintRequest): INativeBridgeResult {
const fn = this.host?.print;
if (typeof fn !== 'function') {
return { success: false, error: 'متاسفانه چاپ امکان‌پذیر نیست.' };
}
try {
fn(payload);
// @ts-ignore
window.NativeBridge.print(JSON.stringify([payload]));
return { success: true };
} catch (error) {
return { success: false, error: 'متاسفانه چاپ امکان‌پذیر نیست.' };
this.toastService.info({ text: (error as Error).message, life: 30000 });
return { success: false, error: (error as Error).message };
}
}