乐趣区

关于swift:智汀家庭云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】场景管制

场景的管制,包含手动场景的执行、主动场景的开启 / 敞开。

  • 执行、开启 / 敞开场景: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)
    }
}
退出移动版