what's an efficient way in swift to get a "change of day" event [closed] - swift

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
so I've a swift-ui app where I show information for the current day - so at midnight I want it to switch to a new day - trouble is I want this to be as "light" as possible - eg I don't want anything consuming any resources as all I want to do is refresh the view, if it's displayed.
How do I do this without consuming background resources when the view isn't shown?

you could try something like this:
(note I did not wait to see if this works)
import SwiftUI
import Combine
#if os(iOS)
import UIKit
#endif
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Text("day change")
.onReceive(NotificationCenter.default.publisher(
for: UIApplication.significantTimeChangeNotification)) { _ in
print("----> day time has changed <----\n")
print("A notification that posts when there is a significant change in time, \n for example, change to a new day (midnight), \n carrier time update, and change to or from daylight savings time.")
}
}
}

Related

Does anyone know what this stack is called SwfitUI? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 months ago.
Improve this question
I'll keep it quick: Does anyone know if SwiftUI have a built in method that renders something like this image:
where the view just changes based on what label you tap? I wonder if it's possible to achieve this using some sort of navigation view or stack? I'd appreciate any input! Thanks.
EDIT:
HStack {
Picker(selection: $selected, label: Text("Mode"), content:{
Text("Projects").tag(1)
Text("Notes").tag(2)
Text("Docus").tag(3)
}).pickerStyle(SegmentedPickerStyle())
.frame(width: 200, height: 10)
.padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: -0))
if selected == 1 {
// how would i show another view if the user selects option 1?
}
}
This is a picker. to be precise, this is a segmented picker.
You can create it like so:
struct ContentView: View {
#State private var favoriteColor = 0
var body: some View {
Picker("What is your favorite color?", selection: $favoriteColor) {
Text("Red").tag(0)
Text("Green").tag(1)
Text("Blue").tag(2)
}
.pickerStyle(.segmented)
}
}
When we create the picker we pass in a binding (when we change the picker's value it will know to switch to it) this is the thing with the dollar sign ($)
The next thing is to add the segments.
So we add text views with a tag attached to each one.
Lastly we need to set the picker style (in this case the segmented)
.pickerStyle(.segmented)
I suggest you to look here: Create a segmented control and read values from it
Hope this helps!

How to persist state from a class into a separate SwiftUI view? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am having problems persisting a class's state into a SwiftUI struct's view.
I have a class that acts as a controller, defined in one file, and a SwiftUI view that is supposed to change according to properties in that controller.
I've defined these files as such:
ClockController.swift
class ClockController:ObservableObject {
#Binding var isAM:Bool
init(){
self.isAM = false
}
func toggleAMPM(){
self.isAM = !self.isAM
}
}
and TestUI.swift
struct TestUI:View{
#ObservedObject var clockController:ClockController = ClockController()
var body: some View {
Button(action: {
self.clockController.toggleAMPM()
}){
Text("Toggle")
}
Text(self.clockController.isAM ? "AM" : "PM")
}
}
I want the TestUI to change/re-render every time the self.clockController.isAM variable changes (when the toggle button is pressed), which is why I have made ClockController an ObservableObject and added the #Binding keyword to the isAM property. However, I keep getting the following errors with this setup on ClockController's initializer method:
'self' used in property access 'isAM' before all stored properties are initialized and Return from initializer without initializing all stored properties
How can I get my TestUI to bind on ClockController's isAM variable?
all! I've fixed the problem myself. I needed to remove the #Binding keyword from ClockController's isAM property and change it to #Published. That did the trick.
Kudo's to YouTube and this video: https://youtu.be/-yjKAb0Pj60?t=919

Unable to use ForEach loop on #Binding variable [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I recently asked a question however it was removed as they thought the answer existed already and so I checked out this question. I have tried implementing the solution found here into my code and now run into a slightly different error.
Basically I am trying to create a simple application with a list of items that can be clicked on to navigate to a detail view with an edit feature. The data is read from a JSON file which is why the data is in a binding so it can be updated and this is not a problem anywhere else in the project.
import SwiftUI
struct EateryList: View {
#Binding var eateries: [Eatery]
var body: some View {
NavigationView {
VStack {
List {
ForEach(eateries.indicies, id: \.self) { i in
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) { //errors appear here
EateryRow(eatery: $eateries[i])
}
}
}
.navigationTitle("Favourite Eateries")
.navigationBarItems(leading: EditButton(), trailing: Button( action: add)
{
Image(systemName: "plus")
}
)
.listStyle(InsetGroupedListStyle())
}
}
}
func add() {
eateries.append(Eatery(name: "Eatery", location: "Insert location here", notes: "Insert notes here", reviews: ["Insert reviews here"], url: "https://i.imgur.com/y3MMnba.png"))
}
}
I get 3 errors with this code currently which I do not fully understand...
Generic struct 'ForEach' requires that 'Binding' conform to 'RandomAccessCollection'
Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'Binding<[Eatery]>'
Value of type '[Eatery]' has no dynamic member 'indicies' using key path from root type '[Eatery]'
All help is greatly appreciated right now and if you need further clarification of other parts of the project please let me know :)
It's a spelling mistake. It's indices not indicies
ForEach(eateries.indices, id: \.self) { i in //<-- Here
NavigationLink(destination: EateryDetail(eatery: $eateries[i])) {
EateryRow(eatery: $eateries[i])
}
}

Flutter : Voice command to give user weather and date time update [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I want to create an application that work on voice command. In this application there is two options when user ask for weather or time update the application get the current weather and give update about weather in voice and the same functionality for time and date. Please help me how I can achieve this in flutter.
You can either use :
https://alan.app/docs/tutorials/flutter/integrating-flutter
or use:
https://pub.dev/packages/speech_to_text
import 'package:speech_to_text/speech_to_text.dart' as stt;
stt.SpeechToText speech = stt.SpeechToText();
bool available = await speech.initialize( onStatus: statusListener, onError: errorListener );
if ( available ) {
speech.listen( onResult: resultListener );
}
else {
print("The user has denied the use of speech recognition.");
}
// some time later...
speech.stop()
Then treat specific words as commands:
void resultListener(SpeechRecognitionResult result) {
++resultListened;
print('Result listener $resultListened');
setState(() {
lastWords = '${result.recognizedWords} - ${result.finalResult}';
});
}

button click counter blackberry cascades [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to make an app that count how many times you've clicked the button in blackberry 10
so i'm trying to int a value that will increase by 1 when you click the button
import bb.cascades 1.2
Page {
Container {
ImageButton {
defaultImageSource: "asset:///item_XS_6405840_3762396.jpg"
onClicked: {
count.setText(count + 1)
}
onTouch: {
count.setText(count + 1)
}
}
Label {
text: "you've clicked:"
}
Label {
id: count
text: 0
}
Button {
id: reset
text: "reset"
onClicked: {
count = 0;
}
}
}
}
I only have knowledge in java. thanks for helping