Compare commits

...

3 commits

Author SHA1 Message Date
TrevKnows
02a0f52c0c
Merge pull request #127 from adafruit/Caching-Fix
Network response caching - Fixed
2023-03-14 10:29:17 -04:00
TrevKnows
598b340a2c First commit - Fixed
Ran into an issue loading new projects into the project list. The app was using the cached list while there was a network connection. The Network library has been updated, so I needed to fix how the project files are cached.
2023-03-14 10:10:43 -04:00
TrevKnows
bbf94b1b34
Merge pull request #126 from adafruit/Adding-MIT-License
Adding mit license
2023-02-16 12:25:18 -05:00
9 changed files with 63 additions and 17 deletions

View file

@ -379,6 +379,7 @@
D58E1C8628A2B0DE00AB683E /* Wifi View */ = {
isa = PBXGroup;
children = (
D567E2DD28C8D3E20009F768 /* SettingsView */,
D58E1C8728A2B10B00AB683E /* WifiView.swift */,
D567E2B728C137880009F768 /* WifiCell.swift */,
D51D1412293A53BD0028AEDD /* WifiCellViewModel.swift */,
@ -411,7 +412,6 @@
D59DFDB2268CCEAC001737F6 /* Views */ = {
isa = PBXGroup;
children = (
D567E2DD28C8D3E20009F768 /* SettingsView */,
D58E1C8628A2B0DE00AB683E /* Wifi View */,
D5507ACE26C668BC00512BAA /* UI Components */,
D59DFDB3268CCEB9001737F6 /* Onboarding Views */,

View file

@ -26,7 +26,7 @@ class NetworkService: ObservableObject {
// Session Configuration & Caching Policy
let configuration = URLSessionConfiguration.default
// configuration.requestCachePolicy = .useProtocolCachePolicy
configuration.requestCachePolicy = .returnCacheDataElseLoad
return URLSession(configuration: configuration)
}()
@ -34,8 +34,8 @@ class NetworkService: ObservableObject {
func fetch(completion: @escaping() -> Void) {
print("Attempting Network Request")
let request = URLRequest(url: URL(string: AdafruitInfo.baseURL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
let request = URLRequest(url: URL(string: AdafruitInfo.baseURL)!, cachePolicy: URLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 60.0)
let task = session.dataTask(with: request) { data, response, error in
if let error = error {
print("error: \(error)")

View file

@ -17,7 +17,7 @@ struct RunItButton: View {
.cornerRadius(25)
.foregroundColor(Color("pyleap_pink"))
Text("Run it!")
Text("Run")
.font(Font.custom("ReadexPro-Regular", size: 25))
.foregroundColor(Color.white)
.frame(height: 50)

View file

@ -7,10 +7,49 @@
import Foundation
import SwiftUI
import Network
import Combine
class InternetConnectionManager: ObservableObject {
private let monitor = NWPathMonitor()
private let queue = DispatchQueue(label: "InternetConnectionMonitor")
@Published var isConnected = false
init() {
startMonitoring(completion: {
monitor.pathUpdateHandler = { path in
DispatchQueue.main.async {
let newIsConnected = path.status == .satisfied
if self.isConnected != newIsConnected {
self.isConnected = newIsConnected
print("net: \(path.status) \(self.isConnected)")
}
}
}
})
}
func startMonitoring(completion:()->Void) {
print("Start Monitoring Network")
monitor.start(queue: queue)
completion()
}
deinit {
print("Network Deinit")
monitor.cancel()
}
}
class MainSelectionViewModel: ObservableObject {
@ObservedObject var networkModel = NetworkService()
@ObservedObject var networkMonitor = InternetConnectionManager()
let fileManager = FileManager.default
@ -19,23 +58,30 @@ class MainSelectionViewModel: ObservableObject {
let dataStore = DataStore()
@Published var pdemos : [ResultItem] = []
var networkMonitorCancellable: AnyCancellable?
init() {
let fileURL = documentsDirectory.appendingPathComponent("StandardPyLeapProjects.json")
if fileManager.fileExists(atPath: fileURL.relativePath) {
networkMonitorCancellable = networkMonitor.$isConnected.sink { isConnected in
if isConnected {
print("The device is currently connected to the internet.")
// Perform some action when the device is connected to the internet.
self.networkModel.fetch {
self.pdemos = self.dataStore.loadDefaultList()
}
} else {
print("The device is not currently connected to the internet.")
// Perform some action when the device is not connected to the internet.
print("Loading cached remote data.")
self.pdemos = self.dataStore.loadDefaultList()
} else {
print("Cached data not found. Fetching default list.")
networkModel.fetch {
self.pdemos = self.dataStore.loadDefaultList()
}
}
}