智汀家庭云-iOS端:业务功能【场景篇】

【1】添加场景

创建场景:EditSceneViewController.swift

private func createScene() {
    guard let name = inputHeader.textField.text else { return }
    
    if name == "" {
        showToast(string: "场景名称不能为空".localizedString)
        return
    }

    if scene.scene_conditions.count == 0 {
        showToast(string: "请先添加条件".localizedString)
        return
    }

    if scene.scene_tasks.count == 0 {
        showToast(string: "请先添加执行任务".localizedString)
        return
    }
    

    scene.name = name

    /// 执行条件
    if scene.scene_conditions.first?.condition_type == 0 { // 手动

        scene.auto_run = false
    } else { // 自动
        scene.auto_run = true

        if self.conditionHeader.conditionRelationshipType == .all { // 满足所有条件

            scene.condition_logic = 1
        } else { // 满足任一条件

            scene.condition_logic = 2
        }
        
        if scene.time_period == nil {
            scene.time_period = 1
            let format = DateFormatter()
            format.dateStyle = .medium
            format.timeStyle = .medium
            format.dateFormat = "yyyy:MM:dd HH:mm:ss"
            if let startTime = format.date(from: "2000:01:01 00:00:00")?.timeIntervalSince1970 {
                scene.effect_start_time = Int(startTime)
            }
            
            if let endTime = format.date(from: "2000:01:02 00:00:00")?.timeIntervalSince1970 {
                scene.effect_end_time = Int(endTime)
            }
            scene.repeat_type = 1
            scene.repeat_date = "1234567"
        }
        
    }
    
    /// 请求接口
    saveButton.selectedChangeView(isLoading: true)
    ApiServiceManager.shared.createScene(scene: scene.transferedEditModel) { [weak self] response in
        self?.showToast(string: "创建成功".localizedString)
        self?.navigationController?.popViewController(animated: true)
        
    } failureCallback: { [weak self] (code, err) in
        self?.showToast(string: err)
        self?.saveButton.selectedChangeView(isLoading: false)
    }

}

【2】场景控制

场景的控制,包括手动场景的执行、自动场景的开启/关闭。

智汀家庭云-iOS端:业务功能【场景篇】_第1张图片

  • 执行、开启/关闭场景:SceneCell.swift

    private func updateAction(_ isOn:Bool,_ isAuto: Bool) {
         
      ApiServiceManager.shared.sceneExecute(scene_id: self.currentSceneModel!.id, is_execute: isOn) {[weak self] respond in
          guard let self = self else {
              return
          }
          if respond.success {
              if isAuto {
                  self.executiveBtn.selectedChangeView(isLoading: false)
                  self.executiveCallback!("自动执行\(isOn ? "开启":"关闭")成功")
              }else{
                  self.executiveBtn.selectedChangeView(isLoading: false)
                  self.executiveCallback!("手动执行成功")
              }
          }
      } failureCallback: { code, err in
          if isAuto {
              //恢复状态
              self.switchIsOn = !self.switchIsOn
              self.autoSwitch.switchIsOn = !self.autoSwitch.switchIsOn
              //按钮样式恢复
              self.executiveBtn.selectedChangeView(isLoading: false)
              self.executiveCallback!("自动执行\(isOn ? "开启":"关闭")失败")
          }else{
              self.executiveBtn.selectedChangeView(isLoading: false)
              self.executiveCallback!("手动执行失败")
          }
      }
    }
    
  • 闭包结果回调:SceneViewController.swift
    在delegate方法cellForRowAt内cell的闭包结果回调

    cell.executiveCallback = {[weak self] result in
              guard let self = self else {
                  return
              }
              //提示执行结果
              self.showToast(string: result)
              //重新刷新列表,更新执行后状态
              self.requestNetwork()
          }

    【3】编辑场景

    枚举定义:SceneType

编辑场景:EditSceneViewController.swift 编辑场景与创建场景共处一个控制器,通过枚举值切换UI及执行操作 编辑场景与创建场景共处一个控制器,通过枚举值切换UI及执行操作

 enum SceneType {
    case edit
    case create
}

编辑场景与创建场景共处一个控制器,通过枚举值切换UI及执行操作

 private func editScene() {
    guard let id = scene_id else { return }
    
    guard let name = inputHeader.textField.text else { return }
    
    if name == "" {
        showToast(string: "场景名称不能为空".localizedString)
        return
    }

    if scene.scene_conditions.count == 0 {
        showToast(string: "请先添加条件".localizedString)
        return
    }

    if scene.scene_tasks.count == 0 {
        showToast(string: "请先添加执行任务".localizedString)
        return
    }
    
    scene.name = name
    
    saveButton.selectedChangeView(isLoading: true)
    ApiServiceManager.shared.editScene(id: id, scene: scene.transferedEditModel) { [weak self] _ in
        self?.showToast(string: "修改成功".localizedString)
        self?.navigationController?.popViewController(animated: true)
    } failureCallback: { [weak self] (code, err) in
        self?.showToast(string: err)
        self?.saveButton.selectedChangeView(isLoading: false)
    }
}

4.删除场景

删除场景:EditSceneViewController.swift

  private func deleteScene() {
    guard let id = scene_id else { return }
    tipsAlert?.isSureBtnLoading = true
    ApiServiceManager.shared.deleteScene(id: id) { [weak self] response in
        self?.tipsAlert?.removeFromSuperview()
        self?.showToast(string: "删除成功".localizedString)
        self?.navigationController?.popViewController(animated: true)

    } failureCallback: { [weak self] (code, err) in
        self?.tipsAlert?.isSureBtnLoading = false
        self?.showToast(string: err)
    }
}

你可能感兴趣的:(swiftios)