|
@@ -1,77 +1,10 @@
|
|
|
-// import { Component, OnInit } from '@angular/core';
|
|
|
-// import { AlertController } from '@ionic/angular';
|
|
|
-// @Component({
|
|
|
-// selector: 'app-memo',
|
|
|
-// templateUrl: './memo.page.html',
|
|
|
-// styleUrls: ['./memo.page.scss'],
|
|
|
-// })
|
|
|
-// export class MemoPage implements OnInit {
|
|
|
-// segment: string = 'memo';
|
|
|
-// memos: string[] = [];
|
|
|
-// newMemo: string = '';
|
|
|
-// newMessage: string = '';
|
|
|
-// constructor(private alertController: AlertController) {}
|
|
|
-// ngOnInit() {
|
|
|
-// this.loadMemos();
|
|
|
-// }
|
|
|
-// //加载备忘录
|
|
|
-// loadMemos() {
|
|
|
-// const memos = localStorage.getItem('memos');
|
|
|
-// if (memos) {
|
|
|
-// this.memos = JSON.parse(memos);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// //保存备忘录
|
|
|
-// saveMemos() {
|
|
|
-// localStorage.setItem('memos', JSON.stringify(this.memos));
|
|
|
-// }
|
|
|
-// //异步函数,添加备忘录
|
|
|
-// async addMemo() {
|
|
|
-// if (this.newMemo.trim().length > 0) {
|
|
|
-// this.memos.push(this.newMemo);
|
|
|
-// this.newMemo = '';
|
|
|
-// this.saveMemos();
|
|
|
-// const alert = await this.alertController.create({
|
|
|
-// header: '提示',
|
|
|
-// message: '添加成功',
|
|
|
-// buttons: ['确定']
|
|
|
-// });
|
|
|
-// await alert.present();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// //异步函数,确认删除
|
|
|
-// async confirmDeleteMemo(memo: string) {
|
|
|
-// const alert = await this.alertController.create({
|
|
|
-// header: '确认删除',
|
|
|
-// message: `你确定要删除 "${memo}" 吗?`,
|
|
|
-// buttons: [
|
|
|
-// {
|
|
|
-// text: '取消',
|
|
|
-// role: 'cancel'
|
|
|
-// },
|
|
|
-// {
|
|
|
-// text: '删除',
|
|
|
-// handler: () => {
|
|
|
-// this.memos = this.memos.filter(m => m !== memo);
|
|
|
-// this.saveMemos();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// ]
|
|
|
-// });
|
|
|
-// await alert.present();
|
|
|
-// }
|
|
|
-// //删除备忘录
|
|
|
-// deleteMemo(memo: string) {
|
|
|
-// this.memos = this.memos.filter(m => m !== memo);
|
|
|
-// this.saveMemos();
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { AlertController } from '@ionic/angular';
|
|
|
+import * as Parse from 'parse';
|
|
|
|
|
|
interface Memo {
|
|
|
+ memoId: number;
|
|
|
+ username: string;
|
|
|
content: string;
|
|
|
timestamp: Date;
|
|
|
}
|
|
@@ -85,39 +18,97 @@ export class MemoPage implements OnInit {
|
|
|
segment: string = 'memo';
|
|
|
memos: Memo[] = [];
|
|
|
newMemo: string = '';
|
|
|
+ username: string = ''; // Current user's username
|
|
|
+ nextMemoId: number = 1; // Track next memoId for the current user
|
|
|
|
|
|
constructor(private alertController: AlertController) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
- this.loadMemos();
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (currentUser) {
|
|
|
+ this.username = currentUser.getUsername()!;
|
|
|
+ this.loadMemos();
|
|
|
+ this.loadNextMemoId(); // Load the next memoId for the current user
|
|
|
+ } else {
|
|
|
+ console.error('User not logged in.');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- loadMemos() {
|
|
|
- const memos = localStorage.getItem('memos');
|
|
|
- if (memos) {
|
|
|
- this.memos = JSON.parse(memos);
|
|
|
+ ionViewDidEnter() {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ if (currentUser) {
|
|
|
+ this.username = currentUser.getUsername()!;
|
|
|
+ this.loadMemos();
|
|
|
+ this.loadNextMemoId(); // Load the next memoId for the current user
|
|
|
+ } else {
|
|
|
+ console.error('User not logged in.');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- saveMemos() {
|
|
|
- localStorage.setItem('memos', JSON.stringify(this.memos));
|
|
|
+ async loadNextMemoId() {
|
|
|
+ try {
|
|
|
+ const query = new Parse.Query('memo_history');
|
|
|
+ query.equalTo('username', this.username);
|
|
|
+ query.descending('memoId');
|
|
|
+ query.limit(1); // Limit to the last memo to get the highest memoId
|
|
|
+ const result = await query.first();
|
|
|
+
|
|
|
+ if (result) {
|
|
|
+ this.nextMemoId = result.get('memoId') + 1;
|
|
|
+ } else {
|
|
|
+ this.nextMemoId = 1; // Initialize to 1 if no memos exist for the user
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error loading next memoId', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async loadMemos() {
|
|
|
+ try {
|
|
|
+ const query = new Parse.Query('memo_history');
|
|
|
+ query.equalTo('username', this.username);
|
|
|
+ query.descending('createdAt');
|
|
|
+ query.limit(3); // Limit to latest three memos
|
|
|
+ const results = await query.find();
|
|
|
+
|
|
|
+ this.memos = results.map((memo: any) => ({
|
|
|
+ memoId: memo.get('memoId'),
|
|
|
+ username: memo.get('username'),
|
|
|
+ content: memo.get('content'),
|
|
|
+ timestamp: memo.get('time'),
|
|
|
+ }));
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error loading memos', error);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
async addMemo() {
|
|
|
if (this.newMemo.trim().length > 0) {
|
|
|
- const newMemoObj: Memo = {
|
|
|
- content: this.newMemo,
|
|
|
- timestamp: new Date(),
|
|
|
- };
|
|
|
- this.memos.push(newMemoObj);
|
|
|
- this.newMemo = '';
|
|
|
- this.saveMemos();
|
|
|
- const alert = await this.alertController.create({
|
|
|
- header: '提示',
|
|
|
- message: '添加成功',
|
|
|
- buttons: ['确定']
|
|
|
- });
|
|
|
- await alert.present();
|
|
|
+ const MemoClass = Parse.Object.extend('memo_history');
|
|
|
+ const memo = new MemoClass();
|
|
|
+
|
|
|
+ memo.set('username', this.username);
|
|
|
+ memo.set('memoId', this.nextMemoId); // Assign the next available memoId
|
|
|
+ memo.set('content', this.newMemo);
|
|
|
+ memo.set('time', new Date());
|
|
|
+
|
|
|
+ try {
|
|
|
+ await memo.save();
|
|
|
+ this.loadMemos(); // Reload memos after adding a new one
|
|
|
+ this.newMemo = '';
|
|
|
+ this.nextMemoId++; // Increment the next memoId for future memos
|
|
|
+
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '提示',
|
|
|
+ message: '添加成功',
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error saving memo', error);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -141,8 +132,19 @@ export class MemoPage implements OnInit {
|
|
|
await alert.present();
|
|
|
}
|
|
|
|
|
|
- deleteMemo(memo: Memo) {
|
|
|
- this.memos = this.memos.filter(m => m !== memo);
|
|
|
- this.saveMemos();
|
|
|
+ async deleteMemo(memo: Memo) {
|
|
|
+ const MemoClass = Parse.Object.extend('memo_history');
|
|
|
+ const query = new Parse.Query(MemoClass);
|
|
|
+ query.equalTo('memoId', memo.memoId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ const result = await query.first();
|
|
|
+ if (result) {
|
|
|
+ await result.destroy();
|
|
|
+ this.loadMemos(); // Reload memos after deleting one
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error deleting memo', error);
|
|
|
+ }
|
|
|
}
|
|
|
}
|