Skip to content

Commit

Permalink
merge integration
Browse files Browse the repository at this point in the history
  • Loading branch information
uareagay committed Feb 4, 2019
2 parents f997bb2 + 928c020 commit cecf8b3
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 88 deletions.
33 changes: 0 additions & 33 deletions WePeiYang/Binding/Controller/TJUBindingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,39 +126,6 @@ class TJUBindingViewController: UIViewController {
})
}

func cancelLogin() {

// unbind tju account
var loginInfo: [String: String] = [String: String]()
loginInfo["tjuuname"] = usernameTextField.text
loginInfo["tjupasswd"] = passwordTextField.text

SolaSessionManager.solaSession(type: .get, url: "/auth/unbind/tju", token: TwTUser.shared.token, parameters: loginInfo, success: { dictionary in

guard let errorCode: Int = dictionary["error_code"] as? Int else {
SwiftMessages.showErrorMessage(body: "解绑错误")
return
}

if errorCode == -1 {
TwTUser.shared.tjuBindingState = false
TwTUser.shared.save()
self.dismiss(animated: true, completion: {
self.completion?(true)
})
} else {
let message = dictionary["message"] as? String
SwiftMessages.showErrorMessage(body: message ?? "解析错误")
}
}, failure: { error in
SwiftMessages.showErrorMessage(body: error.localizedDescription)
self.dismiss(animated: true, completion: {
self.completion?(false)
})

})
}

@objc func dismissBinding() {
TWTKeychain.erase(.tju)
self.dismiss(animated: true, completion: {
Expand Down
32 changes: 30 additions & 2 deletions WePeiYang/Classtable/Controller/ClassTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,36 @@ class ClassTableViewController: UIViewController {
}
originTable = table
group.leave()
}, failure: { errorMessage in
SwiftMessages.showErrorMessage(body: errorMessage)
}, failure: { error in
let msg = error.localizedDescription
guard let error = error as? WPYCustomError else {
SwiftMessages.showErrorMessage(body: msg)
return
}

switch error {
case .custom(let msg):
SwiftMessages.showErrorMessage(body: msg)
case .errorCode(let code, _):
if code == 40010 {
SwiftMessages.showErrorMessage(body: "办公网密码错误,请重新绑定办公网")
AccountManager.unbind(url: BindingAPIs.unbindTJUAccount, success: {
TWTKeychain.erase(.tju)
TwTUser.shared.tjuBindingState = false

let bindVC = TJUBindingViewController()
bindVC.hidesBottomBarWhenPushed = true
bindVC.completion = { success in
if success {
self.load()
}
}
UIViewController.top?.present(bindVC, animated: true, completion: nil)
}, failure: { err in
SwiftMessages.showErrorMessage(body: err.localizedDescription)
})
}
}
group.leave()
})

Expand Down
12 changes: 8 additions & 4 deletions WePeiYang/Classtable/Model/ClasstableDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import ObjectMapper

struct ClasstableDataManager {

static func getClassTable(success: @escaping (ClassTableModel) -> Void, failure: @escaping (String) -> Void) {
static func getClassTable(success: @escaping (ClassTableModel) -> Void, failure: @escaping (Error) -> Void) {
SolaSessionManager.solaSession(type: .get, url: "/classtable", parameters: nil, success: { dic in
if let error_code = dic["error_code"] as? Int,
error_code != -1,
let message = dic["message"] as? String {
failure(message)
failure(WPYCustomError.errorCode(error_code, message))
return
}

Expand All @@ -29,6 +29,10 @@ struct ClasstableDataManager {
var index = 0
repeat {
index = Int(arc4random()) % Metadata.Color.fluentColors.count
// 如果全都被选了,那也没办法
if colorConfig.values.count >= Metadata.Color.fluentColors.count {
break
}
} while colorConfig.values.contains(index)
course.setColorIndex(index: index)
colorConfig[course.courseName] = index
Expand All @@ -38,11 +42,11 @@ struct ClasstableDataManager {

success(model)
} else {
failure("解析失败")
failure(WPYCustomError.custom("解析失败"))
}

}, failure: { error in
failure(error.localizedDescription)
failure(error)
})
}

Expand Down
26 changes: 16 additions & 10 deletions WePeiYang/Examtable/Controller/ExamtableController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ExamtableController: UIViewController {
title = "考表"
tableView.frame = self.view.bounds
tableView.separatorStyle = .none
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 24, right: 0)
self.view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
Expand Down Expand Up @@ -58,7 +59,7 @@ class ExamtableController: UIViewController {

private func load() {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.dateFormat = "yyyy-MM-ddHH:mm"
currentTime = formatter.string(from: Date())

data.removeAll()
Expand All @@ -84,21 +85,26 @@ class ExamtableController: UIViewController {
var cell: UITableViewCell
if let title = item.0 {
cell = tableView.dequeueReusableCell(withIdentifier: "TITLE_CELL", for: indexPath)
let label = UILabel()
label.text = title
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = .lightGray
label.sizeToFit()
cell.contentView.addSubview(label)
label.snp.makeConstraints { make in
make.center.equalToSuperview()
if let label = cell.contentView.viewWithTag(-2) as? UILabel {
label.text = title
} else {
let label = UILabel()
label.tag = -2
label.text = title
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = .lightGray
label.sizeToFit()
cell.contentView.addSubview(label)
label.snp.makeConstraints { make in
make.center.equalToSuperview()
}
}
} else if let model = item.1 {
cell = tableView.dequeueReusableCell(withIdentifier: "ExamTableViewCell", for: indexPath)
(cell as? ExamTableViewCell)?.setData(model, displayDate: false)

if currentTime > model.date + model.arrange {
cell.alpha = 0.5
cell.contentView.alpha = 0.5
}
} else {
cell = UITableViewCell()
Expand Down
5 changes: 3 additions & 2 deletions WePeiYang/Examtable/View/ExamTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ class ExamTableViewCell: UITableViewCell {
// let offset = day > 0 ? "\(BookCard.calculateDay(returnTime: exam.date))天" : ""
// dayLabel.text = offset
if displayDate {
nameLabel.text = exam.name + " " + exam.location + "#" + exam.seat
let shortName = exam.name.prefix(6)
nameLabel.text = shortName + " " + exam.location + "#SEAT " + exam.seat
locationLabel.text = exam.date
arrangeLabel.text = exam.arrange
} else {
nameLabel.text = exam.name
locationLabel.text = exam.location + "#" + exam.seat
locationLabel.text = exam.location + "#SEAT " + exam.seat
arrangeLabel.text = exam.arrange
}

Expand Down
17 changes: 14 additions & 3 deletions WePeiYang/Favorite/View/ExamCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import UIKit
class ExamCard: CardView {
private let titleLabel = UILabel()
private let tableView = UITableView()
private var exams: [ExamModel] {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-ddHH:mm"
let currentTime = formatter.string(from: Date())

let exams = ExamAssistant.exams.filter { exam in
return (exam.date + exam.arrange) > currentTime
}

return exams
}

override func initialize() {
super.initialize()
Expand Down Expand Up @@ -55,7 +66,7 @@ class ExamCard: CardView {
}

func load() {
if (ExamAssistant.exams.count > 0) {
if (exams.count > 0) {
self.setState(.data)
tableView.reloadData()
} else {
Expand Down Expand Up @@ -91,7 +102,7 @@ extension ExamCard: UITableViewDataSource {
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? min(ExamAssistant.exams.count, 1) : 1
return section == 0 ? min(exams.count, 1) : 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
Expand All @@ -104,7 +115,7 @@ extension ExamCard: UITableViewDataSource {

let cell = tableView.dequeueReusableCell(withIdentifier: "ExamTableViewCell", for: indexPath) as! ExamTableViewCell

let data = ExamAssistant.exams[indexPath.row]
let data = exams[indexPath.row]
cell.setData(data, displayDate: true)
return cell
}
Expand Down
30 changes: 29 additions & 1 deletion WePeiYang/GPA/Controller/GPAViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,35 @@ class GPAViewController: UIViewController {
self.isRefreshing = false
self.stopRotating()
SwiftMessages.hideLoading()
SwiftMessages.showErrorMessage(body: error.localizedDescription)
let msg = error.localizedDescription
guard let error = error as? WPYCustomError else {
SwiftMessages.showErrorMessage(body: msg)
return
}

switch error {
case .custom(let msg):
SwiftMessages.showErrorMessage(body: msg)
case .errorCode(let code, _):
if code == 40010 {
SwiftMessages.showErrorMessage(body: "办公网密码错误,请重新绑定办公网")
AccountManager.unbind(url: BindingAPIs.unbindTJUAccount, success: {
TWTKeychain.erase(.tju)
TwTUser.shared.tjuBindingState = false

let bindVC = TJUBindingViewController()
bindVC.hidesBottomBarWhenPushed = true
bindVC.completion = { success in
if success {
self.refresh()
}
}
UIViewController.top?.present(bindVC, animated: true, completion: nil)
}, failure: { err in
SwiftMessages.showErrorMessage(body: err.localizedDescription)
})
}
}
})
}

Expand Down
1 change: 1 addition & 0 deletions WePeiYang/GPA/Model/GPASessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct GPASessionManager {
return
}


if let data = dic["data"] as? [String: Any], let model = Mapper<GPAModel>().map(JSON: data) {
success(model)
} else {
Expand Down
48 changes: 21 additions & 27 deletions WePeiYang/Settings/Controller/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class SettingsViewController: UIViewController {
unbindURL = BindingAPIs.unbindLIBAccount
case 1:
BicycleUser.sharedInstance.unbind(success: {
TWTKeychain.erase(.bicycle)
SwiftMessages.showSuccessMessage(body: "解绑成功")
TwTUser.shared.save()
self.tableView.reloadData()
Expand All @@ -174,37 +175,30 @@ class SettingsViewController: UIViewController {
return
}

SolaSessionManager.solaSession(type: .get, url: unbindURL, token: TwTUser.shared.token, success: { dictionary in
guard let errorCode: Int = dictionary["error_code"] as? Int, let message = dictionary["message"] as? String else {
return
AccountManager.unbind(url: unbindURL, success: {
switch indexPathAtRow {
case 0:
TWTKeychain.erase(.library)
TwTUser.shared.libBindingState = false
case 1:
TWTKeychain.erase(.bicycle)
TwTUser.shared.bicycleBindingState = false
case 2:
TWTKeychain.erase(.tju)
TwTUser.shared.tjuBindingState = false
case 3:
TWTKeychain.erase(.network)
TwTUser.shared.WLANBindingState = false
default:
break
}

if errorCode == -1 {
SwiftMessages.showSuccessMessage(body: "解绑成功")

switch indexPathAtRow {
case 0:
TwTUser.shared.libBindingState = false
case 1:
TwTUser.shared.bicycleBindingState = false
case 2:
TwTUser.shared.tjuBindingState = false
case 3:
TwTUser.shared.WLANBindingState = false
default:
break
}

TwTUser.shared.save()
// services[].status can't get renewed data each time user unbinds
// self.services[indexPathAtRow].status = false
self.tableView.reloadData()
} else {
SwiftMessages.showErrorMessage(body: message)
}
TwTUser.shared.save()
// services[].status can't get renewed data each time user unbinds
// self.services[indexPathAtRow].status = false
self.tableView.reloadData()
}, failure: { error in
SwiftMessages.showErrorMessage(body: error.localizedDescription)
self.dismiss(animated: true, completion: nil)
})
}

Expand Down
17 changes: 17 additions & 0 deletions WePeiYang/Shared/Network/AccountManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ struct AccountManager {
})
}

static func unbind(url: String, success: @escaping () -> Void, failure: @escaping (Error) -> Void) {
SolaSessionManager.solaSession(type: .get, url: url, token: TwTUser.shared.token, success: { dictionary in
guard let errorCode: Int = dictionary["error_code"] as? Int, let message = dictionary["message"] as? String else {
failure(WPYCustomError.custom("解析错误"))
return
}

if errorCode == -1 {
SwiftMessages.showSuccessMessage(body: "解绑成功")
success()
} else {
SwiftMessages.showErrorMessage(body: message)
}
})

}

static func getSelf(success: (() -> Void)?, failure: (() -> Void)?) {
SolaSessionManager.solaSession(type: .get, baseURL: "https://open.twtstudio.com", url: "/api/v2/auth/self", parameters: nil, success: { dict in
if let errorno = dict["error_code"] as? Int,
Expand Down
19 changes: 19 additions & 0 deletions WePeiYang/main/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

registerAppNotification(launchOptions: launchOptions)
registerShortcutItems()
showMessage()
showNewFeature()

return true
}

func showMessage() {
SolaSessionManager.solaSession(type: .get, url: "/app/message", token: nil, parameters: nil, success: { dict in
if let data = dict["data"] as? [String: Any],
let version = data["version"] as? Int,
let title = data["title"] as? String,
let message = data["message"] as? String {
let prev = UserDefaults.standard.integer(forKey: MessageKey)
if version > prev {
// new message
SwiftMessages.showNotification(title: title, message: message, handler: { _ in
UserDefaults.standard.set(version, forKey: MessageKey)
SwiftMessages.hideAll()
})
}
}
})
}

func showNewFeature() {
let NewFeatureVersionKey = "NewFeatureVersionKey"
// plus one next version
Expand Down
Loading

0 comments on commit cecf8b3

Please sign in to comment.