services ts - 2023 ionic
Tue Jun 18 2024 11:13:05 GMT+0000 (Coordinated Universal Time)
Saved by @iamkatmakhafola
//Services
//FakeSubscriptionDataService
import { Injectable } from "@angular/core";
import { Subscription } from "../tabs/Model/subscriptionModel";
@Injectable({
providedIn: 'root'
})
export class FakeSubscriptionDataService {
subscriptions : Subscription[];
constructor () {
this.subscriptions = [
{
id: 1,
name: "Netflix",
description: "At Netflix, we want to entertain the world. Whatever your taste, and no matter where you live, we give you access to best-in-class TV series, documentaries, feature films and mobile games.",
price : 199
},
{
id: 2,
name: "Showmax",
description: "Showmax is an internet TV service. What sets Showmax apart is a unique combination of hit African content, first and exclusive international series, movies, the best kids’ shows, and live sport.",
price : 349
},
{
id: 3,
name: "Amazon Prime Video",
description: "Amazon Prime Video, or simply Prime Video, is an American subscription video on-demand over-the-top streaming and rental service of Amazon offered as a standalone service or as part of Amazon's Prime subscription.",
price : 79
},
{
id: 4,
name: "Hulu",
description: "Hulu is an American subscription streaming service majority-owned by The Walt Disney Company, with Comcast's NBCUniversal holding a minority stake that is pending sale to Disney; which will make Hulu into a wholly owned subsidiary of Disney.",
price : 225
},
{
id: 5,
name: "Disney+",
description: "Disney+ is an American subscription video on-demand over-the-top streaming service owned and operated by the Disney Entertainment division of The Walt Disney Company.",
price : 119
},
];
}
getOfferedSubscriptions () {
return this.subscriptions;
}
}
//SubscriptionCartOrganiserService
import { Injectable } from "@angular/core";
import { Subject } from "rxjs";
import { Subscription } from "../tabs/Model/subscriptionModel";
import { CartSubScription } from "../tabs/Model/cartSubscription";
@Injectable({
providedIn: 'root'
})
export class SubscriptionCartOrganiserService {
static tmpSubscriptionsCartName : string = "ls-cart-subscriptions";
cartProductsNumberDS = new Subject<number>();
cartItemsOrderName : string = "Subs Order @ ";
notifyOnNewItemInCart() {
this.cartProductsNumberDS.next(this.getNumberOfItemsInCart());
}
getLocalStorageSubscriptions(): Subscription[] {
let storedSubString = localStorage.getItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName)
let cartSubscriptions = [];
if (storedSubString) {
cartSubscriptions = JSON.parse(storedSubString)
}
return cartSubscriptions;
}
getNumberOfItemsInCart() : number {
return this.getLocalStorageSubscriptions().length
}
getSubscriptionsInCart() : CartSubScription[] {
let localStorageSubs = this.getLocalStorageSubscriptions();
let cartSubscriptions : CartSubScription[] = [];
let subCounts = new Map<Number, Number>(); //temporary storage
localStorageSubs.forEach(sub => {
if (!subCounts.has(sub.id)) {
let count = localStorageSubs.filter(currSub => currSub.id == sub.id).length;
subCounts.set(sub.id, count)
let cartSub = new CartSubScription(sub, count);
cartSubscriptions.push(cartSub);
}
});
return cartSubscriptions;
}
getTotalCostOfSubcriptionsInCart() : number {
let totalCost = 0;
let cartSubs = this.getSubscriptionsInCart();
cartSubs.forEach(cartSub => {
totalCost += (cartSub.subscription.price * cartSub.quantity);
});
return totalCost;
}
getCartOrderName() {
return this.cartItemsOrderName + Date.now();
}
addSubscriptionToCart(product : Subscription) {
let storedSubString = localStorage.getItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName)
let cartSubscriptions = [];
if (storedSubString) {
cartSubscriptions = JSON.parse(storedSubString)
}
cartSubscriptions.push(product);
localStorage.setItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName, JSON.stringify(cartSubscriptions))
this.notifyOnNewItemInCart();
}
removeProdFromCart(subscr : Subscription) {
let storedSubString = localStorage.getItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName)
let cartSubscriptions = [];
if (storedSubString) {
cartSubscriptions = JSON.parse(storedSubString)
}
for (var idx = 0; idx < cartSubscriptions.length; idx++) {
if (cartSubscriptions[idx].id == subscr.id) {
cartSubscriptions.splice(idx, 1);
break;
}
}
localStorage.setItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName, JSON.stringify(cartSubscriptions))
this.notifyOnNewItemInCart();
}
addProdFromCart(subscr : Subscription) {
this.addSubscriptionToCart(subscr);
this.notifyOnNewItemInCart();
}
clearCart () {
localStorage.removeItem(SubscriptionCartOrganiserService.tmpSubscriptionsCartName);
this.notifyOnNewItemInCart();
}
}



Comments