Compare commits

...

4 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
TrevKnows
b546ad5040 Commit 2023-02-16 12:23:54 -05:00
10 changed files with 90 additions and 19 deletions

25
LICENSE.txt Normal file
View file

@ -0,0 +1,25 @@
MIT License
Copyright (c) 2023 Adafruit Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -379,6 +379,7 @@
D58E1C8628A2B0DE00AB683E /* Wifi View */ = { D58E1C8628A2B0DE00AB683E /* Wifi View */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D567E2DD28C8D3E20009F768 /* SettingsView */,
D58E1C8728A2B10B00AB683E /* WifiView.swift */, D58E1C8728A2B10B00AB683E /* WifiView.swift */,
D567E2B728C137880009F768 /* WifiCell.swift */, D567E2B728C137880009F768 /* WifiCell.swift */,
D51D1412293A53BD0028AEDD /* WifiCellViewModel.swift */, D51D1412293A53BD0028AEDD /* WifiCellViewModel.swift */,
@ -411,7 +412,6 @@
D59DFDB2268CCEAC001737F6 /* Views */ = { D59DFDB2268CCEAC001737F6 /* Views */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D567E2DD28C8D3E20009F768 /* SettingsView */,
D58E1C8628A2B0DE00AB683E /* Wifi View */, D58E1C8628A2B0DE00AB683E /* Wifi View */,
D5507ACE26C668BC00512BAA /* UI Components */, D5507ACE26C668BC00512BAA /* UI Components */,
D59DFDB3268CCEB9001737F6 /* Onboarding Views */, D59DFDB3268CCEB9001737F6 /* Onboarding Views */,
@ -886,7 +886,7 @@
DEVELOPMENT_TEAM = 2X94RM7457; DEVELOPMENT_TEAM = 2X94RM7457;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = PyLeap/Info.plist; INFOPLIST_FILE = PyLeap/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.1; IPHONEOS_DEPLOYMENT_TARGET = 15.5;
LD_RUNPATH_SEARCH_PATHS = ( LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",
@ -916,7 +916,7 @@
DEVELOPMENT_TEAM = 2X94RM7457; DEVELOPMENT_TEAM = 2X94RM7457;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = PyLeap/Info.plist; INFOPLIST_FILE = PyLeap/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.1; IPHONEOS_DEPLOYMENT_TARGET = 15.5;
LD_RUNPATH_SEARCH_PATHS = ( LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"@executable_path/Frameworks", "@executable_path/Frameworks",

View file

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

View file

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

View file

@ -7,10 +7,49 @@
import Foundation import Foundation
import SwiftUI 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 { class MainSelectionViewModel: ObservableObject {
@ObservedObject var networkModel = NetworkService() @ObservedObject var networkModel = NetworkService()
@ObservedObject var networkMonitor = InternetConnectionManager()
let fileManager = FileManager.default let fileManager = FileManager.default
@ -19,22 +58,29 @@ class MainSelectionViewModel: ObservableObject {
let dataStore = DataStore() let dataStore = DataStore()
@Published var pdemos : [ResultItem] = [] @Published var pdemos : [ResultItem] = []
var networkMonitorCancellable: AnyCancellable?
init() { init() {
let fileURL = documentsDirectory.appendingPathComponent("StandardPyLeapProjects.json") let fileURL = documentsDirectory.appendingPathComponent("StandardPyLeapProjects.json")
if fileManager.fileExists(atPath: fileURL.relativePath) {
print("Loading cached remote data.") networkMonitorCancellable = networkMonitor.$isConnected.sink { isConnected in
self.pdemos = self.dataStore.loadDefaultList() if isConnected {
print("The device is currently connected to the internet.")
} else { // Perform some action when the device is connected to the internet.
print("Cached data not found. Fetching default list.") self.networkModel.fetch {
networkModel.fetch { self.pdemos = self.dataStore.loadDefaultList()
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()
}
}
} }