|
@@ -0,0 +1,376 @@
|
|
|
+//
|
|
|
+// MarketSearchViewModel.swift
|
|
|
+// Koala
|
|
|
+//
|
|
|
+// Created by 刘千军 on 2023/10/16.
|
|
|
+// Copyright © 2023 刘千军. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+import UIKit
|
|
|
+import ReactiveCocoa
|
|
|
+import ReactiveSwift
|
|
|
+
|
|
|
+class MarketSearchViewModel: BaseViewModel {
|
|
|
+
|
|
|
+ lazy var collectionList:[String] = {
|
|
|
+ let arr:[String] = [String]()
|
|
|
+ return arr
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var cnProductHotList:[ProductHotListModel] = {
|
|
|
+ let arr:[ProductHotListModel] = [ProductHotListModel]()
|
|
|
+ return arr
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var hkProductHotList:[ProductHotListModel] = {
|
|
|
+ let arr:[ProductHotListModel] = [ProductHotListModel]()
|
|
|
+ return arr
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var usProductHotList:[ProductHotListModel] = {
|
|
|
+ let arr:[ProductHotListModel] = [ProductHotListModel]()
|
|
|
+ return arr
|
|
|
+ }()
|
|
|
+
|
|
|
+ lazy var symbolList:[KSymbolModel] = {//交易对
|
|
|
+ let arr:[KSymbolModel] = [KSymbolModel]()
|
|
|
+ return arr
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+ var productHotList:[[ProductHotListModel]] = [[ProductHotListModel]]()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ var isSearch:Bool = false {
|
|
|
+ didSet{
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var screenText:String = ""{
|
|
|
+ didSet{
|
|
|
+ self.netSymbolList(isUp: false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var pageNum = 1
|
|
|
+
|
|
|
+ //netForHomeSymbolList
|
|
|
+ var userListAction: Action <(),(Bool),Never>!
|
|
|
+
|
|
|
+ var productHotListAction: Action <(),(Bool),Never>!
|
|
|
+ var symbolListAction: Action <(Bool),(Bool),Never>!
|
|
|
+ var insertProductUserAction: Action <(String),(Bool),Never>!
|
|
|
+ var deleteProductUserAction: Action <(String),(Bool),Never>!
|
|
|
+
|
|
|
+
|
|
|
+ override func initialize() {
|
|
|
+ userListAction = Action<(),(Bool),Never> {[weak self] _ in
|
|
|
+ return SignalProducer<(Bool),Never>{ observer, _ in
|
|
|
+ self?.requestUserList(observer: observer)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ productHotListAction = Action<(),(Bool),Never> {[weak self] _ in
|
|
|
+ return SignalProducer<(Bool),Never>{ observer, _ in
|
|
|
+ self?.requestProductHotList(observer: observer)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ symbolListAction = Action<(Bool),(Bool),Never> {[weak self] isUp in
|
|
|
+ return SignalProducer<(Bool),Never>{ observer, _ in
|
|
|
+ self?.requestSymbolList(observer: observer,isUp:isUp)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ insertProductUserAction = Action<(String),(Bool),Never> {[weak self] symbol in
|
|
|
+ return SignalProducer<(Bool),Never>{ observer, _ in
|
|
|
+ self?.requestInsertProductUser(observer: observer,symbol:symbol)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ deleteProductUserAction = Action<(String),(Bool),Never> {[weak self] symbol in
|
|
|
+ return SignalProducer<(Bool),Never>{ observer, _ in
|
|
|
+ self?.requestDeleteProductUser(observer: observer,symbol:symbol)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override func webSocketdidReceive(nofi: NSNotification) {
|
|
|
+ // self.webSocketdidReceiveDataTreating(nofi: nofi)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension MarketSearchViewModel {
|
|
|
+ //获取自选产品列表
|
|
|
+ func netUserList() {
|
|
|
+ if OperationalUserInfoData.getLogin(){
|
|
|
+ userListAction.apply().start()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func requestUserList(observer: Signal< Bool, Never>.Observer) -> Void {
|
|
|
+ ProgressHUD.showLoading()
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType: .Post, URLString: netForProductUserList, success: { (code, result) in
|
|
|
+ ProgressHUD.hideHud()
|
|
|
+ if let list:[NSDictionary] = result?["list"] as? [NSDictionary] {
|
|
|
+ self.collectionList.removeAll()
|
|
|
+ for dic in list {
|
|
|
+ self.collectionList.append((dic["symbol"] as? String) ?? "")
|
|
|
+ }
|
|
|
+
|
|
|
+ observer.send(value: true)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }else{
|
|
|
+ observer.send(value: false)
|
|
|
+ observer.sendCompleted()
|
|
|
+ ProgressHUD.showError(status:DataTypeErrorPrompt)
|
|
|
+ }
|
|
|
+
|
|
|
+ }) { (code, result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ observer.send(value: false)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func netProductHotList() {
|
|
|
+ productHotListAction.apply().start()
|
|
|
+ }
|
|
|
+
|
|
|
+ func requestProductHotList(observer: Signal<(Bool),Never>.Observer) -> Void {
|
|
|
+ let queue = DispatchQueue.init(label: "ProductHotList")
|
|
|
+ let group = DispatchGroup()
|
|
|
+
|
|
|
+ queue.async(group: group, execute: {
|
|
|
+ group.enter()
|
|
|
+ let param:[String:Any] = [
|
|
|
+ "market":"CN",
|
|
|
+ "pageNum":"1",
|
|
|
+ "pageSize":"5"
|
|
|
+ ]
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType:.Post, URLString:netForGetProductHotList,parameters:param, success: {[weak self] (errno,result) in
|
|
|
+ if let list:[NSDictionary] = result?["list"] as? [NSDictionary] {
|
|
|
+ self?.cnProductHotList.removeAll()
|
|
|
+ for dict in list{
|
|
|
+ let model = ProductHotListModel.deserialize(from: dict)!
|
|
|
+ var ask = model.productBean.ask
|
|
|
+ if conerData(ask, .MyDouble).2! == 0{
|
|
|
+ ask = model.productBean.open
|
|
|
+ }
|
|
|
+ model.productBean.newPrice = precisionString(with: ask, precision: model.productBean.digits)
|
|
|
+ let np = decimalNumber(A: model.productBean.newPrice, B: model.productBean.open, type: .Subtract, scale:model.productBean.digits)
|
|
|
+ let increase = (conerData(np, .MyDouble).2!/conerData(model.productBean.open, .MyDouble).2!)*100
|
|
|
+ model.productBean.increase = String(format: "%.2f",increase)
|
|
|
+ self?.cnProductHotList.append(model)
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ ProgressHUD.showError(status:DataTypeErrorPrompt)
|
|
|
+ }
|
|
|
+
|
|
|
+ group.leave()
|
|
|
+ }) { (errno,result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ group.leave()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ queue.async(group: group, execute: {
|
|
|
+ group.enter()
|
|
|
+ let param:[String:Any] = [
|
|
|
+ "market":"HK",
|
|
|
+ "pageNum":"1",
|
|
|
+ "pageSize":"5"
|
|
|
+ ]
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType:.Post, URLString:netForGetProductHotList,parameters:param, success: {[weak self] (errno,result) in
|
|
|
+ if let list:[NSDictionary] = result?["list"] as? [NSDictionary] {
|
|
|
+ self?.hkProductHotList.removeAll()
|
|
|
+ for dict in list{
|
|
|
+ let model = ProductHotListModel.deserialize(from: dict)!
|
|
|
+ var ask = model.productBean.ask
|
|
|
+ if conerData(ask, .MyDouble).2! == 0{
|
|
|
+ ask = model.productBean.open
|
|
|
+ }
|
|
|
+ model.productBean.newPrice = precisionString(with: ask, precision: model.productBean.digits)
|
|
|
+ let np = decimalNumber(A: model.productBean.newPrice, B: model.productBean.open, type: .Subtract, scale:model.productBean.digits)
|
|
|
+ let increase = (conerData(np, .MyDouble).2!/conerData(model.productBean.open, .MyDouble).2!)*100
|
|
|
+ model.productBean.increase = String(format: "%.2f",increase)
|
|
|
+ self?.hkProductHotList.append(model)
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ ProgressHUD.showError(status:DataTypeErrorPrompt)
|
|
|
+ }
|
|
|
+
|
|
|
+ group.leave()
|
|
|
+ }) { (errno,result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ group.leave()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ queue.async(group: group, execute: {
|
|
|
+ group.enter()
|
|
|
+ let param:[String:Any] = [
|
|
|
+ "market":"US",
|
|
|
+ "pageNum":"1",
|
|
|
+ "pageSize":"5"
|
|
|
+ ]
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType:.Post, URLString:netForGetProductHotList,parameters:param, success: {[weak self] (errno,result) in
|
|
|
+ if let list:[NSDictionary] = result?["list"] as? [NSDictionary] {
|
|
|
+ self?.usProductHotList.removeAll()
|
|
|
+ for dict in list{
|
|
|
+ let model = ProductHotListModel.deserialize(from: dict)!
|
|
|
+ var ask = model.productBean.ask
|
|
|
+ if conerData(ask, .MyDouble).2! == 0{
|
|
|
+ ask = model.productBean.open
|
|
|
+ }
|
|
|
+ model.productBean.newPrice = precisionString(with: ask, precision: model.productBean.digits)
|
|
|
+ let np = decimalNumber(A: model.productBean.newPrice, B: model.productBean.open, type: .Subtract, scale:model.productBean.digits)
|
|
|
+ let increase = (conerData(np, .MyDouble).2!/conerData(model.productBean.open, .MyDouble).2!)*100
|
|
|
+ model.productBean.increase = String(format: "%.2f",increase)
|
|
|
+ self?.usProductHotList.append(model)
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ ProgressHUD.showError(status:DataTypeErrorPrompt)
|
|
|
+ }
|
|
|
+
|
|
|
+ group.leave()
|
|
|
+ }) { (errno,result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ group.leave()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ group.notify(queue: queue){
|
|
|
+ ProgressHUD.hideHud()
|
|
|
+ self.productHotList = [self.usProductHotList,self.hkProductHotList,self.cnProductHotList]
|
|
|
+ observer.send(value: true)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取交易对
|
|
|
+ func netSymbolList(isUp:Bool) {
|
|
|
+ symbolListAction.apply(isUp).start()
|
|
|
+ }
|
|
|
+ //
|
|
|
+ func requestSymbolList(observer: Signal< Bool, Never>.Observer,isUp:Bool) -> Void {
|
|
|
+ if !isUp{
|
|
|
+ self.pageNum = 1
|
|
|
+ }
|
|
|
+ let param : [String : Any] = [
|
|
|
+
|
|
|
+ "pageNum" : self.pageNum,
|
|
|
+ "pageSize" : "20",
|
|
|
+ "symbol_title" : self.screenText
|
|
|
+ ]
|
|
|
+ ProgressHUD.showLoading()
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType: .Post, URLString: netForHomeSymbolList, parameters: param, success: {[weak self](code, result) in
|
|
|
+
|
|
|
+ ProgressHUD.hideHud()
|
|
|
+ if let list:[NSDictionary] = result?["list"] as? [NSDictionary] {
|
|
|
+ if !isUp {
|
|
|
+ self?.symbolList.removeAll()
|
|
|
+ }
|
|
|
+ self?.pageNum += 1
|
|
|
+ for dic in list {
|
|
|
+ let model = KSymbolModel.deserialize(from: dic)!
|
|
|
+ var ask = model.ask
|
|
|
+ if conerData(ask, .MyDouble).2! == 0{
|
|
|
+ ask = model.open
|
|
|
+ }
|
|
|
+ model.newPrice = precisionString(with: ask, precision: model.digits)
|
|
|
+ let np = decimalNumber(A: model.newPrice, B: model.open, type: .Subtract, scale:model.digits)
|
|
|
+ let increase = (conerData(np, .MyDouble).2!/conerData(model.open, .MyDouble).2!)*100
|
|
|
+ model.increase = String(format: "%.2f",increase)
|
|
|
+
|
|
|
+ for str in self!.collectionList {
|
|
|
+ if str == model.symbol{
|
|
|
+ model.isSelect = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ self?.symbolList.append(model)
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ let isNoData:Bool = (list.count < 20) ? true : false
|
|
|
+ observer.send(value:isNoData)
|
|
|
+
|
|
|
+ }else{
|
|
|
+ observer.send(value: false)
|
|
|
+ ProgressHUD.showError(status:DataTypeErrorPrompt)
|
|
|
+ }
|
|
|
+ observer.sendCompleted()
|
|
|
+ }) { (code, result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ observer.send(value: false)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加收藏
|
|
|
+ func netInsertProductUser(symbol:String) {
|
|
|
+ if OperationalUserInfoData.getLogin(){
|
|
|
+ insertProductUserAction.apply(symbol).start()
|
|
|
+ }else{
|
|
|
+ currentVc()!.navigationController?.pushViewController(LoginViewController(), animated: true)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ func requestInsertProductUser(observer: Signal< Bool, Never>.Observer,symbol:String) -> Void {
|
|
|
+ ProgressHUD.showLoading()
|
|
|
+ let parame:[String:Any] = ["symbol":symbol]
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType: .Post, URLString: netForInsertProductUser ,parameters: parame, success: { (code, result) in
|
|
|
+ ProgressHUD.showSuccess(status: switchLanguage("收藏成功"))
|
|
|
+ self.collectionList.append(symbol)
|
|
|
+ for model in self.symbolList{
|
|
|
+ if model.symbol == symbol{
|
|
|
+ model.isCollection = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ observer.send(value: true)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }) { (code, result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ observer.send(value: false)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //取消收藏
|
|
|
+ func netDeleteProductUser(symbol:String) {
|
|
|
+ if OperationalUserInfoData.getLogin(){
|
|
|
+ deleteProductUserAction.apply(symbol).start()
|
|
|
+ }else{
|
|
|
+ currentVc()!.navigationController?.pushViewController(LoginViewController(), animated: true)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ func requestDeleteProductUser(observer: Signal< Bool, Never>.Observer,symbol:String) -> Void {
|
|
|
+ let parame:[String:Any] = ["symbol":symbol]
|
|
|
+ NetWork.NetWorkInstance.requestData(requestType: .Post, URLString: netForDeleteProductUser,parameters: parame, success: { (code, result) in
|
|
|
+ ProgressHUD.showSuccess(status: switchLanguage("取消成功"))
|
|
|
+ let index = self.collectionList.firstIndex(of: symbol)
|
|
|
+ self.collectionList.remove(at:index!)
|
|
|
+ for model in self.symbolList{
|
|
|
+ if model.symbol == symbol{
|
|
|
+ model.isCollection = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ observer.send(value: true)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }) { (code, result) in
|
|
|
+ ProgressHUD.showError(status: result as! String)
|
|
|
+ observer.send(value: false)
|
|
|
+ observer.sendCompleted()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|