From 688dce5813be25c24a09162a72d47a1eadcbf22d Mon Sep 17 00:00:00 2001 From: Abhishek Date: Jul 10 2018 08:16:25 +0000 Subject: add actionsheet logic --- diff --git a/src/pages/magazine/magazine.ts b/src/pages/magazine/magazine.ts index cce42c8..f73f9cf 100644 --- a/src/pages/magazine/magazine.ts +++ b/src/pages/magazine/magazine.ts @@ -1,8 +1,9 @@ import { Component } from '@angular/core'; -import { SocialSharing } from '@ionic-native/social-sharing'; import { Browser } from '../../providers/browser/browser'; import { FedoraMagazineService, Post } from '../../providers/fedora-magazine/fedora-magazine'; +import { NotificationsPage } from '../../pages/notifications/notifications'; +import { NavController, ActionSheetController, ToastController } from 'ionic-angular'; /** * Shows latest posts from Fedora Magazine @@ -18,47 +19,145 @@ export class MagazinePage { */ private posts: Post[]; + /** + * Number of posts + */ + + postCount = 15; + + //Initialize array for showing calendar icon + bookmarkIcon: string[] = []; + //set icon values + activeIcon: string = './assets/img/bookmark-active.svg'; + inactiveIcon: string = './assets/img/bookmark-inactive.svg'; + constructor(private browser: Browser, - private fedoraMag: FedoraMagazineService, private socialSharing: SocialSharing) { + private fedoraMag: FedoraMagazineService, public navCtrl: NavController, public actionSheetCtrl: ActionSheetController, public toastCtrl: ToastController) { + for (let i = 0; i < 50; i++) { + //update src of icon + this.bookmarkIcon[i] = this.inactiveIcon; + } } ngOnInit() { - this.updatePosts(); + this.updatePosts(this.postCount); } /** * Fetch latest posts from Fedor Magazine API */ - updatePosts(): void { - this.fedoraMag.getPosts() + updatePosts(postCount): void { + this.fedoraMag.getPosts(postCount) .subscribe(posts => { this.posts = posts; }); } /** + * load more posts + */ + loadMore(postCount):void{ + this.postCount += 5; + this.updatePosts(this.postCount); + } + + /** * Open a post in a browser * * Opens the post in an in-app browser in mobile app and in a new tab on desktop. * * @param post post to open */ - openPost(post:Post): void { + openPost(post: Post): void { this.browser.open(post.link); } /** - * Share the post using a third-party app installed in the user's device - * - * Allows to share the post using apps like WhatsApp, Facebook, or any app that - * exposes a share interface to the underlying OS. - * - * @param post post to share + * Create an action sheet to sort the articles + */ + + presentActionSheet() { + let actionSheet = this.actionSheetCtrl.create({ + title: 'SORT ARTICLES BY', + buttons: [ + { + /** + * Sort the articles by date in descending order + */ + text: 'Latest First', + handler: () => { + this.posts.sort(function (a: any, b: any): number { + let firstDate = new Date(a.publishedAt); + let secondDate = new Date(b.publishedAt); + return secondDate.getTime() - firstDate.getTime(); + }); + } + }, + { + /** + * Sort the articles by date in ascending order + */ + text: 'Oldest First', + handler: () => { + this.posts.sort(function (a: any, b: any): number { + let firstDate = new Date(a.publishedAt); + let secondDate = new Date(b.publishedAt); + return firstDate.getTime() - secondDate.getTime(); + }); + } + }, + { + /** + * Sort the articles by number of comments in descending order + */ + text: 'Comments', + handler: () => { + this.posts.sort(function (a: any, b: any): number { + return b.comments - a.comments; + }); + } + }, + ] + }); + /** + * Load the action sheet + */ + actionSheet.present(); + } + + + /** + * Open the notifications pane from the home page */ - sharePost(post:Post): void { - this.socialSharing.share( - post.excerpt, post.title, - null, post.permalink - ); + openNotificationPage() { + this.navCtrl.push(NotificationsPage, { animate: true, direction: 'forward' }); + } + + /** + * Function called when someone taps the star to subscribe to the calendar + */ + addToBookmark(post: Post, i: number): void { + /** + * Declare toasts for showing events + */ + const subscribedToast = this.toastCtrl.create({ + message: 'Saved the article for offline reading', + duration: 2000 + }); + const unsubscribedToast = this.toastCtrl.create({ + message: 'Removed the article from bookmarks', + duration: 2000 + }); + + /** + * Fire event on the basis of selected icon + */ + if (this.bookmarkIcon[i] === this.inactiveIcon) { + this.bookmarkIcon[i] = this.activeIcon; + subscribedToast.present(); + } else { + this.bookmarkIcon[i] = this.inactiveIcon; + unsubscribedToast.present(); + } } }