213 lines
7.3 KiB
Swift
213 lines
7.3 KiB
Swift
//
|
|
// SearchLocationResultVC.swift
|
|
// QuickLocation
|
|
//
|
|
|
|
import UIKit
|
|
import RxSwift
|
|
import RxCocoa
|
|
import ObjectMapper
|
|
import URLNavigator
|
|
|
|
class SearchLocationResultVC: BaseViewController {
|
|
|
|
fileprivate var rootView: SearchLocationResultView!
|
|
|
|
override func loadView() {
|
|
rootView = SearchLocationResultView(frame: UIScreen.main.bounds)
|
|
view = rootView
|
|
}
|
|
|
|
private let phone: String
|
|
private let code: Int
|
|
private let memberData: [String: Any]
|
|
private var groupModel: GroupModel?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
reactiveAction()
|
|
applyResultPresentation()
|
|
requestPhoneArea()
|
|
if code == 0 || code == 20005 {
|
|
requestGroupInfo()
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
applyResultPresentation()
|
|
}
|
|
|
|
private func applyResultPresentation() {
|
|
handleCode()
|
|
rootView.updateSubViews()
|
|
}
|
|
|
|
private func reactiveAction() {
|
|
rootView.actionBtn.rx.tap.subscribe(onNext: { _ in
|
|
switch self.code {
|
|
case 0:
|
|
var userInfo = self.memberData
|
|
let selectedKey = self.rootView.currentGroupKey
|
|
if !selectedKey.isEmpty {
|
|
userInfo["group_key"] = selectedKey
|
|
}
|
|
NotificationCenter.default.post(name: .ShowMemberLocationNotification,
|
|
object: nil,
|
|
userInfo: userInfo)
|
|
self.navigationController?.popToRootViewController(animated: true)
|
|
if let tab = self.tabBarController as? MainTabBarController
|
|
?? UIViewController.topMost?.tabBarController as? MainTabBarController {
|
|
tab.selectTab(at: 0)
|
|
}
|
|
case 20004:
|
|
SharePopView.show()
|
|
case 20005:
|
|
self.showGroupPicker()
|
|
default:
|
|
break
|
|
}
|
|
}).disposed(by: disposeBag)
|
|
}
|
|
|
|
private func showGroupPicker() {
|
|
guard let model = groupModel else {
|
|
requestGroupInfo(showPicker: true)
|
|
return
|
|
}
|
|
presentGroupPicker(with: model)
|
|
}
|
|
|
|
private func presentGroupPicker(with model: GroupModel) {
|
|
GroupListPopView.show(groupModel: model) { groupKey in
|
|
guard let key = groupKey,
|
|
let groupInfo = model.groups.first(where: { $0.group_key == key })?.toJSON() else { return }
|
|
AppRouter.push(Route.inviteJoin, userInfo: ["groupInfo": groupInfo])
|
|
}
|
|
}
|
|
|
|
private func handleCode() {
|
|
rootView.actionBtn.isHidden = true
|
|
|
|
switch code {
|
|
case 0:
|
|
rootView.configureResult(
|
|
status: "定位成功",
|
|
disclaimer: "选择圈子查看可获得具体位置",
|
|
showWarning: false,
|
|
showGroups: hasSharedGroups
|
|
)
|
|
rootView.actionBtn.setTitle("去查看", for: .normal)
|
|
rootView.actionBtn.isHidden = false
|
|
case 20004:
|
|
rootView.configureResult(
|
|
status: "用户未注册",
|
|
disclaimer: "您查询的用户未注册账号,无法提供具体定位,请邀请他加入您的圈子吧",
|
|
showWarning: true,
|
|
showGroups: false
|
|
)
|
|
rootView.actionBtn.setTitle("分享", for: .normal)
|
|
rootView.actionBtn.isHidden = false
|
|
case 20005:
|
|
rootView.configureResult(
|
|
status: "定位成功",
|
|
disclaimer: "您查询的用户与您不在同一个圈子,无法提供具体定位,请邀请他加入您的圈子吧",
|
|
showWarning: true,
|
|
showGroups: false
|
|
)
|
|
rootView.actionBtn.setTitle("邀请TA加入", for: .normal)
|
|
rootView.actionBtn.isHidden = false
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
private var hasSharedGroups: Bool {
|
|
guard let model = groupModel else { return false }
|
|
return !sharedGroups(from: model.groups).isEmpty
|
|
}
|
|
|
|
/// 只保留与被查找用户的共同圈子。
|
|
private func sharedGroups(from myGroups: [GroupInfoModel]) -> [GroupInfoModel] {
|
|
let keys = sharedGroupKeys()
|
|
guard !keys.isEmpty else { return [] }
|
|
let keySet = Set(keys)
|
|
return myGroups.filter { keySet.contains($0.group_key) }
|
|
}
|
|
|
|
private func sharedGroupKeys() -> [String] {
|
|
var keys: [String] = []
|
|
keys.append(contentsOf: groupKeys(in: memberData["common_groups"]))
|
|
keys.append(contentsOf: groupKeys(in: memberData["groups"]))
|
|
if let list = memberData["group_keys"] as? [Any] {
|
|
keys.append(contentsOf: list.compactMap(stringValue).filter { !$0.isEmpty })
|
|
}
|
|
let single = stringValue(memberData["group_key"])
|
|
if !single.isEmpty { keys.append(single) }
|
|
|
|
var seen = Set<String>()
|
|
return keys.filter { seen.insert($0).inserted }
|
|
}
|
|
|
|
private func groupKeys(in value: Any?) -> [String] {
|
|
guard let list = value as? [Any] else { return [] }
|
|
return list.compactMap { item in
|
|
if let dict = item as? [String: Any] {
|
|
return stringValue(dict["group_key"])
|
|
}
|
|
return stringValue(item)
|
|
}.filter { !$0.isEmpty }
|
|
}
|
|
|
|
private func stringValue(_ value: Any?) -> String {
|
|
switch value {
|
|
case let text as String:
|
|
return text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
case let number as NSNumber:
|
|
return number.stringValue
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
private func requestPhoneArea() {
|
|
SystemService.phoneArea(phone: phone).subscribe(onNext: { response in
|
|
guard let data = response.data else { return }
|
|
let province = data["Province"].safeString
|
|
let city = data["City"].safeString
|
|
self.rootView.phoneAreaLab.text = "\(province)·\(city)"
|
|
self.applyResultPresentation()
|
|
}).disposed(by: disposeBag)
|
|
}
|
|
|
|
private func requestGroupInfo(showPicker: Bool = false) {
|
|
GroupService.groupInfo().subscribe { [weak self] response in
|
|
guard let self = self, let model = response.model else { return }
|
|
self.groupModel = model
|
|
if self.code == 0 {
|
|
let shared = self.sharedGroups(from: model.groups)
|
|
let preferred = self.stringValue(self.memberData["group_key"])
|
|
let selected = shared.contains(where: { $0.group_key == preferred })
|
|
? preferred
|
|
: (shared.first?.group_key ?? "")
|
|
self.rootView.reloadGroups(shared, selectedKey: selected)
|
|
}
|
|
self.handleCode()
|
|
if showPicker {
|
|
self.presentGroupPicker(with: model)
|
|
}
|
|
}.disposed(by: disposeBag)
|
|
}
|
|
|
|
init(phone: String, code: Int, memberData: [String: Any] = [:]) {
|
|
self.phone = phone
|
|
self.code = code
|
|
self.memberData = memberData
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
}
|