option to select textcolor per m5Stack

This commit is contained in:
Johan Degraeve 2019-10-06 22:11:50 +02:00
parent 8599820a9b
commit a0f62239cb
6 changed files with 106 additions and 13 deletions

View File

@ -15,15 +15,35 @@ enum ConstantsM5Stack {
}
enum M5StackTextColor:UInt32, CaseIterable {
enum M5StackTextColor:UInt16, CaseIterable {
case red = 0xFF0000
// here what I found as hex values for RGB565 colors
// TFT_BLACK 0x0000
// TFT_NAVY 0x000F
// TFT_DARKGREEN 0x03E0
// TFT_DARKCYAN 0x03EF
// TFT_MAROON 0x7800
// TFT_PURPLE 0x780F
// TFT_OLIVE 0x7BE0
// TFT_LIGHTGREY 0xC618
// TFT_DARKGREY 0x7BEF
// TFT_BLUE 0x001F
// TFT_GREEN 0x07E0
// TFT_CYAN 0x07FF
// TFT_RED 0xF800
// TFT_MAGENTA 0xF81F
// TFT_YELLOW 0xFFE0
// TFT_WHITE 0xFFFF
// TFT_ORANGE 0xFDA0
// TFT_GREENYELLOW 0xB7E0
case green = 0x00FF00
case red = 0xF800
case white = 0xFFFFFF
case green = 0x07E0
case yellow = 0xFFFF00
case white = 0xFFFF
case yellow = 0xFFE0
var description:String {
switch self {
@ -38,7 +58,7 @@ enum M5StackTextColor:UInt32, CaseIterable {
}
}
init?(forUInt32: UInt32) {
self.init(rawValue: forUInt32)
init?(forUInt16: UInt16) {
self.init(rawValue: forUInt16)
}
}

View File

@ -12,7 +12,7 @@ import CoreData
public class M5Stack: NSManagedObject {
/// create M5Stack, shouldconnect default value = true
init(address: String, name: String, nsManagedObjectContext:NSManagedObjectContext) {
init(address: String, name: String, textColor: M5StackTextColor, nsManagedObjectContext:NSManagedObjectContext) {
let entity = NSEntityDescription.entity(forEntityName: "M5Stack", in: nsManagedObjectContext)!
@ -21,6 +21,7 @@ public class M5Stack: NSManagedObject {
self.address = address
self.name = name
self.shouldconnect = true
self.textcolor = Int32(textColor.rawValue)
}

View File

@ -2,7 +2,7 @@
// M5Stack+CoreDataProperties.swift
// xdrip
//
// Created by Johan Degraeve on 23/09/2019.
// Created by Johan Degraeve on 06/10/2019.
// Copyright © 2019 Johan Degraeve. All rights reserved.
//
//
@ -21,6 +21,7 @@ extension M5Stack {
@NSManaged public var blepassword: String?
@NSManaged public var name: String
@NSManaged public var shouldconnect: Bool
@NSManaged public var textcolor: Int32
@NSManaged public var m5StackName: M5StackName?
}

View File

@ -524,7 +524,7 @@ extension UserDefaults {
get {
let textColorAsInt = integer(forKey: Key.m5StackTextColor.rawValue)
if textColorAsInt > 0 {
return M5StackTextColor(forUInt32: UInt32(textColorAsInt))
return M5StackTextColor(forUInt16: UInt16(textColorAsInt))
} else {
return nil
}

View File

@ -240,7 +240,7 @@ extension M5StackManager: M5StackBluetoothDelegate {
bluetoothTransmitter.stopScanning()
// create a new M5Stack with new peripheral's address and name
let newM5Stack = M5Stack(address: address, name: name, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
let newM5Stack = M5Stack(address: address, name: name, textColor: UserDefaults.standard.m5StackTextColor ?? ConstantsM5Stack.defaultTextColor, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
// add to list of m5StacksBlueToothTransmitters
m5StacksBlueToothTransmitters[newM5Stack] = bluetoothTransmitter

View File

@ -19,6 +19,9 @@ fileprivate enum Setting:Int, CaseIterable {
/// ble password
case blePassword = 4
/// textColor
case textColor = 5
}
/// UIViewController to show
@ -84,11 +87,20 @@ final class M5StackViewController: UIViewController {
private weak var m5StackManager: M5StackManaging?
/// name given by user as alias , to easier recognize different M5Stacks
///
/// temp storage of value while user is editing the M5Stack attributes
private var userDefinedName: String?
/// should the app try to connect automatically to the M5Stack or not, setting to false because compiler needs to have a value. It's set to the correct value in configure
///
/// temp storage of value while user is editing the M5Stack attributes
private var shouldConnect: Bool = false
/// textColor to be used in M5Stack
///
/// temp storage of value while user is editing the M5Stack attributes
private var textColor: M5StackTextColor?
/// M5StackNames accessor
private var m5StackNameAccessor: M5StackNameAccessor?
@ -113,6 +125,9 @@ final class M5StackViewController: UIViewController {
// temporary store the value of shouldConnect, user can change this via the view, it will be stored back in the m5StackAsNSObject only after clicking 'done' button
shouldConnect = m5StackAsNSObject.shouldconnect
// temporary store the value of textColor, user can change this via the view, it will be stored back in the m5StackAsNSObject only after clicking 'done' button
textColor = M5StackTextColor(forUInt16: UInt16(m5StackAsNSObject.textcolor))
// don't delete the M5Stack when going back to prevous viewcontroller
deleteM5StackWhenClosingViewController = false
@ -223,9 +238,13 @@ final class M5StackViewController: UIViewController {
m5StackAsNSObject.m5StackName = m5Stackname
}
// shouldConnect to be stored
// store value of shouldconnect
m5StackAsNSObject.shouldconnect = shouldConnect
debuglogging("shouldconnect = " + shouldConnect.description)
// store value of textcolor
if let textColor = textColor {
m5StackAsNSObject.textcolor = Int32(textColor.rawValue)
}
// save all changes now
m5StackManager?.save()
@ -255,6 +274,7 @@ final class M5StackViewController: UIViewController {
// assign local variables
self.shouldConnect = m5Stack.shouldconnect
self.userDefinedName = m5Stack.m5StackName?.userDefinedName //should be nil anyway
self.textColor = M5StackTextColor(forUInt16: UInt16(m5Stack.textcolor))
// reload the full section , all rows in the tableView
self.tableView.reloadSections(IndexSet(integer: 0), with: .none)
@ -428,6 +448,20 @@ extension M5StackViewController: UITableViewDataSource, UITableViewDelegate {
cell.textLabel?.text = Texts_M5StackView.m5StackAlias
cell.detailTextLabel?.text = userDefinedName
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
case .textColor:
cell.textLabel?.text = Texts_SettingsView.m5StackTextColor
if let textColor = textColor {
cell.detailTextLabel?.text = textColor.description
} else {
if let textColor = UserDefaults.standard.m5StackTextColor {
cell.detailTextLabel?.text = textColor.description
} else {
cell.detailTextLabel?.text = ConstantsM5Stack.defaultTextColor.description
}
}
}
return cell
@ -490,7 +524,44 @@ extension M5StackViewController: UITableViewDataSource, UITableViewDelegate {
// present the alert
self.present(alert, animated: true, completion: nil)
case .textColor:
var texts = [String]()
var colors = [M5StackTextColor]()
for textColor in M5StackTextColor.allCases {
texts.append(textColor.description)
colors.append(textColor)
}
//find index for color stored in M5Stack or userdefaults
var selectedRow:Int?
if let textColor = textColor {
selectedRow = texts.firstIndex(of:textColor.description)
} else if let textColor = UserDefaults.standard.m5StackTextColor?.description {
selectedRow = texts.firstIndex(of:textColor)
}
// configure PickerViewData
let pickerViewData = PickerViewData(withMainTitle: nil, withSubTitle: Texts_SettingsView.m5StackTextColor, withData: texts, selectedRow: selectedRow, withPriority: nil, actionButtonText: nil, cancelButtonText: nil, onActionClick: {(_ index: Int) in
if index != selectedRow {
// set temp value textColor to new textColor
self.textColor = colors[index]
// reload table
tableView.reloadRows(at: [IndexPath(row: Setting.textColor.rawValue, section: 0)], with: .none)
// enable the done button
self.doneButtonOutlet.enable()
}
}, onCancelClick: nil, didSelectRowHandler: nil)
// create and present PickerViewController
PickerViewController.displayPickerViewController(pickerViewData: pickerViewData, parentController: self)
}
}