Przeglądaj źródła

feat: 修改主页获取宝宝信息的部分逻辑, 新增搜索静态页面

YuJing 1 miesiąc temu
rodzic
commit
5e93fc0ad9

+ 3 - 0
features/feature/src/main/ets/model/Persistence.ets

@@ -0,0 +1,3 @@
+export class RecipSearch{
+  searchList: string[] = []
+}

+ 13 - 0
features/feature/src/main/ets/pages/BabyInfoPage.ets

@@ -1,11 +1,24 @@
 import { _YtHeader } from '../components/_YtHeader';
 import { IncreaseBabyInfoViewModel } from '../viewModel/IncreaseBabyInfoViewModel';
 import { BasicType } from 'basic';
+import { emitter } from '@kit.BasicServicesKit';
 
 @ComponentV2
 struct BabyInfoPage {
   @Local vm: IncreaseBabyInfoViewModel = new IncreaseBabyInfoViewModel(false);
 
+  aboutToAppear(): void {
+    emitter.on('upLoadEnd', (data) => {
+      console.log(JSON.stringify(data))
+      this.vm.babyInfo.avatarUrl = data!.data!.toString()
+      this.vm.updateBaby()
+    })
+  }
+
+  aboutToDisappear(): void {
+    emitter.off('upLoadEnd')
+  }
+
   build() {
     NavDestination() {
       Column() {

+ 187 - 0
features/feature/src/main/ets/pages/RecipeSearchPage.ets

@@ -0,0 +1,187 @@
+import { yTRouter } from 'basic'
+import { LengthMetrics } from '@kit.ArkUI'
+import { RecipeSearchViewModel } from '../viewModel/RecipeSearchViewModel'
+import { Cuisine } from '../model/Index'
+
+
+@ComponentV2
+struct RecipeSearchPage{
+  @Local vm: RecipeSearchViewModel = new RecipeSearchViewModel()
+
+  aboutToAppear(): void {
+  }
+
+  build(){
+    NavDestination(){
+      Column(){
+        // Title
+        Row(){
+          Image($r("app.media.back"))
+            .width(20)
+            .aspectRatio(1)
+            .onClick(() => {
+              yTRouter.pop()
+            })
+        }
+        .width('100%')
+        .alignItems(VerticalAlign.Top)
+        .justifyContent(FlexAlign.Start)
+        .padding({top: 10, bottom: 10})
+
+        // 搜索框
+        Row({space: 15}){
+          Image($r("app.media.Search"))
+            .width(18)
+            .aspectRatio(1)
+
+          TextInput({ placeholder: "输入日记标题", text: $$this.vm.keyWord })
+            .padding(0)
+            .fontSize(16)
+            .layoutWeight(1)
+            .borderRadius(0)
+            .placeholderColor('#ff777272')
+            .placeholderFont({ size: 16 })
+            .backgroundColor(Color.Transparent)
+            .caretColor('app.color.main_ac_color_dark')
+            .onSubmit(()=>{ this.vm.onSearch() })
+
+
+          if(this.vm.keyWord.length > 0) {
+            Image($r('app.media.Diary_close'))
+              .width(25)
+              // .padding(5)
+              .aspectRatio(1)
+              .borderRadius(15)
+              .onClick(() => { this.vm.clearKeyWord() })
+          }
+
+          Text("确认")
+            .borderRadius(32)
+            .linearGradient(this.vm.linearInfo)
+            .padding({ left: 17, top: 5.5, right: 17, bottom: 5.5 })
+            .onClick(() => { this.vm.onSearch() })
+
+        }
+        .width("100%")
+        .alignItems(VerticalAlign.Center)
+        .border({ width: { bottom: 1 } })
+        .padding({ bottom: 21, top: 13 })
+
+        // 历史记录
+        Column({space: 15}){
+          if(this.vm.searchList.length !== 0) {
+            Column(){
+              List(){
+                ForEach(this.vm.searchList, (item: Cuisine, i: number) => {
+                  ListItem() {
+                    // DiaryTitleItem({
+                    //   title: item.title,
+                    //   onClickBack: () => {
+                    //     this.routerDiaryPage(item.id)
+                    //   }
+                    // })
+                  }
+                }, (item: Cuisine) => item.id?.toString())
+              }
+              .width("100%")
+              .height("100%")
+              .scrollBar(BarState.Off)
+            }
+            .width("100%")
+            .layoutWeight(1)
+            .padding({bottom: 20})
+
+          } else if(this.vm.historicalSearchRecords.searchList.length > 0){
+            this.searchTitleBar()
+
+            Flex({wrap: FlexWrap.Wrap, space: { main: new LengthMetrics(8), cross: new LengthMetrics(8)}}){
+              ForEach(this.vm.historicalSearchRecords.searchList, (item: string, index: number) => {
+                this.keyWordItem(item, index)
+              })
+            }
+          } else {
+            // 搜索历史
+            this.searchTitleBar()
+
+            Column({ space: 5 }){
+              Image($r('app.media.Search'))
+                .width(80)
+                .aspectRatio(1)
+
+              Text("暂无搜索历史")
+            }
+            .width("100%")
+            .padding({ top: 100 })
+            .alignItems(HorizontalAlign.Center)
+          }
+        }
+        .width("100%")
+        .layoutWeight(1)
+        .padding({ top: 35 })
+      }
+      .padding({ top: this.vm.safeTop, left: 24, right: 24 })
+    }
+  }
+
+  // 搜索记录关键字
+  @Builder
+  keyWordItem(keyWord: string, index: number){
+    Row({space: 8}){
+      Text(keyWord)
+        .fontSize(16)
+
+      if(this.vm.delModel){
+        Image($r('app.media.close'))
+          .width(16)
+          .aspectRatio(1)
+          .padding(2)
+      }
+    }
+    .borderRadius(8)
+    .backgroundColor("#F7F7F7")
+    .padding({top: 8, bottom: 8, left: 25, right: this.vm.delModel ? 5 : 25})
+    .onClick(() => {
+      if(this.vm.delModel) {
+        this.vm.onDelSearch(index)
+        return;
+      }
+      this.vm.keyWord = keyWord
+      this.vm.onSearch()
+    })
+  }
+
+  // 搜索记录标题栏
+  @Builder
+  searchTitleBar(){
+    // 搜索历史
+    Row(){
+      Text("搜索历史")
+        .fontSize(16)
+        .fontColor(Color.Black)
+
+      Row(){
+        if(this.vm.delModel) {
+          Text("取消")
+            .fontSize(14)
+            .fontColor('#ff625a5a')
+        } else {
+          Image($r('app.media.del'))
+            .width(20)
+            .height(30)
+        }
+      }
+      .onClick(() => {
+        this.vm.delModel = !this.vm.delModel
+      })
+    }
+    .height(30)
+    .width("100%")
+    .justifyContent(FlexAlign.SpaceBetween)
+
+  }
+}
+
+@Builder
+function RecipeSearchBuilder() {
+  RecipeSearchPage()
+}

+ 6 - 7
features/feature/src/main/ets/viewModel/IncreaseBabyInfoViewModel.ets

@@ -5,6 +5,9 @@ import { BabyFoodApi } from '../Apis/BabyFoodApi';
 import { AppStorageV2 } from '@kit.ArkUI';
 import { emitter } from '@kit.BasicServicesKit';
 
+/**
+ * @description 添加宝宝信息和修改宝宝信息共用的 vm
+ */
 @ObservedV2
 export class IncreaseBabyInfoViewModel{
   @Trace babyInfo: BabyInfo = new BabyInfo()
@@ -45,15 +48,10 @@ export class IncreaseBabyInfoViewModel{
     this.isAdd = isAdd
     this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
 
+    // 并非 添加宝宝信息, 从 AppStore 中获取宝宝信息。
     if(!isAdd) {
       this.babyInfo = AppStorageV2.connect<BabyInfo>(BabyInfo, () => new BabyInfo())!
       this.babyInfo.birthday = YTDateUtil.formatDate(new Date(this.babyInfo.birthday!), DateFormat.UNDERLINE)
-
-      emitter.on('upLoadEnd', (data) => {
-        console.log(JSON.stringify(data))
-        this.babyInfo.avatarUrl = data!.data!.toString()
-        this.updateBaby()
-      })
     }
   }
 
@@ -89,7 +87,7 @@ export class IncreaseBabyInfoViewModel{
 
   // 跳过填写
   notFill(){
-    // this._onBackPressed()
+    this._onBackPressed()
   }
 
   // 选择生日
@@ -216,6 +214,7 @@ export class IncreaseBabyInfoViewModel{
       if(ans && ans == 'true'){
         try {
           await BabyFoodApi.deleteBabyInfo(this.babyInfo.id!)
+          this._onBackPressed('success')
         } catch (error){
 
         }

+ 12 - 2
features/feature/src/main/ets/viewModel/MainViewModel.ets

@@ -138,7 +138,17 @@ export class MainViewModel{
   // 获取宝宝信息列表
   async getBabyList(){
     this.babyList = await BabyFoodApi.getBabyList()
-    this.updateBabyInfo(0)
+    let index = this.babyList.findIndex(item => item.id === this.babyInfo.id)
+
+    if(index != -1) {
+      this.updateBabyInfo(index)
+    } else {
+      if(this.babyList.length > 0) {
+        this.updateBabyInfo(0)
+      } else {
+        this.clearDataSource()
+      }
+    }
   }
 
   // 修改购买状态 - 清单 id
@@ -170,7 +180,7 @@ export class MainViewModel{
   // 进入编辑宝宝信息页面
   editBabyInfo() {
     routerUtils.router2BabyInfoPage((res) => {
-
+      this.getBabyList()
     })
   }
 

+ 53 - 0
features/feature/src/main/ets/viewModel/RecipeSearchViewModel.ets

@@ -0,0 +1,53 @@
+import { IBestToast, userInfo, UserInfo, YTAvoid } from "basic"
+import { AppStorageV2, PersistenceV2 } from "@kit.ArkUI"
+import { RecipSearch } from "../model/Persistence"
+import { Cuisine } from "../model/Index"
+
+@ObservedV2
+export class RecipeSearchViewModel{
+  @Trace safeTop: number = 0
+  @Trace delModel: boolean = false
+  @Trace historicalSearchRecords: RecipSearch = new RecipSearch()
+  @Trace keyWord: string = ''
+  @Trace searchList: Cuisine[] = []
+  // 用户信息
+  @Trace userInfo: UserInfo = AppStorageV2.connect<UserInfo>(UserInfo, 'UserInfo', () => userInfo)!
+
+  linearInfo: LinearGradientOptions = {
+    colors: [ ['#B9FD2A', 0.01], ['#F5FD6D', 1] ],
+    angle: 150
+  }
+
+  constructor() {
+    this.safeTop = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number;
+    this.historicalSearchRecords =  PersistenceV2.connect(RecipSearch, `${this.userInfo.getId()}Tags`, () => new RecipSearch())!
+  }
+
+  async onSearch(){
+    if(!this.keyWord) {
+      IBestToast.show('请输入搜索内容')
+      return
+    }
+
+    const targetIndex: number = this.historicalSearchRecords.searchList.indexOf(this.keyWord)
+    if(targetIndex !== -1){
+      this.historicalSearchRecords.searchList.splice(targetIndex, 1)
+    }
+    this.historicalSearchRecords.searchList.unshift(this.keyWord)
+
+    // this.searchList = await DiaryViewModel.diaryLogSearch(this.keyWord)
+    if(this.searchList.length === 0) {
+      IBestToast.show('没有搜索结果')
+    }
+  }
+
+  onDelSearch(index: number){
+    this.historicalSearchRecords.searchList.splice(index, 1)
+  }
+
+  // 清空搜索框
+  clearKeyWord(){
+    this.keyWord = ''
+    this.searchList = []
+  }
+}

+ 4 - 0
features/feature/src/main/resources/base/media/Diary_close.svg

@@ -0,0 +1,4 @@
+<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="design-iconfont">
+  <path fill="#EBEBEB" d="M12 2A10 10 0 1 0 12 22A10 10 0 1 0 12 2Z"/>
+  <path d="M14.8 9L12 11.8966M12 11.8966L15 15M12 11.8966L9.2 9M12 11.8966L9 15" stroke="#BDBDBD" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
+</svg>

+ 4 - 0
features/feature/src/main/resources/base/media/Search.svg

@@ -0,0 +1,4 @@
+<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" class="design-iconfont">
+  <rect x="14.3585" y="12.5641" width="57.2833" height="57.2833" rx="28.6417" stroke="#898989" stroke-width="10.8"/>
+  <path d="M63.2891 64.5L73.7541 75.2777" stroke="#898989" stroke-width="10.8" stroke-linecap="round" stroke-linejoin="round"/>
+</svg>

+ 1 - 0
features/feature/src/main/resources/base/media/back.svg

@@ -0,0 +1 @@
+<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1755071625373" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="6319" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M395.21518 513.604544l323.135538-312.373427c19.052938-18.416442 19.052938-48.273447 0-66.660212-19.053961-18.416442-49.910737-18.416442-68.964698 0L291.75176 480.290811c-19.052938 18.416442-19.052938 48.273447 0 66.660212l357.633237 345.688183c9.525957 9.207709 22.01234 13.796214 34.497699 13.796214 12.485359 0 24.971741-4.588505 34.466999-13.82896 19.052938-18.416442 19.052938-48.242747 0-66.660212L395.21518 513.604544z" fill="#272636" p-id="6320"></path></svg>

BIN
features/feature/src/main/resources/base/media/close.png


+ 10 - 0
features/feature/src/main/resources/base/media/del.svg

@@ -0,0 +1,10 @@
+<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="design-iconfont">
+  <g clip-path="url(#necc0pqe1__clip0_4980_81502)">
+    <path d="M6.59961 8.4H7.79961M7.79961 8.4H17.3996M7.79961 8.4V16.8C7.79961 17.1183 7.92604 17.4235 8.15108 17.6485C8.37612 17.8736 8.68135 18 8.99961 18H14.9996C15.3179 18 15.6231 17.8736 15.8481 17.6485C16.0732 17.4235 16.1996 17.1183 16.1996 16.8V8.4M9.59961 8.4V7.2C9.59961 6.88174 9.72604 6.57652 9.95108 6.35147C10.1761 6.12643 10.4813 6 10.7996 6H13.1996C13.5179 6 13.8231 6.12643 14.0481 6.35147C14.2732 6.57652 14.3996 6.88174 14.3996 7.2V8.4M10.7996 11.4V15M13.1996 11.4V15" stroke="#000" stroke-opacity=".5" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
+  </g>
+  <defs>
+    <clipPath id="necc0pqe1__clip0_4980_81502">
+      <path fill="#fff" d="M0 0H24V24H0z"/>
+    </clipPath>
+  </defs>
+</svg>

+ 4 - 0
features/feature/src/main/resources/base/profile/router_map.json

@@ -16,6 +16,10 @@
       "name": "BabyInfoPage",
       "pageSourceFile": "src/main/ets/pages/BabyInfoPage.ets",
       "buildFunction": "BabyInfoPageBuilder"
+    }, {
+      "name": "RecipeSearchPage",
+      "pageSourceFile": "src/main/ets/pages/RecipeSearchPage.ets",
+      "buildFunction": "RecipeSearchBuilder"
     }
   ]
 }