Skip to content

Commit

Permalink
Merge pull request #267 from loopandlearn/watch-image-removal
Browse files Browse the repository at this point in the history
Remove Watch Image Feature
  • Loading branch information
marionbarker committed Mar 26, 2024
2 parents 5716f1b + 3c59f1e commit e18f529
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 227 deletions.
4 changes: 0 additions & 4 deletions LoopFollow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
FC16A97D24996747003D6245 /* Alarms.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC16A97C24996747003D6245 /* Alarms.swift */; };
FC16A97F249969E2003D6245 /* Graphs.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC16A97E249969E2003D6245 /* Graphs.swift */; };
FC16A98124996C07003D6245 /* DateTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC16A98024996C07003D6245 /* DateTime.swift */; };
FC1BB3A8252CA4C5003839C2 /* UIImageExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC1BB3A7252CA4C5003839C2 /* UIImageExtension.swift */; };
FC1BDD2B24A22650001B652C /* Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC1BDD2A24A22650001B652C /* Stats.swift */; };
FC1BDD2D24A23204001B652C /* StatsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC1BDD2C24A23204001B652C /* StatsView.swift */; };
FC1BDD3224A2585C001B652C /* DataStructs.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC1BDD2E24A232A3001B652C /* DataStructs.swift */; };
Expand Down Expand Up @@ -230,7 +229,6 @@
FC16A97C24996747003D6245 /* Alarms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alarms.swift; sourceTree = "<group>"; };
FC16A97E249969E2003D6245 /* Graphs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Graphs.swift; sourceTree = "<group>"; };
FC16A98024996C07003D6245 /* DateTime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTime.swift; sourceTree = "<group>"; };
FC1BB3A7252CA4C5003839C2 /* UIImageExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIImageExtension.swift; sourceTree = "<group>"; };
FC1BDD2A24A22650001B652C /* Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Stats.swift; sourceTree = "<group>"; };
FC1BDD2C24A23204001B652C /* StatsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsView.swift; sourceTree = "<group>"; };
FC1BDD2E24A232A3001B652C /* DataStructs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStructs.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -453,7 +451,6 @@
children = (
DD98F54324BCEFEE0007425A /* ShareClientExtension.swift */,
DDCF979324C0D380002C9752 /* UIViewExtension.swift */,
FC1BB3A7252CA4C5003839C2 /* UIImageExtension.swift */,
DD7FFAFC2A72953000C3A304 /* EKEventStore+Extensions.swift */,
);
path = Extensions;
Expand Down Expand Up @@ -1001,7 +998,6 @@
DDF9676E2AD08C6E00C5EB95 /* SiteChange.swift in Sources */,
DDCF979C24C14EFB002C9752 /* AdvancedSettingsViewController.swift in Sources */,
FC97881C2485969B00A7906C /* MainViewController.swift in Sources */,
FC1BB3A8252CA4C5003839C2 /* UIImageExtension.swift in Sources */,
DD493AD52ACF2109009A6922 /* ResumePump.swift in Sources */,
DDCF979624C1443C002C9752 /* GeneralSettingsViewController.swift in Sources */,
FCC0FAC224922A22003E610E /* DictionaryKeyPath.swift in Sources */,
Expand Down
53 changes: 0 additions & 53 deletions LoopFollow/Extensions/UIImageExtension.swift

This file was deleted.

4 changes: 0 additions & 4 deletions LoopFollow/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
<true/>
<key>NSCalendarsUsageDescription</key>
<string>Loop Follow would like to access your calendar to save BG readings</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>Loop Follow would like to access to your photo library to save graph images</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Loop Follow would like to access to your photo library to save graph images</string>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
148 changes: 1 addition & 147 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Charts
import EventKit
import ShareClient
import UserNotifications
import Photos
import AVFAudio

class MainViewController: UIViewController, UITableViewDataSource, ChartViewDelegate, UNUserNotificationCenterDelegate, UIScrollViewDelegate {

Expand Down Expand Up @@ -505,146 +505,7 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
graphics:[String:String]=["Flat":"","DoubleUp":"↑↑","SingleUp":"","FortyFiveUp":"","FortyFiveDown":"↘︎","SingleDown":"","DoubleDown":"↓↓","None":"-","NONE":"-","NOT COMPUTABLE":"-","RATE OUT OF RANGE":"-", "": "-"]
return graphics[value]!
}

// Test code to save an image of graph for viewing on watch
func saveChartImage() {
var originalColor = BGChart.backgroundColor
BGChart.backgroundColor = NSUIColor.black
guard var image = BGChart.getChartImage(transparent: true) else {
BGChart.backgroundColor = originalColor
return }
var newImage = image.resizeImage(448.0, landscape: false, opaque: false, contentMode: .scaleAspectFit)
createAlbums(name1: "Loop Follow", name2: "Loop Follow Old")
if let collection1 = fetchAssetCollection("Loop Follow"), let collection2 = fetchAssetCollection("Loop Follow Old") {
deleteExistingImagesFromCollection(collection: collection1)
saveImageToAssetCollection(image, collection1: collection1, collection2: collection2)
}

BGChart.backgroundColor = originalColor
}

func createAlbums(name1: String, name2: String) {
if let collection1 = fetchAssetCollection(name1) {
} else {
// Album does not exist, create it and attempt to save the image
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name1)
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not create the album")
if let err = error {
NSLog("Error: \(err)")
}
return
}

if let newCollection1 = self.fetchAssetCollection(name1) {
}
})
}

if let collection2 = fetchAssetCollection(name2) {
} else {
// Album does not exist, create it and attempt to save the image
PHPhotoLibrary.shared().performChanges({
PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name2)
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not create the album")
if let err = error {
NSLog("Error: \(err)")
}
return
}

if let newCollection2 = self.fetchAssetCollection(name2) {
}
})
}
}

func fetchAssetCollection(_ name: String) -> PHAssetCollection? {

let fetchOption = PHFetchOptions()
fetchOption.predicate = NSPredicate(format: "title == '" + name + "'")

let fetchResult = PHAssetCollection.fetchAssetCollections(
with: PHAssetCollectionType.album,
subtype: PHAssetCollectionSubtype.albumRegular,
options: fetchOption)

return fetchResult.firstObject
}

func deleteOldImages() {
if let collection = fetchAssetCollection("Loop Follow Old") {
let library = PHPhotoLibrary.shared()
library.performChanges({
let fetchOptions = PHFetchOptions()
let allPhotos = PHAsset.fetchAssets(in: collection, options: .none)
PHAssetChangeRequest.deleteAssets(allPhotos)
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not delete the image")
if let err = error {
NSLog("Error: " + err.localizedDescription)
}
return
}
})
}

self.sendGeneralNotification(self, title: "Watch Face Cleanup", subtitle: "", body: "Delete old watch face graph images", timer: 86400)


}

func deleteExistingImagesFromCollection(collection: PHAssetCollection) {

// This code removes existing photos from collection but does not delete them
if collection.estimatedAssetCount < 1 { return }

PHPhotoLibrary.shared().performChanges( {

if let request = PHAssetCollectionChangeRequest(for: collection) {
request.removeAssets(at: [0])
}

}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not delete the image")
if let err = error {
NSLog("Error: " + err.localizedDescription)
}
return
}
})
}

func saveImageToAssetCollection(_ image: UIImage, collection1: PHAssetCollection, collection2: PHAssetCollection) {

PHPhotoLibrary.shared().performChanges({

let creationRequest = PHAssetCreationRequest.creationRequestForAsset(from: image)
if let request = PHAssetCollectionChangeRequest(for: collection1),
let placeHolder = creationRequest.placeholderForCreatedAsset {
request.addAssets([placeHolder] as NSFastEnumeration)
}
if let request2 = PHAssetCollectionChangeRequest(for: collection2),
let placeHolder2 = creationRequest.placeholderForCreatedAsset {
request2.addAssets([placeHolder2] as NSFastEnumeration)
}
}, completionHandler: { (success: Bool, error: Error?) in
guard success == true && error == nil else {
NSLog("Could not save the image")
if let err = error {
NSLog("Error: " + err.localizedDescription)
}
return
}
})
}

func writeCalendar() {
if UserDefaultsRepository.debugLog.value {
self.writeDebugLog(value: "Write calendar start")
Expand Down Expand Up @@ -768,13 +629,6 @@ class MainViewController: UIViewController, UITableViewDataSource, ChartViewDele
// Display error to user
//if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Error: Calendar Write") }
}

if UserDefaultsRepository.saveImage.value {
DispatchQueue.main.async {
self.saveChartImage()
}

}
}


Expand Down
10 changes: 0 additions & 10 deletions LoopFollow/ViewControllers/SettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,6 @@ class SettingsViewController: FormViewController {
), onDismiss: nil)

}
<<< LabelRow("Clear Images"){ row in
row.title = "Delete Watch Face Images"
}.onCellSelection{ cell,row in
if UserDefaultsRepository.saveImage.value {
guard let mainScreen = self.tabBarController!.viewControllers?[0] as? MainViewController else { return }

mainScreen.deleteOldImages()
mainScreen.saveChartImage()
}
}

+++ Section("Advanced Settings")
<<< ButtonRow() {
Expand Down
8 changes: 0 additions & 8 deletions LoopFollow/ViewControllers/WatchSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,6 @@ class WatchSettingsViewController: FormViewController {
guard let value = row.value else { return }
UserDefaultsRepository.watchLine2.value = value
}
<<< SwitchRow("saveImage"){ row in
row.title = "Save Graph Image for Watch Face"
row.value = UserDefaultsRepository.saveImage.value
}.onChange { [weak self] row in
guard let value = row.value else { return }
UserDefaultsRepository.saveImage.value = value
}



+++ Section(header: "Available Variables", footer: "")
Expand Down
1 change: 0 additions & 1 deletion LoopFollow/repository/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class UserDefaultsRepository {
static let writeCalendarEvent = UserDefaultsValue<Bool>(key: "writeCalendarEvent", default: false)
static let watchLine1 = UserDefaultsValue<String>(key: "watchLine1", default: "%BG% %DIRECTION% %DELTA% %MINAGO%")
static let watchLine2 = UserDefaultsValue<String>(key: "watchLine2", default: "C:%COB% I:%IOB% B:%BASAL%")
static let saveImage = UserDefaultsValue<Bool>(key: "saveImage", default: false)

// Alarm Settings
static let systemOutputVolume = UserDefaultsValue<Float>(key: "systemOutputVolume", default: 0.5)
Expand Down

0 comments on commit e18f529

Please sign in to comment.