Hi,
I can't explain why this happens. But as soon as I put a "1" via the "countTime" variable in the text control, it dissapears. But only if I add a shadow to the text. Without .shadow, the "1" is shown. Here is the code section:
VStack {
Text("\(Int(self.countTime))")
.bold().font(.system(size: 80, design: .rounded))
.foregroundColor(palette_mint_dark)
.padding(-19)
.shadow(radius: 2)
Text("minutes left")
.fontWeight(.light)
Text("\(Int(self.countTime))")
.fontWeight(.light)
.foregroundColor(.gray)
.padding(1)
}
Please find here the full code:
//
// ContentView.swift
// Nuvora
//
// Created by Pohl, Kay on 23.12.21.
//
import SwiftUI
import UserNotifications
//########################################################################
//Landing Page ###########################################################
//########################################################################
struct ContentView: View {
var body: some View {
NavigationView{
ZStack{
VStack(spacing: 0.0) {
Header() //Header View
Spacer ()
Main() //Main View
Spacer()
Spacer()
Bottom() //Bottom View
}
}
.navigationTitle("")
.navigationBarHidden(true)
}
}
}
//########################################################################
//Extensions #############################################################
//########################################################################
//Color extension
extension Color {
init(_ hex: UInt, alpha: Double = 1) {
self.init(
.sRGB,
red: Double((hex >> 16) & 0xFF) / 255,
green: Double((hex >> 8) & 0xFF) / 255,
blue: Double(hex & 0xFF) / 255,
opacity: alpha
)
}
}
//########################################################################
//Classes ################################################################
//########################################################################
class NotificationManger {
static let instance = NotificationManger()
func requestAuthorization(){
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
UNUserNotificationCenter.current().requestAuthorization(options: options) { success, error in
if let error = error {
print("ERROR: \(error)")
} else {
print("Success")
}
}
}
func scheduleNotification(param: Int = 0) {
let content = UNMutableNotificationContent()
content.title = "Nuvora Notification"
content.subtitle = "It is time to go home!"
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(param) * 60, repeats: false)
let request = UNNotificationRequest(identifier: "MSG", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
//########################################################################
//Previews ###############################################################
//########################################################################
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
// Created by Kay Pohl
// 24.12.2021
//########################################################################
//Header View ############################################################
//########################################################################
struct Header: View {
let pallette_mint = Color(0x219EBC)
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Text("Nuvora")
.bold()
.foregroundColor(.white)
.font(.system(size:45, design: .rounded))
Spacer()
NavigationLink(destination: Settings()){
Image(systemName: "gearshape.fill")
.resizable()
.frame(width: 28.0, height: 28.0)
.foregroundColor(.white)
}
}
Text("Take care of your working time")
.font(.subheadline)
.fontWeight(.light)
.bold()
.foregroundColor(.black)
}
//.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(pallette_mint)
.shadow(radius: 20)
}
}
//########################################################################
//Main View ##############################################################
//########################################################################
struct Main: View {
//Timer settings
@State var start = false
@State var startTime : CGFloat = 1
@State var countTime = 0
@State var seccount = 0
@State var time = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect()
//Color assign
let pallette_orange_light = Color(0xFFB703)
let palette_mint_light = Color(0x8ecae6)
let palette_mint_dark = Color(0x023047)
let palette_orange_dark = Color(0xfb8500)
let pallette_mint = Color(0x219EBC)
var body: some View {
//Configure Circle gradient
let gradient = Gradient(colors: [palette_mint_light, pallette_mint])
let linearGradient = LinearGradient(gradient: gradient, startPoint: .top, endPoint: .bottom)
let style = StrokeStyle(lineWidth: 15,
lineCap: .round,
lineJoin: .round,
miterLimit: 1,
dash: [],
dashPhase: 0)
//Circle and Time
VStack {
ZStack{
Circle()
.stroke(Color.gray.opacity(0.08), style: StrokeStyle(lineWidth: 20, lineCap: .round))
.frame(width: 320, height: 320)
Circle()
.trim(from: 0, to: 1 - ((startTime - CGFloat(countTime)) / startTime) )
.stroke(linearGradient, style: style)
.rotationEffect(.degrees(-90))
.frame(width: 320, height: 320)
.animation(Animation.linear(duration: 1), value: 1)
.shadow(radius: 5)
VStack {
Text("\(Int(self.countTime))")
.bold().font(.system(size: 80, design: .rounded))
.foregroundColor(palette_mint_dark)
.padding(-19)
.shadow(radius: 2)
Text("minutes left")
.fontWeight(.light)
Text("\(Int(self.countTime))")
.fontWeight(.light)
.foregroundColor(.gray)
.padding(1)
}
}.padding(40)
//Buttons
HStack {
//Play & Pause
Button(action: {
if self.countTime == 0{
self.countTime = Int(self.startTime)
}
if self.start == false{
print("Start notification Manager")
NotificationManger.instance.scheduleNotification(param: self.countTime)
}
if self.start == true{
print("Stop notification Manager")
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
self.seccount = 0
}
self.start.toggle()
}) {
HStack(spacing: 20){
Image(systemName: self.start ? "pause.fill" : "play.fill")
.foregroundColor(.white)
Text(self.start ? "Pause" : "Start")
.foregroundColor(.white)
}
.padding(.vertical)
.frame(width: (UIScreen.main.bounds.width / 2) - 55)
.background(pallette_orange_light)
.clipShape(Capsule())
.shadow(radius: 6)
}
Spacer()
//Restart
Button(action: {
//self.countTime = self.startTime
self.start = false
self.countTime = Int(self.startTime)
self.seccount = 0
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}) {
HStack(spacing: 20){
Image(systemName: "arrow.clockwise")
.foregroundColor(pallette_orange_light)
Text("Restart")
.foregroundColor(pallette_orange_light)
}
.padding(.vertical)
.frame(width: (UIScreen.main.bounds.width / 2) - 55)
.background(
Capsule()
.stroke(pallette_orange_light, lineWidth: 2)
)
.shadow(radius: 6)
}
}
.padding(.horizontal, 40)
}// Ask for sending local notification
.onAppear(perform: {
UNUserNotificationCenter.current().requestAuthorization(options: [.badge,.sound,.alert]) { (_, _) in
}
countTime = Int(startTime)
})
//Fires every second
.onReceive(self.time) { (_) in
//if Timer is active
if self.start{
//if Timer is not finished
if self.countTime > 0{
//Count seconds until 60
self.seccount += 1
//print(self.seccount)
print(self.countTime)
//If seconds reached 60 count minutes
if self.seccount > 59{
self.countTime -= 1
self.seccount = 0
}
}
else{
self.start.toggle()
}
}
}
}
}
//########################################################################
//Bottom View ############################################################
//########################################################################
struct Bottom: View {
var body: some View {
let pallette_mint = Color(0x219EBC)
Text("Made with ")
.foregroundColor(.secondary)
+ Text(Image(systemName: "heart.fill"))
.foregroundColor(pallette_mint)
+ Text(" by myself")
.foregroundColor(.secondary)
}
}
I am using Xcode 13.2.1 and don't know how to fix that. Can someone please test it and try to reproduce it?
Thanks in advance
Kay