Skip to content

Commit

Permalink
fix: code optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
s8ngyu committed Dec 17, 2023
1 parent d3d5b31 commit ca0cbdf
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 38 deletions.
2 changes: 0 additions & 2 deletions ios/OTL Watch App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import SwiftUI

struct ContentView: View {
@ObservedObject var viewModel = WatchViewModel()

@State private var loginState: Bool = true

var body: some View {
Expand Down
16 changes: 8 additions & 8 deletions ios/OTL Watch App/OTLAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ class OTLAPI {
let json = try decoder.decode([Timetable].self, from: data)
completion(.success(json))
} catch {
print("Error: \(error)")
print("getTimetables Error: \(error)")
completion(.failure(error))
}
case .failure(let error):
print("Error: \(error)")
print("getTimetables Error: \(error)")
completion(.failure(error))
}
}
Expand All @@ -235,11 +235,11 @@ class OTLAPI {
let json = try decoder.decode([Semester].self, from: data)
completion(.success(json))
} catch {
print ("Error \(error)")
print ("getSemesters Error \(error)")
completion(.failure(error))
}
case .failure(let error):
print("Error: \(error)")
print("getSemesters Error: \(error)")
completion(.failure(error))
}
}
Expand Down Expand Up @@ -271,11 +271,11 @@ class OTLAPI {
}
completion(.success([timetable]))
} catch {
print("Error: \(error)")
print("getActualTimetable Error: \(error)")
completion(.failure(error))
}
case .failure(let error):
print("Error: \(error)")
print("getActualTimetable Error: \(error)")
completion(.failure(error))
}
}
Expand Down Expand Up @@ -315,11 +315,11 @@ class OTLAPI {
})
completion(.success(semesters))
} catch {
print("Error: \(error)")
print("getActualSemesters Error: \(error)")
completion(.failure(error))
}
case .failure(let error):
print("Error: \(error)")
print("getActualSemesters Error: \(error)")
completion(.failure(error))
}
}
Expand Down
2 changes: 2 additions & 0 deletions ios/OTL Watch App/Pages/WeeklyTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Alamofire
struct WeeklyTableView: View {
@Binding var loginState: Bool

@ObservedObject var viewModel = WatchViewModel()

@AppStorage("sessionID") var sessionID: String = ""
@AppStorage("userID") var userID: String = ""

Expand Down
17 changes: 0 additions & 17 deletions ios/OTL Watch App/WatchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@ class WatchViewModel: NSObject, ObservableObject {
extension WatchViewModel: WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { }

func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
DispatchQueue.main.async {
guard let rawMethod = message["method"] as? String, let method = WatchReceiveMethod(rawValue: rawMethod) else {
return
}

switch method {
case .sendSessionID:
self.sessionID = message["data"] as? String ?? ""
UserDefaults.standard.set(message["data"] as? String ?? "", forKey: "sessionID")
case .sendUserID:
self.userID = message["data"] as? String ?? ""
UserDefaults.standard.set(message["data"] as? String ?? "", forKey: "userID")
}
}
}

func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
DispatchQueue.main.async {
guard let rawMethod = userInfo["method"] as? String, let method = WatchReceiveMethod(rawValue: rawMethod) else {
Expand Down
14 changes: 3 additions & 11 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import WatchConnectivity
}

private func initFlutterChannel() {
print("AppDelegate")
if let controller = window?.rootViewController as? FlutterViewController {
let channel = FlutterMethodChannel(
name: "org.sparcs.otlplus.watchkitapp",
Expand All @@ -35,18 +34,11 @@ import WatchConnectivity
let methodData = call.arguments as? [String: Any],
let method = methodData["method"],
let data = methodData["data"] else {
print("failllllllll")
print(self?.session?.isPaired)
print("llllllllllll")
result(false)
return
}
print("watchData Start")
let watchData: [String: Any] = ["method": method, "data": data]
print(watchData)
print("watchData End")
watchSession.transferUserInfo(watchData)
// watchSession.sendMessage(watchData, replyHandler: nil, errorHandler: nil)
result(true)
default:
result(FlutterMethodNotImplemented)
Expand All @@ -63,13 +55,13 @@ extension AppDelegate: WCSessionDelegate {

func sessionDidDeactivate(_ session: WCSession) { }

func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
DispatchQueue.main.async {
if let method = message["method"] as? String, let controller = self.window?.rootViewController as? FlutterViewController {
if let method = userInfo["method"] as? String, let controller = self.window?.rootViewController as? FlutterViewController {
let channel = FlutterMethodChannel(
name: "org.sparcs.otlplus.watchkitapp",
binaryMessenger: controller.binaryMessenger)
channel.invokeMethod(method, arguments: message)
channel.invokeMethod(method, arguments: userInfo)
}
}
}
Expand Down

0 comments on commit ca0cbdf

Please sign in to comment.