|
Hi all, i'm new here. And also in swift. I don't want to become a dev, nor learn swift in deep because i'm not planning to make another app than the one i'm working on. Starting with this, i'm sure i've lost the most of you... I got an M2 Apple Sillicon last year, and the notch is really cool as it is, bringing more spaces to the screen, but it need some fun! I've decided maybe 3 months ago to make a simple app that show battery level around the notch, displaying color gradually the more the battery level decrease. Was a pain for someone like me... But it's there, it work. I can leave it as is. But i'm a perfectionniste, and i want to made some changes and improvements. I would appreciate if i could find any help here. I've started with an app called TheNotch on github. With help of AI, i've made the color animation, battery level and powersource monitor. But after about a month of searches, i couldn't find my way. There's many things i couldn't achieve alone, my AI assisstant didn't help me in these cases... make me think artificial intelligence is not so intelligent than that... :) First of all, i know my code could/should be cleaner... Be lenient please. But you can help improving it!! :) 1) Trying to be clear, i have stroke of RoundedRectangle that animate at start with progress, representing the battery level. A second stroke with progress too behind representing the background, which make the notch more visible (color.white.opacity(0.2)). The animation works well, start from 0 to 0.5 as it represent the half of the RoundedRectangle. What i want is the animation go back to 0 when i press the button that close the app, then close the app. The button is in another swift file (ConfigView). But when i click on the close app button, nothing. Just a delay (DispatchQueue). AI said it's because it didn't act on the same instance of progress... hum....?! I'm not familiar with that, i don't know what to do now. The answers AI gave me doesn't work, or at least i couldn't make them works in my project. He don't understand "the button is on another swift file"... Here is my code:
The button is in the prefpane, in another swift file. 2) Not related to the first one. Powersource monitor detect if i'm plugged in or not. All i want here is a small pill like on ios or dynamicIsland (roundedrectangle with high radius), dropping from the notch, that show a simple text saying "plugged in", but it's hidden/don't work and i don't know why... 3) Maybe related to the second. Something is wrong with the code (the same on the original) i think as i couldn't expand the NotchView on onHover for example to its both sides, it expand only from right side (trailing? but leading in this case)... I'm pretty sure it comes from here: let pos = CGPoint(x: screenWidth/2 - notchWidth/2, y: screenHeight - 42) self.setFrameOrigin(pos) Here's the code:
Thanks for reading. Thanks for all, even if no help!! |
|
Hi, is there something wrong with my questions? Is there too many questions at same time? I've tryed this for the first question, it keep asking me for argument: "Missing argument for parameter 'progresss' in call" in my ConfigView_Previews but i don't know what to put... All my attempts returns me errors or don't work (no error but no animation). Can anyone tell me if i'm doing it well? I put this on my ConfigView file, which contains the button: struct ConfigView: View { @ObservedObject var progress: UserProgress
} this on top of my NotchView file, which contains BatteryView struct and the progress i want to animate from the button: class UserProgress: ObservableObject { @Published var score: Double = 0.0 } and inside BatteryView():
edited every "progress" in trim and animation to "progress.score". It then ask for argument in call... I'm missing something? Can you tell me what to put inside the parentheses?? I've tryed "progress: UserProgress()", no error but no anim on click... only on start. Please just tell me if i'm doing it well. Thanks. |
|
In a few other posts, I offered advice to break a big problem into many smaller problems to solve one at a time. This concept is taught in some engineering courses as "How to Eat an Elephant" See -> How to Eat an Elephant Try this. Create a new project. Remove all your color coding, spacing, and visual effects. Architect your solution so that each functional requirement is solved in its own Swift class or struct. Solve one element at a time. Don't work on others until you've solved the current one. For example, these are two different bites of your elephant:
You could have ONE struct that represents the battery. What is its maxium value? What is its current value. Calculate the percentage of remaining battery life. Calculate percentage when user should be alerted. Calculate battery drain per minute. These are all properties that you want to know about your battery. All values. No visuals. SOLVE THIS PROBLEM on its own. Later, you want to display color coding to represent these values. Develop a BatteryView object that is independent of the Battery struct. Your BatteryView should only ask the Battery for its values. The BatteryView should NOT do any calculations. Leave those rules in the Battery object. When you finish these two bites, then figure out your next bite at the elephant.
Yes! Keep coding |
|
Sorry for the late reply. Thanks for yours @Obelix (may be you're french...?!). I just want add that english is not my native language.... I'm french. I understand, too many questions... Sorry for my dumbness... With your wisdom Obé, i got the second question to work. I started from zero as you said. There's still a "mask" that hide everything is outside and i don't know why... But i have just enough space to display a "pill".
I'm not sure to understand well. If i create a Battery struct and add it to NotchView as Battery() in a ZStack, i cannot create a pill, this is where a mask hides everything is outside the notch! I don't need calculations. A simple if getLevel() < 20 {... should be good. Now, on hover, it display a pill that show battery percentage. That's it, it's enough for me. I've followed your tips by getting calculations out of the NotchView like this, is this good?:
The last one is in startTimer(), it still has Back to my first and last remaining question. I couldn't get the animation to work. Even if i add ConfigView() to NotchView(), call of progress from the same file doesn't work... In ConfigView(), i've tryed Thanks. |
|
I have not tested this and am away from my setup. But this is off the top of my head.... Create a Battery object and put ALL BATTERY logic here. This is ONE bite of the elephant. What do you need to know about a battery in your app? Do not think about how you view it. Focus on what you need. Then use this object in a view as needed.
This answer doesn't cover your Maybe you don't want this as a Keep coding! |
|
Also, please edit your post an put all code inside the markup code structure. This makes your code stand out from your questions and comments.
|
|
Thanks. Is this a good start?
I have some "property initializers run before 'self' is available" when i put startTimer() function inside Battery() class... But i think we're doing approximately the same things here... (Just stoled some var from your message on the way...) What is UUID for?? What's the necessity to make it unique? |
|
Sorry... but why do i have to make calculations like max value and drain rate since i don't need them? I just want it to display the percentage. Which is done by getLevel(). Is this not good? not enough? or not the good way? It work as is. And we're far from my first question, so what's the link between them? I'm not a dev as i said, so please don't ask me so much, please... Tell me exactly what's wrong and what to change in my code. For me, asked changes are already made... Your post make me think there's still something wrong, with me, and with my code... |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.