refactor: route localStorage access through shared LocalStorageService

Sprint 2 high-priority cleanup: cart/language/location services called
localStorage directly, bypassing the try/catch safety and core/<domain>
pattern used elsewhere (e.g. ProjectEditorDraftStorageService). New
core/storage/LocalStorageService centralizes get/set/remove and JSON
helpers with private-mode/quota error handling, reused across all three.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
sdarbinyan
2026-07-15 02:58:39 +04:00
parent 96a338ef6f
commit 3cf732797c
4 changed files with 73 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ import { Injectable, signal, computed, effect, Injector } from '@angular/core';
import { DeliveryOption, CartItem } from '../models';
import { getDiscountedPrice } from '../utils/item.utils';
import { normalizeDeliveryOption, normalizeOptionalNumber } from '../utils/normalization.utils';
import { LocalStorageService } from '../core/storage/local-storage.service';
import type { } from '../types/telegram.types';
type CartVariant = { colour?: string; size?: string; price?: number; currency?: string };
@@ -49,7 +50,7 @@ export class CartService {
&& items.some(item => (item.deliveryOptions?.length ?? 0) > 0 || item.selectedDelivery != null);
});
constructor(private injector: Injector) {
constructor(private injector: Injector, private readonly storage: LocalStorageService) {
this.loadCart();
// Auto-save whenever cart changes (skip the initial empty state)
@@ -131,10 +132,10 @@ export class CartService {
private saveToStorage(items: CartItem[]): void {
const data = JSON.stringify(items);
// Always save to localStorage
localStorage.setItem(this.STORAGE_KEY, data);
this.storage.setItem(this.STORAGE_KEY, data);
// Also save to Telegram CloudStorage if available
if (this.isTelegram) {
window.Telegram!.WebApp.CloudStorage.setItem(this.STORAGE_KEY, data, (err) => {
@@ -167,7 +168,7 @@ export class CartService {
}
private loadFromLocalStorage(): void {
const stored = localStorage.getItem(this.STORAGE_KEY);
const stored = this.storage.getItem(this.STORAGE_KEY);
if (stored) {
this.parseAndSetCart(stored);
}