From 36bd892d12251a72d790e06b1610c854548629b1 Mon Sep 17 00:00:00 2001
From: yz <221840196@smail.nju.edu.cn>
Date: Sat, 18 Jan 2025 03:23:43 +0800
Subject: [PATCH] feat: todolist

---
 entry/src/main/ets/pages/Index.ets           |  3 +-
 entry/src/main/ets/pages/LocalPage.ets       | 13 +++
 entry/src/main/ets/public/CommonConstant.ets |  4 +-
 entry/src/main/ets/public/WebAPI.ets         | 20 ++---
 entry/src/main/ets/view/AddItem.ets          | 87 ++++++-------------
 entry/src/main/ets/view/ItemDetail.ets       | 52 +++--------
 entry/src/main/ets/view/TodoItem.ets         | 90 ++------------------
 7 files changed, 73 insertions(+), 196 deletions(-)

diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets
index df3ce8f..b87e9b6 100644
--- a/entry/src/main/ets/pages/Index.ets
+++ b/entry/src/main/ets/pages/Index.ets
@@ -16,7 +16,8 @@ function calUncompleted(tasks: Array<TodoItemModel>) {
 function calImmediate(tasks: Array<TodoItemModel>) {
   let num = 0
   for(const item of tasks) {
-    if(item.ddl.substring(0,4)=='2025' && item.ddl.substring(5,7)=='01' && Number.parseInt(item.ddl.substring(5,7))-18<=7) num++
+    if(!item.isDone && item.ddl.substring(0,4)=='2025' && item.ddl.substring(5,7)=='01'
+      && Number.parseInt(item.ddl.substring(8))>=18 && Number.parseInt(item.ddl.substring(8))-18<=7) num++
   }
   return num
 }
diff --git a/entry/src/main/ets/pages/LocalPage.ets b/entry/src/main/ets/pages/LocalPage.ets
index ebc37a4..e84273f 100644
--- a/entry/src/main/ets/pages/LocalPage.ets
+++ b/entry/src/main/ets/pages/LocalPage.ets
@@ -52,6 +52,7 @@ export struct LocalPage {
     taskpool.execute(calImmediate, this.totalTasks).then((ret) => {
       if(ret.toString()) this.immediateCnt = ret as number
     })
+    this.deal()
   }
 
   build() {
@@ -146,4 +147,16 @@ export struct LocalPage {
     .navDestination(this.PageMap)
     .mode(NavigationMode.Stack)
   }
+
+  deal() {
+    let num1 = 0
+    let num2 = 0
+    for(const item of this.totalTasks) {
+      if(!item.isDone) num1++
+      if(!item.isDone && item.ddl.substring(0,4)=='2025' && item.ddl.substring(5,7)=='01'
+        && Number.parseInt(item.ddl.substring(8))>=18 && Number.parseInt(item.ddl.substring(8))-18<=7) num2++
+    }
+    this.uncompletedCnt = num1
+    this.immediateCnt = num2
+  }
 }
\ No newline at end of file
diff --git a/entry/src/main/ets/public/CommonConstant.ets b/entry/src/main/ets/public/CommonConstant.ets
index 71351ed..6cc25f7 100644
--- a/entry/src/main/ets/public/CommonConstant.ets
+++ b/entry/src/main/ets/public/CommonConstant.ets
@@ -7,7 +7,7 @@ export default class CommonConstants {
   //浜嬮」妗嗙殑瀹�/楂樺害
   static readonly list_width = '93.3%'
   static readonly list_height = '64vp'
-  static readonly list_margin_top = '13vp'
+  static readonly list_margin_top = '16vp'
   static readonly list_margin_bottom = '64vp'
 
   //浜嬮」鎵撳嬀鎸夐挳
@@ -41,7 +41,7 @@ export default class CommonConstants {
   //涓棿鏍囬鎵€鍗犲搴�
   static readonly mid_title_col_width = '70%'
   //璇︽儏
-  static readonly detail_margin_Left = '30'
+  static readonly detail_margin_Left = '10'
   static readonly detail_width = '40%'
   //璇︽儏妗�
   static readonly name_height = '80vp'
diff --git a/entry/src/main/ets/public/WebAPI.ets b/entry/src/main/ets/public/WebAPI.ets
index e1080ac..bbe3e0e 100644
--- a/entry/src/main/ets/public/WebAPI.ets
+++ b/entry/src/main/ets/public/WebAPI.ets
@@ -150,10 +150,11 @@ export default class webAPI {
         if (Array.isArray(resultJson.result)) {
           resultValue = resultJson.result
         }
+        console.log(String(JSON.stringify(data)))
         return resultValue
       }
       // for debug
-      // console.log(String(JSON.stringify(data)))
+      console.log(String(JSON.stringify(data)))
     } catch (error) {
       console.error('Login API error:', error)
     } finally {
@@ -203,28 +204,27 @@ export default class webAPI {
     return result
   }
 
-  static async DoneSingleItem(id:number): Promise<string> {
+  static async DoneSingleItem(id:number, done:boolean): Promise<string> {
     /*
-      鏂板缓浜嬮」锛岃繑鍥瀞tring绫诲瀷缁撴灉
+      鏍囪浜嬮」宸�/鏈畬鎴愶紝杩斿洖string绫诲瀷缁撴灉
       * 1. 鎴愬姛锛歴uccess
       * 2. 澶辫触锛歟rror
     */
     if(webAPI.token == '') {
-      promptAction.showToast({ message: '鏈櫥褰曪紝娣诲姞浜嬮」澶辫触' })
+      promptAction.showToast({ message: '鏈櫥褰曪紝鏍囪浜嬮」澶辫触' })
       return 'error'
     }
     let httpRequest = http.createHttp()
     let result = 'error' // 榛樿杩斿洖 error
     try {
-      const data = await httpRequest.request('localhost:8080/api/todolist/add', {
+      const data = await httpRequest.request('localhost:8080/api/todolist/update', {
         method: http.RequestMethod.POST,
         header: {
           'token': webAPI.token
         },
         extraData: {
           "id": id,
-          "isDone": 'TRUE',
-          "userId": webAPI.userId
+          "isDone": done,
         },
       })
       if (data.responseCode === 200) {
@@ -276,14 +276,14 @@ export default class webAPI {
 
   static async DeleteSingleItem(id:number): Promise<string> {
     /*
-      鐢ㄦ埛鐧诲綍锛岃繑鍥瀞tring绫诲瀷缁撴灉
+      鍒犻櫎浜嬮」锛岃繑鍥瀞tring绫诲瀷缁撴灉
       * 1. 鎴愬姛锛歴uccess
-      * 2. 澶辫触锛歠ailed锛堝ぇ姒傜巼鏄墜鏈哄彿/瀵嗙爜閿欒锛�
+      * 2. 澶辫触锛歠ailed||error
     */
     let httpRequest = http.createHttp()
     let result = ''  // 鍒濆鍊间负绌哄瓧绗︿覆
     try {
-      const data = await httpRequest.request(`localhost:8080/api/todolist/update?id=${id}`, {
+      const data = await httpRequest.request(`localhost:8080/api/todolist/delete?id=${id}`, {
         method: http.RequestMethod.POST,
         header: {
           'token': webAPI.token
diff --git a/entry/src/main/ets/view/AddItem.ets b/entry/src/main/ets/view/AddItem.ets
index 0376485..23a0508 100644
--- a/entry/src/main/ets/view/AddItem.ets
+++ b/entry/src/main/ets/view/AddItem.ets
@@ -1,19 +1,12 @@
 // 娣诲姞浜嬮」椤甸潰
 import CommonConstants from '../public/CommonConstant'
-import { http } from '@kit.NetworkKit'
 import { promptAction } from '@kit.ArkUI';
 import webAPI from '../public/WebAPI';
 
-interface SysTimeResponse {
-  sysTime1: string;
-  sysTime2: string;
-}
-
 @Component
-
 export default struct AddItem {
   @Consume('localPageStack') LocalPageStack: NavPathStack
-  selectedDate: Date = new Date("2025-1-18")
+  @State selectedDate: Date = new Date("2025-1-18")
   @State itemName: string = ''
   @State itemDisc: string = ''
   @State isMilitaryTime: boolean = false
@@ -76,7 +69,6 @@ export default struct AddItem {
           })
       }
     }
-
   }
 
   @Builder
@@ -84,13 +76,13 @@ export default struct AddItem {
     Column() {
       // 鏍囬鍜岃緭鍏ユ琛�
       Row() {
-        Text('鏍囬')
+        Text('鍚嶇О')
           .fontSize('23fp')
           .fontWeight(FontWeight.Bold)
           .width('15%')
-          .margin({ left: CommonConstants.detail_margin_Left })
+          .margin({ left: CommonConstants.detail_margin_Left, top: 6 })
 
-        TextInput({ placeholder: '璇疯緭鍏ヤ簨椤瑰悕瀛�' })
+        TextInput({ placeholder: '璇疯緭鍏ヤ簨椤瑰悕绉�' })
           .width('70%')
           .margin({ left: CommonConstants.input_margin_left })
           .backgroundColor('#EDEDED') // 璁剧疆鑳屾櫙閫忔槑
@@ -109,25 +101,15 @@ export default struct AddItem {
       .justifyContent(FlexAlign.Center)
       .alignItems(VerticalAlign.Top)
 
-
-      // 璇︽儏杈撳叆妗�
       Row() {
         Text('璇︽儏')
-          .fontSize('22fp')
+          .fontSize('23fp')
           .fontWeight(FontWeight.Bold)
           .width('15%')
-          .margin({ left: 40 })
+          .margin({ left: CommonConstants.detail_margin_Left, top: 6 })
 
-      }
-      .margin({ top: CommonConstants.list_margin_top })
-      .width(CommonConstants.list_width)
-      .height('20vp')
-      .borderRadius(CommonConstants.border_radius)
-      .justifyContent(FlexAlign.Start)
-
-      Row() {
-        TextArea({ placeholder: '璇疯緭鍏ヤ簨椤规弿杩�' })
-          .width('90%')
+        TextArea({ placeholder: '璇疯緭鍏ヤ簨椤硅鎯�' })
+          .width('70%')
           .height('100%')
           .margin({ left: CommonConstants.input_margin_left })
           .backgroundColor('#EDEDED') // 璁剧疆鑳屾櫙閫忔槑
@@ -139,17 +121,22 @@ export default struct AddItem {
             this.itemDisc = value;
           })
       }
-      .margin({ top: CommonConstants.list_margin_top })
       .width(CommonConstants.list_width)
-      .height('350vp')
+      .height('300vp')
       .borderRadius(CommonConstants.border_radius)
       .justifyContent(FlexAlign.Center)
       .alignItems(VerticalAlign.Top)
 
 
       Row() {
+        Text(this.handleDate(this.selectedDate))
+          .width('40%')
+          .fontSize(20)
+          .padding(5)
+          .textAlign(TextAlign.Center)
+          .margin({right:20})
+
         Button("閫夋嫨鎴鏃ユ湡")
-          .margin({ left: 40 })
           .onClick(() => {
             DatePickerDialog.show({
               start: new Date("2025-1-18"),
@@ -169,47 +156,20 @@ export default struct AddItem {
             })
           })
           .height('50pv')
-          .width('50%')
+          .width('45%')
 
       }
       .margin({ top: CommonConstants.list_margin_top })
       .width(CommonConstants.list_width)
       .height('80vp')
       .borderRadius(CommonConstants.border_radius)
-      .justifyContent(FlexAlign.End)
+      .justifyContent(FlexAlign.Center)
 
     }
   }
 
-/*  private async requestTime(): Promise<string> { //Todo:杩欎釜鏄姹傚綋鍓嶆椂闂达紝娌℃湁鎰忎箟锛岄渶瑕佹坊鍔犱竴涓緭鍏dl鐨勬棩鏈熺粍浠�
-    let httpRequest = http.createHttp()
-    let res = ''
-    try {
-      const data = await httpRequest.request('http://quan.suning.com/getSysTime.do', { // 鑻忓畞鏃堕棿鎴矨PI
-        method: http.RequestMethod.GET
-      })
-      if (data.responseCode === 200) {
-        let response = data.result + ""
-        console.info('Response: ' + response)
-        const json_res = JSON.parse(data.result + "") as SysTimeResponse
-        res = json_res.sysTime2.substring(0, 10)
-      } else {
-        console.error('Request failed with response code: ' + data.responseCode)
-      }
-    } catch (err) {
-      console.error('Request error: ' + JSON.stringify(err))
-    } finally {
-      httpRequest.destroy()
-    }
-    return res
-  }*/
-
-  private async addItem() {
-/*    let time = ''
-    while(time == '') {
-      time = await this.requestTime()
-    }*/
-    const res = await webAPI.addSingleItem(this.itemName, this.selectedDate.toLocaleDateString('en-CA'), this.itemDisc)
+  async addItem() {
+    const res = await webAPI.addSingleItem(this.itemName, this.handleDate(this.selectedDate), this.itemDisc)
     if(res == 'error') {
       promptAction.showToast({
         message: '娣诲姞澶辫触锛�'
@@ -222,4 +182,11 @@ export default struct AddItem {
       this.LocalPageStack.pop({}, true)
     }
   }
+
+  handleDate(date:Date) {
+    const year = date.getFullYear();
+    const month = (date.getMonth() + 1).toString().padStart(2, '0');  // 鏈堜唤浠�0寮€濮嬶紝闇€瑕佸姞1
+    const day = date.getDate().toString().padStart(2, '0');
+    return `${year}-${month}-${day}`;  // 鐢熸垚 YYYY-MM-DD 鏍煎紡鐨勬棩鏈�
+  }
 }
\ No newline at end of file
diff --git a/entry/src/main/ets/view/ItemDetail.ets b/entry/src/main/ets/view/ItemDetail.ets
index 3307c61..b5f17cd 100644
--- a/entry/src/main/ets/view/ItemDetail.ets
+++ b/entry/src/main/ets/view/ItemDetail.ets
@@ -136,6 +136,8 @@ export default struct ItemDetail {
             .width(CommonConstants.detail_width)
             .textAlign(TextAlign.Start)
             .margin({ left: 10 })
+            .fontColor((this.content.ddl.substring(0,4)=='2025' && this.content.ddl.substring(5,7)=='01'
+            && Number.parseInt(this.content.ddl.substring(8))-18>=0)||this.content.isDone?'black':'red')
         }
         .margin({ top: CommonConstants.list_margin_top })
         .width(CommonConstants.list_width)
@@ -143,51 +145,19 @@ export default struct ItemDetail {
         .backgroundColor($r('app.color.white'))
         .borderRadius(CommonConstants.border_radius)
         .justifyContent(FlexAlign.Start)
-
       }
-
-      // 濡傛灉宸插畬鎴愶紝鏄剧ず瀹屾垚鏃堕棿
-      // if (this.content.isDone) {
-      //   Column() {
-      //     Row() {
-      //       Text('瀹屾垚鏃堕棿')
-      //         .fontSize(CommonConstants.detail_title_font_size)
-      //         .fontWeight(FontWeight.Bold)
-      //         .width(CommonConstants.detail_width)
-      //         .margin({ left: CommonConstants.detail_margin_Left })
-      //
-      //       Text(this.content.completeTime)
-      //         .fontSize(CommonConstants.detail_font_size)
-      //         .width(CommonConstants.detail_width)
-      //         .textAlign(TextAlign.Start)
-      //         .margin({ left: 10 })
-      //     }
-      //   }
-      //   .margin({ top: CommonConstants.list_margin_top })
-      //   .width(CommonConstants.list_width)
-      //   .height(CommonConstants.time_height)
-      //   .backgroundColor($r('app.color.white'))
-      //   .borderRadius(CommonConstants.border_radius)
-      //   .justifyContent(FlexAlign.Start)
-      //   .alignItems(HorizontalAlign.Start)
-      // }
     }
-
   }
 
-  private async deleteItem() {
+  async deleteItem() {
     // 鍒犻櫎浜嬩欢
-        const res = await webAPI.DeleteSingleItem(this.content.id)
-        if(res == 'success') {
-          promptAction.showToast({ message: '鍒犻櫎鎴愬姛' })
-          setTimeout(() => {
-
-          }, 500)
-        }
-        else {
-          promptAction.showToast({ message: '鍒犻櫎澶辫触' })
-        }
-
-    this.LocalPageStack.pop({},true)
+    const res = await webAPI.DeleteSingleItem(this.content.id)
+    if(res == 'success') {
+      promptAction.showToast({ message: '鍒犻櫎鎴愬姛' })
+      this.LocalPageStack.pop({},true)
+    }
+    else {
+      promptAction.showToast({ message: '鍒犻櫎澶辫触' })
+    }
   }
 }
\ No newline at end of file
diff --git a/entry/src/main/ets/view/TodoItem.ets b/entry/src/main/ets/view/TodoItem.ets
index 06b634e..3c3a74b 100644
--- a/entry/src/main/ets/view/TodoItem.ets
+++ b/entry/src/main/ets/view/TodoItem.ets
@@ -2,19 +2,8 @@
 import CommonConstants from '../public/CommonConstant'
 import TodoItemModel from '../viewmodel/TodoItemModel'
 import { promptAction } from '@kit.ArkUI'
-import { http } from '@kit.NetworkKit'
 import webAPI from '../public/WebAPI';
 
-interface HttpError {
-  message: string;
-  code: number;
-}
-
-interface SysTimeResponse {
-  sysTime1: string;
-  sysTime2: string;
-}
-
 @Component
 export default struct ToDoItem {
   @Link totalTasks: Array<TodoItemModel> //todolist
@@ -31,9 +20,7 @@ export default struct ToDoItem {
       .height(CommonConstants.checkbox_width)
       .margin(CommonConstants.checkbox_margin)
       .onClick(() => {
-        // this.myTask.isCompleted = !this.myTask.isCompleted
-        //this.markComplete()
-        this.DoneItem(this.myTask.id)
+        this.DoneItem()
       })
   }
 
@@ -58,78 +45,17 @@ export default struct ToDoItem {
     .borderRadius(CommonConstants.border_radius)
   }
 
-  async markComplete() { //Todo锛氭爣璁板畬鎴愰渶瑕佷慨鏀癸紝鐪嬩笉鎳備笅闈唬鐮佺殑璇濆彲浠ョ洿鎺ュ啓涓€涓柊鐨勯€昏緫...
-    this.myTask.isDone = !this.myTask.isDone
-    // this.myTask.itemName = 'aaa' //鍦ㄨ繖閲岀殑鏀瑰彉鍙互琚療瑙�
-    if(this.myTask.isDone) { //宸插畬鎴�
-      //Todo锛氳姹傚畬鎴愭椂闂达紝濂藉儚娌℃湁鎰忎箟锛屽彲浠ョ洿鎺ュ垹鎺�
-      let httpRequest = http.createHttp();
-      let finishTime = ''
-      let count = 0
-      while(finishTime == '' && count<10) { //寰幆鐩村埌璇锋眰鍒版椂闂�
-        count++
-        await httpRequest.request('http://quan.suning.com/getSysTime.do', { // 鑻忓畞鏃堕棿鎴矨PI
-          method: http.RequestMethod.GET,
-        }).then((data) => {
-          if (data.responseCode === 200) {
-            let response = data.result + "";
-            console.info('Response: ' + response);
-
-            const json_res = JSON.parse(data.result + "") as SysTimeResponse
-            finishTime = json_res.sysTime2.substring(0,10)
-          } else {
-            console.error('Request failed with response code: ' + data.responseCode);
-          }
-        }).catch((err:HttpError) => {
-          console.error('Request error: ' + JSON.stringify(err));
-        })
-      }
-      httpRequest.destroy(); // 閿€姣丠TTP璇锋眰瀵硅薄
-      //璇锋眰缁撴灉
-      if(finishTime == '') { //璇锋眰澶辫触锛屼笉鎿嶄綔
-        this.myTask.isDone = !this.myTask.isDone
-        promptAction.showToast({
-          message: '鏃堕棿璇锋眰澶辫触锛岃閲嶈瘯锛�'
-        })
-        return
-      }
-      else {
-      }
-    }
-    else { //鏇存柊鍚庣姸鎬侊細鏈畬鎴�
-    }
-    //鏇挎崲鍘熸潵鐨勫璞�
-    this.myTask = JSON.parse(JSON.stringify(this.myTask))
-    this.totalTasks.splice(this.myIndex, 1, this.myTask)
-    promptAction.showToast({
-      message: '鏇存柊鎴愬姛锛�'
-    })
-    //鍙洿鏂板璞$殑灞炴€�
-    // let result = DataModel.updateData(this.myTask)
-    // if(result==-1) {
-    //   promptAction.showToast({
-    //     message: '鏇存柊澶辫触锛岃鍒锋柊椤甸潰鍚庨噸鏂板皾璇曪紒'
-    //   })
-    // }
-    // else {
-    //   promptAction.showToast({
-    //     message: '鏇存柊鎴愬姛锛�'
-    //   })
-    // }
-  }
-  private async DoneItem(id:number) {
+  async DoneItem() {
     // 瀹屾垚浜嬩欢
-    const res = await webAPI.DoneSingleItem(id)
+    const res = await webAPI.DoneSingleItem(this.myTask.id, !this.myTask.isDone)
     if(res == 'success') {
-      promptAction.showToast({ message: '宸插畬鎴愭浜嬩欢' })
-      setTimeout(() => {
-
-      }, 500)
+      promptAction.showToast({ message: '鏇存柊鎴愬姛锛�' })
+      this.myTask.isDone = !this.myTask.isDone
+      this.myTask = JSON.parse(JSON.stringify(this.myTask))
+      this.totalTasks.splice(this.myIndex, 1, this.myTask)
     }
     else {
-      promptAction.showToast({ message: '瀹屾垚澶辫触' })
+      promptAction.showToast({ message: '鏇存柊澶辫触..' })
     }
-
-    this.LocalPageStack.pop({},true)
   }
 }
\ No newline at end of file
-- 
GitLab