Skip to content

Commit c0700c9

Browse files
committed
manage keep screen always on
1 parent fbd582e commit c0700c9

5 files changed

+85
-6
lines changed

Diff for: OpenGpxTracker/Preferences.swift

+24
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ let kDefaultsKeyDateFormatUseEN: String = "DateFormatPresetUseEN"
4040
/// Key on Defaults for the folder where GPX files are store, `nil` means default folder
4141
let kDefaultsKeyGPXFilesFolder: String = "GPXFilesFolder"
4242

43+
let kDefaultsKeyKeepScreenAlwaysOn: String = "KeepScreenAlwaysOn"
44+
4345
/// A class to handle app preferences in one single place.
4446
/// When the app starts for the first time the following preferences are set:
4547
///
@@ -86,6 +88,9 @@ class Preferences: NSObject {
8688
///
8789
private var _gpxFilesFolderBookmark: Data?
8890

91+
///
92+
private var _keepScreenAlwaysOn: Bool = false
93+
8994
/// UserDefaults.standard shortcut
9095
private let defaults = UserDefaults.standard
9196

@@ -160,6 +165,13 @@ class Preferences: NSObject {
160165
_gpxFilesFolderBookmark = gpxFilesFolderBookmark
161166
print("** Preferences:: loaded preference from defaults gpxFilesFolderBookmark \(gpxFilesFolderBookmark)")
162167
}
168+
169+
// load previous date format, to use EN locale instead of local locale
170+
if let keepScreenAlwaysOnBool = defaults.object(forKey: kDefaultsKeyKeepScreenAlwaysOn) as? Bool {
171+
_keepScreenAlwaysOn = keepScreenAlwaysOnBool
172+
print("** Preferences:: loaded preference from defaults keepScreenAlwaysOn \(keepScreenAlwaysOnBool)")
173+
}
174+
163175
}
164176

165177
/// If true, user prefers to display imperial units (miles, feets). Otherwise metric units
@@ -293,6 +305,18 @@ class Preferences: NSObject {
293305
}
294306
}
295307

308+
/// Get and sets whether to set the screen always On or not
309+
var keepScreenAlwaysOn: Bool {
310+
get {
311+
return _keepScreenAlwaysOn
312+
}
313+
set {
314+
_keepScreenAlwaysOn = newValue
315+
defaults.set(newValue, forKey: kDefaultsKeyKeepScreenAlwaysOn)
316+
print("** Preferences:: setting keepScreenAlwaysOn: \(newValue)")
317+
}
318+
}
319+
296320
var gpxFilesFolderURL: URL? {
297321
get {
298322
guard let bookmarkData = self._gpxFilesFolderBookmark else {

Diff for: OpenGpxTracker/PreferencesTableViewController.swift

+45-5
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,33 @@ import CoreServices
1616
/// Units Section Id in PreferencesTableViewController
1717
let kUnitsSection = 0
1818

19+
/// Screen always on section id
20+
let kScreenSection = 1
21+
1922
/// Cache Section Id in PreferencesTableViewController
20-
let kCacheSection = 1
23+
let kCacheSection = 2
2124

2225
/// Map Source Section Id in PreferencesTableViewController
23-
let kMapSourceSection = 2
26+
let kMapSourceSection = 3
2427

2528
/// Activity Type Section Id in PreferencesTableViewController
26-
let kActivityTypeSection = 3
29+
let kActivityTypeSection = 4
2730

2831
/// Default Name Section Id in PreferencesTableViewController
29-
let kDefaultNameSection = 4
32+
let kDefaultNameSection = 5
3033

3134
/// GPX Files Location Section Id in PreferencesTableViewController
32-
let kGPXFilesLocationSection = 5
35+
let kGPXFilesLocationSection = 6
3336

3437
/// Cell Id of the Use Imperial units in UnitsSection
3538
let kUseImperialUnitsCell = 0
3639

40+
41+
/// Cell Id of the keepScreenAlwaysOnl units in ScreenSection
42+
let kKeepScreenAlwaysOnCell = 0
43+
44+
45+
3746
/// Cell Id for Use offline cache in CacheSection of PreferencesTableViewController
3847
let kUseOfflineCacheCell = 0
3948

@@ -119,6 +128,7 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
119128
case kActivityTypeSection: return NSLocalizedString("ACTIVITY_TYPE", comment: "no comment")
120129
case kDefaultNameSection: return NSLocalizedString("DEFAULT_NAME_SECTION", comment: "no comment")
121130
case kGPXFilesLocationSection: return NSLocalizedString("GPX_FILES_FOLDER", comment: "no comment")
131+
case kScreenSection: return NSLocalizedString("SCREEN", comment: "no comment")
122132
default: fatalError("Unknown section")
123133
}
124134
}
@@ -134,6 +144,7 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
134144
case kActivityTypeSection: return CLActivityType.count
135145
case kDefaultNameSection: return 1
136146
case kGPXFilesLocationSection: return 1
147+
case kScreenSection: return 1
137148
default: fatalError("Unknown section")
138149
}
139150
}
@@ -166,6 +177,19 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
166177
}
167178
}
168179

180+
// Units section
181+
if indexPath.section == kScreenSection {
182+
switch indexPath.row {
183+
case kKeepScreenAlwaysOnCell:
184+
cell = UITableViewCell(style: .value1, reuseIdentifier: "CacheCell")
185+
cell.textLabel?.text = NSLocalizedString("KEEP_SCREEN_ALWAYS_ON", comment: "no comment")
186+
if preferences.keepScreenAlwaysOn {
187+
cell.accessoryType = .checkmark
188+
}
189+
default: fatalError("Unknown section")
190+
}
191+
}
192+
169193
// Cache Section
170194
if indexPath.section == kCacheSection {
171195
switch indexPath.row {
@@ -264,6 +288,22 @@ class PreferencesTableViewController: UITableViewController, UINavigationBarDele
264288
}
265289
}
266290

291+
if indexPath.section == kScreenSection {
292+
switch indexPath.row {
293+
case kKeepScreenAlwaysOnCell:
294+
let newKeepScreenAlwaysOn = !preferences.keepScreenAlwaysOn
295+
preferences.keepScreenAlwaysOn = newKeepScreenAlwaysOn
296+
print("PreferencesTableViewController: toggle keep screen always on to \(newKeepScreenAlwaysOn)")
297+
// Update cell UI
298+
tableView.cellForRow(at: indexPath)?.accessoryType = newKeepScreenAlwaysOn ? .checkmark : .none
299+
// Notify the map
300+
self.delegate?.didUpdateKeepScreenAlwaysOn(newKeepScreenAlwaysOn)
301+
default:
302+
fatalError("didSelectRowAt: Unknown cell")
303+
}
304+
}
305+
306+
267307
if indexPath.section == kCacheSection { // 0 -> sets and unsets cache
268308
switch indexPath.row {
269309
case kUseOfflineCacheCell:

Diff for: OpenGpxTracker/PreferencesTableViewControllerDelegate.swift

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ protocol PreferencesTableViewControllerDelegate: AnyObject {
2222
/// User updated the usage of imperial units
2323
func didUpdateUseImperial(_ newUseImperial: Bool)
2424

25+
/// User updated the keep screen always on option
26+
func didUpdateKeepScreenAlwaysOn(_ newKeepScreenAlwaysOn: Bool)
27+
2528
/// User updated the activity type
2629
func didUpdateActivityType(_ newActivityType: Int)
2730

Diff for: OpenGpxTracker/ViewController.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ extension ViewController: StopWatchDelegate {
13131313

13141314
extension ViewController: PreferencesTableViewControllerDelegate {
13151315

1316+
13161317
/// Update the activity type that the location manager is using.
13171318
///
13181319
/// When user changes the activity type in preferences, this function is invoked to update the activity type of the location manager.
@@ -1356,7 +1357,14 @@ extension ViewController: PreferencesTableViewControllerDelegate {
13561357
// In regular circunstances it will go to the new units relatively fast.
13571358
speedLabel.text = kUnknownSpeedText
13581359
signalAccuracyLabel.text = kUnknownAccuracyText
1359-
}}
1360+
}
1361+
1362+
// User changed the setting of use imperial units.
1363+
func didUpdateKeepScreenAlwaysOn(_ newKeepScreenAlwaysOn: Bool) {
1364+
print("PreferencesTableViewControllerDelegate:: didUpdateKeepScreenAlwaysOn: \(newKeepScreenAlwaysOn)")
1365+
UIApplication.shared.isIdleTimerDisabled = newKeepScreenAlwaysOn
1366+
}
1367+
}
13601368

13611369
/// Extends `ViewController`` to support `GPXFilesTableViewControllerDelegate` function
13621370
/// that loads into the map a the file selected by the user.

Diff for: OpenGpxTracker/en.lproj/Localizable.strings

+4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@
3333
"SHARE" = "Share";
3434
"LOADING_FILE" = "Loading GPX File...";
3535
"ABOUT" = "About";
36+
37+
// Preferences
3638
"PREFERENCES" = "Preferences";
3739
"UNITS" = "Units";
40+
"SCREEN" = "Screen";
41+
"KEEP_SCREEN_ALWAYS_ON" = "Keep Screen always on?";
3842
"CACHE" = "Cache";
3943
"MAP_SOURCE" = "Map Source";
4044
"ACTIVITY_TYPE" = "Activity Type";

0 commit comments

Comments
 (0)