|
@@ -1,6 +1,7 @@
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { Router } from '@angular/router';
|
|
|
import * as Parse from 'parse';
|
|
|
+import { IonRefresher } from '@ionic/angular';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-home',
|
|
@@ -10,7 +11,7 @@ import * as Parse from 'parse';
|
|
|
export class HomePage implements OnInit {
|
|
|
items: any[] = [];
|
|
|
filteredItems: any[] = [];
|
|
|
- newPostId: string = '';
|
|
|
+ selectedSegment: string = 'recommendations';
|
|
|
|
|
|
constructor(private router: Router) {}
|
|
|
|
|
@@ -18,6 +19,10 @@ export class HomePage implements OnInit {
|
|
|
this.fetchPosts();
|
|
|
}
|
|
|
|
|
|
+ ionViewWillEnter() {
|
|
|
+ this.fetchPosts();
|
|
|
+ }
|
|
|
+
|
|
|
async fetchPosts() {
|
|
|
const Post = Parse.Object.extend("Post240709");
|
|
|
const query = new Parse.Query(Post);
|
|
@@ -30,14 +35,32 @@ export class HomePage implements OnInit {
|
|
|
author: post.get('author'),
|
|
|
image: post.get('image'),
|
|
|
description: post.get('description'),
|
|
|
+ likes: post.get('likes') || 0,
|
|
|
+ comments: post.get('comments') || 0,
|
|
|
+ createdAt: post.createdAt,
|
|
|
footer: `${post.get('likes') || 0} 赞同 · ${post.get('comments') || 0} 评论`
|
|
|
}));
|
|
|
- this.filteredItems = [...this.items]; // 初始化时展示全部帖子
|
|
|
+ this.updateFilteredItems();
|
|
|
} catch (error) {
|
|
|
console.error("Error while fetching posts", error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ updateFilteredItems() {
|
|
|
+ if (this.selectedSegment === 'recommendations') {
|
|
|
+ this.filteredItems = [...this.items];
|
|
|
+ } else if (this.selectedSegment === 'hot') {
|
|
|
+ this.filteredItems = [...this.items].sort((a, b) => b.likes - a.likes);
|
|
|
+ } else if (this.selectedSegment === 'newest') {
|
|
|
+ this.filteredItems = [...this.items].sort((a, b) => b.createdAt - a.createdAt);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ onSegmentChanged(event: any) {
|
|
|
+ this.selectedSegment = event.detail.value;
|
|
|
+ this.updateFilteredItems();
|
|
|
+ }
|
|
|
+
|
|
|
onSearch(event: any) {
|
|
|
const query = event.target.value.toLowerCase();
|
|
|
this.filteredItems = this.items.filter(item =>
|
|
@@ -105,4 +128,12 @@ export class HomePage implements OnInit {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ async doRefresh(event: CustomEvent) {
|
|
|
+ await this.fetchPosts();
|
|
|
+ const refresher = event.target as unknown as IonRefresher;
|
|
|
+ if (refresher) {
|
|
|
+ refresher.complete();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|