ContentView cleanup

This commit is contained in:
Maxim Ananov 2020-12-13 17:01:29 +03:00
parent 72fcf85524
commit 192f36a162
3 changed files with 136 additions and 220 deletions

Binary file not shown.

View File

@ -1,56 +0,0 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"colors" : [
{
"idiom" : "universal",
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "1.000",
"alpha" : "1.000",
"blue" : "1.000",
"green" : "1.000"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "light"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.800",
"alpha" : "1.000",
"blue" : "0.800",
"green" : "0.800"
}
}
},
{
"idiom" : "universal",
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"red" : "0.100",
"alpha" : "1.000",
"blue" : "0.100",
"green" : "0.100"
}
}
}
]
}

View File

@ -6,13 +6,10 @@
// Copyright © 2020 David Wernhart. All rights reserved.
//
import SwiftUI
import ServiceManagement
import Foundation
import LaunchAtLogin
import Combine
import SwiftUI
struct BlueButtonStyle: ButtonStyle {
private struct BlueButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.foregroundColor(configuration.isPressed ? Color.blue : Color.white)
@ -22,7 +19,7 @@ struct BlueButtonStyle: ButtonStyle {
}
}
struct RedButtonStyle: ButtonStyle {
private struct RedButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.foregroundColor(configuration.isPressed ? Color.red : Color.white)
@ -32,224 +29,199 @@ struct RedButtonStyle: ButtonStyle {
}
}
var osxScale:Bool = true
private var osxScale = true
struct settings<Content: View>: View {
var content: () -> Content
@State var launchOnLogin = LaunchAtLogin.isEnabled
@State var osxScaleToggle = osxScale
@ObservedObject var presenter = SMCPresenter.shared
init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content
//Helper.instance.delegate = self
}
private struct Settings: View {
@State private var launchAtLogin = LaunchAtLogin.isEnabled
@State private var osxScaleToggle = osxScale
@ObservedObject private var presenter = SMCPresenter.shared
var body: some View {
VStack(alignment: .center){
HStack(alignment: .center) {
Toggle(isOn: Binding(
get: {
self.launchOnLogin
},
set: {(newValue) in
self.launchOnLogin = newValue
if(newValue){
print("launch on login turned on!")
LaunchAtLogin.isEnabled = true
VStack {
HStack {
VStack(alignment: .leading) {
Toggle(isOn: Binding(
get: { launchAtLogin },
set: { newValue in
launchAtLogin = newValue
print("Launch at login turned \(newValue ? "on" : "off")!")
LaunchAtLogin.isEnabled = newValue
}
else{
print("launch on login turned off!")
LaunchAtLogin.isEnabled = false
)) {
Text("Launch at login")
}
Toggle(isOn: Binding(
get: { osxScaleToggle },
set: { newValue in
osxScaleToggle = newValue
osxScale = newValue
presenter.setValue(value: Float(presenter.value))
}
)) {
Text("Use macOS battery scale")
}
)) {
Text("Launch on Login")
}.padding()
Toggle(isOn: Binding(
get: {
self.osxScaleToggle
},
set: {(newValue) in
self.osxScaleToggle = newValue
osxScale = newValue
self.presenter.setValue(value: Float(self.presenter.value))
}
)) {
Text("Use macOS battery scale")
}.padding()
Spacer()
Button( action: {
Helper.instance.installHelper()
}
) {
Button(action: {
Helper.instance.installHelper()
}) {
Text("Reinstall Helper")
.frame(maxWidth: 120, maxHeight: 30)
}.buttonStyle(BlueButtonStyle())
}
HStack(alignment: .center) {
HStack {
Spacer()
VStack(alignment: .leading){
Text("AlDente 1.2 🍝").font(.subheadline)
Button(action:{
let url = URL(string: "https://www.github.com/davidwernhart/AlDente")!
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
}){
Text("github.com/davidwernhart/AlDente").foregroundColor(Color.blue)
VStack(alignment: .leading) {
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
Text("AlDente \(version ?? "") 🍝").font(.headline)
let address = "github.com/davidwernhart/AlDente"
Button(action: {
openURL("https://" + address)
}) {
Text(address).foregroundColor(Color(.linkColor))
}.buttonStyle(PlainButtonStyle())
Text("Created with 🤍 by David Wernhart in 2020")
Text("Created with by David Wernhart in 2020")
// Text("AlDente 🍝").font(.title)
// Text("Keep your battery just right").font(.subheadline)
}
Spacer()
Button(action: {
let url = URL(string: "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6PLR7D9ZCZGGC&source=url")!
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
openURL("https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6PLR7D9ZCZGGC&source=url")
}) {
Text("Donate")
.frame(maxWidth: 60, maxHeight: 50)
}
.buttonStyle(BlueButtonStyle())
}
}.background(Color("SettingsColor")).cornerRadius(5)
}
.background(Color(.unemphasizedSelectedContentBackgroundColor))
.cornerRadius(5)
}
private func openURL(_ string: String) {
let url = URL(string: string)!
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
}
}
struct ContentView: View{
@State var adaptableHeight = CGFloat(100)
@State var showSettings = false
@ObservedObject var presenter = SMCPresenter.shared
struct ContentView: View {
@State private var adaptableHeight = CGFloat(100)
@State private var showSettings = false
@ObservedObject private var presenter = SMCPresenter.shared
init() {
Helper.instance.delegate = presenter
Helper.instance.readMaxBatteryCharge()
}
var body: some View {
let vstack = VStack{
VStack(alignment: .leading) {
//Text("Value is: \(presenter.value)")
HStack(alignment: .center){
Text(" Max. Battery Charge:").padding(.leading)
TextField("Number", value: Binding(
get: {
Float(self.presenter.value)
},
set: {(newValue) in
if(newValue >= 20 && newValue <= 100){
self.presenter.setValue(value: newValue)
}
}
), formatter: NumberFormatter())
.multilineTextAlignment(.center)
.frame(maxWidth:50)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
Button( action: {
self.showSettings = !self.showSettings
if(self.showSettings){
self.adaptableHeight = 235
var body: some View {
VStack(alignment: .leading) {
HStack {
Text("Max. Battery Charge:").padding(.leading)
TextField("Number", value: Binding(
get: {
Float(presenter.value)
},
set: { newValue in
if newValue >= 20 && newValue <= 100 {
presenter.setValue(value: newValue)
}
else{
self.adaptableHeight = 100
}
}) {
Text("Settings")
.frame(maxWidth: 70, maxHeight: 30)
}.buttonStyle(BlueButtonStyle()).padding(.leading,-30)
Button( action: {
NSApplication.shared.terminate(self)
}) {
Text("Exit")
.frame(maxWidth: 50, maxHeight: 30)
}.buttonStyle(RedButtonStyle()).padding(.leading,-30)
}
HStack(alignment: .center){
Slider(value: Binding(
get: {
Float(self.presenter.value)
},
set: {(newValue) in
if(newValue >= 20 && newValue <= 100){
self.presenter.setValue(value: newValue)
}
}
), in: 20...100).padding(.horizontal).padding(.top,-20)
}
}
), formatter: NumberFormatter())
.multilineTextAlignment(.center)
.frame(maxWidth: 50)
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
if(self.showSettings){
//settings{Text("")}
settings({Text("")})
Button(action: {
showSettings.toggle()
adaptableHeight = showSettings ? 235 : 100
}) {
Text("Settings")
.frame(maxWidth: 70, maxHeight: 30)
}.buttonStyle(BlueButtonStyle()).padding(.leading, -30)
Button(action: {
NSApplication.shared.terminate(self)
}) {
Text("Quit")
.frame(maxWidth: 50, maxHeight: 30)
}.buttonStyle(RedButtonStyle()).padding(.leading, -30)
}
Slider(value: Binding(
get: {
Float(presenter.value)
},
set: { newValue in
if newValue >= 20 && newValue <= 100 {
presenter.setValue(value: newValue)
}
}
}.frame(width: 400, height: adaptableHeight)
}
return vstack
), in: 20...100).padding(.horizontal).padding(.top, -20)
Spacer()
if showSettings {
Settings()
}
}.frame(width: 400, height: adaptableHeight)
}
}
class SMCPresenter: ObservableObject, HelperDelegate{
private final class SMCPresenter: ObservableObject, HelperDelegate {
static let shared = SMCPresenter()
@Published var value: UInt8 = 0
private var timer: Timer?
func OnMaxBatRead(value: UInt8){
func OnMaxBatRead(value: UInt8) {
DispatchQueue.main.async {
if(osxScale){
if osxScale {
self.value = value + 3
}
else{
} else {
self.value = value
}
}
}
func setValue(value: Float){
func setValue(value: Float) {
DispatchQueue.main.async {
self.value = UInt8(value)
}
self.timer?.invalidate()
timer?.invalidate()
self.timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: { timer in
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { _ in
var setval = value
if(osxScale){
if osxScale {
setval -= 3
}
if(setval >= 20 && setval <= 100){
print("Setting Max Battery To: ",setval)
if setval >= 20 && setval <= 100 {
print("Setting Max Battery To: ", setval)
Helper.instance.writeMaxBatteryCharge(setVal: UInt8(setval))
Helper.instance.readMaxBatteryCharge()
self.timer = nil
}
})
}
}
}