How do I change the root view with SwiftUI
You just display each view conditionally in a root view
var body: some View {
if isLoggedIn {
HomeView()
} else {
LoginView() {
}
}
Change view via ViewModel
Thanks to Gorky's respond I figured out how to do that.
In Fragment I created observer
sharedViewModel.changeView.observe(viewLifecycleOwner, Observer<Boolean> { hasFinished ->
if (hasFinished) nextFragment()
})
I created changeView variable in ViewModel. When
var changeView = MutableLiveData<Boolean>()
change to true, observer call function.
source:
https://developer.android.com/codelabs/kotlin-android-training-live-data#6
How to change view on midnight in WidgetKit, SwiftUI?
- Assuming you have an Entry (in your app you have
entry.state...
but for this example I used a simplified version):
struct SimpleEntry: TimelineEntry {
let date: Date
let lesson: Lesson
}
- Setup your TimelineProvider to refresh timeline after the next midnight:
struct SimpleProvider: TimelineProvider {
...
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let currentDate = Date()
let midnight = Calendar.current.startOfDay(for: currentDate)
let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
let entries = [
SimpleEntry(date: currentDate, lesson: Lesson()) // pass the lesson here
]
let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
completion(timeline)
}
}
In the TimelineProvider you may pass any lesson you want (depending on the day or the previous lesson - it's up to you). You may also pass a variable to an Entry indicating whether the lesson is completed.
By setting the .after(nextMidnight)
policy you indicate when do you want your Timeline (and therefore you Widget View) to be reloaded.
React How to change view after click on button
There are multiple ways to do this. You can set flags around the views. For example
render(){
return(
<div className="button-toolbar">
<button className="button" onClick={() => setState({view: 1})}>Button 1</button>
<button className="button" onClick={() => setState({view: 2})}>Button 2</button>
<button className="button">Button 3</button>
{this.state.view === 1 ? <View1></View1> : ''}
{this.state.view === 2 ? <View2></View2> : ''}
<View3></View3>
</div>
)
Changing views via conditional statements inside of async commands in SwiftUI IOS15
SwiftUI renders your user interface in response to changes in state. So instead of trying to render Dashboard
or ContentView
within your Task
, instead you need to set some form of state higher up that determines which view to show to the user.
For example, if you had a @State
variable recording your logged in status, your code would start to look like
struct ContentView: View {
@State var loggedIn = false
var body: some View {
if loggedIn {
Dashboard()
} else {
// login form
Button {
Task {
let logInStatus = try await network.LoginFunction(userName: username, passWord: password)
self.loggedIn = logInStatus
}
}
}
}
}
When your async
tasks changes the ContentView's state, SwiftUI's rendering subsystem will pick up that loggedIn
has changed, and re-render body
so that the login form is replaced by a call to Dashboard()
.
Related Topics
How to Split One Column into Two Columns in SQL Server
How to Remove Special Characters in Column With MySQL
Sql: How to Get Both Match and Non-Match Records
1052: Column 'Id' in Field List Is Ambiguous
Mysql - Operand Should Contain 1 Column(S)
How to Use Json_Extract in MySQL and Get a String Without the Quotes
Sql Query for Increase Item Value Price for Multiple Item
Why Postgresql Does Not Like Uppercase Table Names
Selecting a Single Record from the Same Multiple Rows from a Single Table
How to Enforce Case Sensitive Table and Column Names in MySQL
Trim Whitespaces (New Line and Tab Space) in a String in Oracle
Calculating Percent Change Between Two Rows
What Is and How to Remove Tablespace Error from My Database
State Wise Data of Country and City
Mysql Error 1264: Out of Range Value for Column
List All Employee'S Names and Their Managers by Manager Name Using an Inner Join