【HarmonyOS第一课:运动健康实战】我的运动生活

应用简介

本文章是参加“【创意Demo挑战赛】HarmonyOS运动健康实战——等你来战!”活动写的一个应用介绍,活动地址:

https://developer.huawei.com/consumer/cn/forum/topic/0202116956892085528?fid=0101562279236410779

本文档通过学习健康生活案例,熟悉了常用的控件用法和一些开发技巧,并制作了一个简单的运动健康类程序,主要分为任务、运动和我的三个页面。任务页面包含一些打卡任务,完成进度会以环形进度条展示。运动页面包含六项运动内容,主要是呼吸、步行、跑步、健身、瑜伽和跳绳,不同的运动项目会有相应的操作。

效果预览

简要介绍

在欢迎页面之前,先通过首选项判断用户是否接受了《用户隐私条款》,隐私条款是一个用户自定义弹窗。

欢迎界面之后,就进入了主页面,底部分为三个TAB,任务、运动和我的。

任务页面:

运动页面:

训练包含吸气和呼气练习,

我的页面:

代码分析

应用程序欢迎页面通过首选项的简单存储,记录用户上次操作。

let preferences = data_preferences.getPreferences(this.context, SPORT_PREF);
preferences.then((res) => {
  res.put(IS_ACCEPT, false);
  res.get(IS_ACCEPT, false).then((isAccept) => {
    if (isAccept === true) {
      this.waitToMainPage();
    } else {
      this.dialogController.open();
    }
  });
});
复制

运动项目中的呼吸训练,开始按钮触发了一个定时器。

startBreathing() {
  var that = this
  this.intervalID = setInterval(function() {
    that.currentCount = that.currentCount + 1
    if(that.currentCount == (that.select + 1) * 60) {
      clearInterval(that.intervalID);

      that.breathing = false
      that.loopnum = 0
      that.currentCount = 0
      that.countdown = 0

      AlertDialog.show(
        {
          title: '',
          message: '\n恭喜训练完成\n',
          confirm: {
            value: '关闭',
            action: () => {
              console.info('Button-clicking callback')
            }
          },
          cancel: () => {
            console.info('Closed callbacks')
          }
        }
      )
    }
    if((that.currentCount - 1) % 6 == 0) {
      that.loopnum += 1
    }


    if(that.loopnum % 2 == 1) {
      that.countdown = that.countdown + 1
      that.prepare_tips = "第 " + Math.floor(that.loopnum / 2 + 1).toString() + " 次"+ "吸气..."
    } else {
      that.countdown = that.countdown - 1
      that.prepare_tips = "第 " + (that.loopnum / 2).toString() + " 次"+ "呼气..."
    }      
  }, 1000);
}
复制

训练完成后,会记录此次训练时间。

putLatestRecord() {
  let preferences = data_preferences.getPreferences(this.context, commonConst.RECORD_PREF);

  var that = this
  preferences.then((res) => {
    // res.put(IS_ACCEPT, false);
    let date = new Date();
    let currentTime = date.getFullYear() + "/" + (date.getMonth() + 1).toString().padStart(2, '0') + "/" + date.getDate().toString().padStart(2, '0') + " " + date.getHours() + ":" + date.getMinutes().toString().padStart(2, '0') + ":" + date.getSeconds().toString().padStart(2, '0')
    console.log("currentTime", currentTime)
    res.put("breath", currentTime);

    that.lastRecord = "上次运动: " + currentTime
  });
}
复制

项目总结

通过了这次简单的练习,学会了不少常用的知识。AppStorage是应用程序中的单例对象,可以为应用程序范围内的可变状态属性提供中央存储。Flex是一个功能强大的容器组件,支持横向布局,竖向布局,子组件均分和流式换行布局。首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。

 

 

{{o.name}}
{{m.name}}

猜你喜欢

转载自my.oschina.net/u/4478396/blog/9008370