Passing parameter value to Observable object - swift

I have an ObservableObject class used to fetch data from an api. It takes one parameter which is the api key. I am trying to pass that key from a parameter of my ContentView to the object.
class UnsplashAPI: ObservableObject {
//some code
var clientId: String
init(clientId: String) {
self.clientId = clientId
}
//some more code
}
This works fine when I'm asking for a parameter in my struct
struct ContentView: View {
#ObservedObject var api = UnsplashAPI(clientId: "APIKEY")
var body: some View {
//View
}
}
However this doesn't:
struct ContentView: View {
var clientId: String
#ObservedObject var api = UnsplashAPI(clientId: self.clientId) // Cannot find 'self' in scope
init(clientId: String){
self.clientId = clientId
}
var body: some View {
//View
}
}
I think i'm initialising the struct wrong as I am getting the error "Cannot find 'self' in scope"

Initialize it inside init
struct ContentView: View {
var clientId: String
#ObservedObject var api: UnsplashAPI
init(clientId: String){
self.clientId = clientId
self.api = UnsplashAPI(clientId: clientId) // << here !!
}
// ...

Related

How to observer a property in swift ui

How to observe property value in SwiftUI.
I know some basic publisher and observer patterns. But here is a scenario i am not able to implement.
class ScanedDevice: NSObject, Identifiable {
//some variables
var currentStatusText: String = "Pending"
}
here CurrentStatusText is changed by some other callback method that update the status.
Here there is Model class i am using
class SampleModel: ObservableObject{
#Published var devicesToUpdated : [ScanedDevice] = []
}
swiftui component:
struct ReviewView: View {
#ObservedObject var model: SampleModel
var body: some View {
ForEach(model.devicesToUpdated){ device in
Text(device.currentStatusText)
}
}
}
Here in UI I want to see the real-time status
I tried using publisher inside ScanDevice class but sure can to use it in 2 layer
You can observe your class ScanedDevice, however you need to manually use a objectWillChange.send(),
to action the observable change, as shown in this example code.
class ScanedDevice: NSObject, Identifiable {
var name: String = "some name"
var currentStatusText: String = "Pending"
init(name: String) {
self.name = name
}
}
class SampleViewModel: ObservableObject{
#Published var devicesToUpdated: [ScanedDevice] = []
}
struct ReviewView: View {
#ObservedObject var viewmodel: SampleViewModel
var body: some View {
VStack (spacing: 33) {
ForEach(viewmodel.devicesToUpdated){ device in
HStack {
Text(device.name)
Text(device.currentStatusText).foregroundColor(.red)
}
Button("Change \(device.name)") {
viewmodel.objectWillChange.send() // <--- here
device.currentStatusText = UUID().uuidString
}.buttonStyle(.bordered)
}
}
}
}
struct ContentView: View {
#StateObject var viewmodel = SampleViewModel()
var body: some View {
ReviewView(viewmodel: viewmodel)
.onAppear {
viewmodel.devicesToUpdated = [ScanedDevice(name: "device-1"), ScanedDevice(name: "device-2")]
}
}
}

SwiftUI how to use enviromentObject in viewModel init?

I have a base API:
class API: ObservableObject {
#Published private(set) var isAccessTokenValid = false
#AppStorage("AccessToken") var accessToken: String = ""
#AppStorage("RefreshToken") var refreshToken: String = ""
func request1() {}
func request2() {}
}
And it was passed to all views by using .environmentObject(API()). So in any views can easily access the API to do the http request calls.
Also I have a view model to fetch some data on the view appears:
class ViewModel: ObservableObject {
#Published var data: [SomeResponseType]
init() {
// do the request and then init data using the response
}
}
struct ViewA: View {
#StateObject private var model = ViewModel()
var body: View {
VStack {
model.data...
}
}
}
But in the init(), the API is not accessable in the ViewModel.
So, to solve this problem, I found 3 solutions:
Solution 1: Change API to Singleton:
class API: ObservableObject {
static let shared = API()
...
}
Also we should change the enviromentObject(API()) to enviromentObject(API.shared).
So in the ViewModel, it can use API.shared directly.
Solution 2: Call the request on the onAppear/task
struct ViewA: View {
#EnvironmentObject var api: API
#State private var data: [SomeResponseType] = []
var body: View {
VStack {}
.task {
let r = try? await api.request1()
if let d = r {
data = d
}
}
}
}
Solution 3: Setup the API to the ViewModel onAppear/task
class ViewModel: ObservableObject {
#Published var data: [SomeResponseType]
var api: API?
setup(api: API) { self.api = api }
requestCall() { self.api?.reqeust1() }
}
struct ViewA: View {
#EnvironmentObject var api: API
#StateObject private var model = ViewModel()
var body: View {
VStack {}
.onAppear {
model.setup(api)
model.requestCall()
}
}
}
Even though, I still think they are not a SwiftUI way. And my questions is a little XY problem. Probably, the root question is how to refactor my API. But I am new to SwiftUI.
Solution 2 is best. You can also try/catch the exception and set an #State for an error message.
Try to avoid using UIKit style view model objects because in SwiftUI the View data struct plus #State already fulfils that role. You only need #StateObject when you need a reference type for view data which is not very often given now we have .task.

How to pass Binding variable to an ObservableObject?

I have a binding variable that I need to pass to an ObservableObject in swiftUI.
Let's say I have this code:
struct MYView: View {
#ObservedObject var fetch = Fetch()
#Binding var IDTxt: Int
var body: some View{
//some code here
}
}
Now I want to pass the IDTxt value to this:
public class Fetch: ObservableObject {
//I need to pass the Binding variable here. something like this?
#Binding var IDTxt: Int
init(){
load()
}
func load() {
//So I can use it here like this:
let url = URL(string: "http://someurl.com/\(IDTxt)")
}
Could someone please advice on the above?
You do not need to pass the Binding value. But you can pass direct value.
public class Fetch: ObservableObject {
var IDTxt: Int
init(id: Int){
self.IDTxt = id
load()
}
func load() {
//So I can use it here like this:
let url = URL(string: "http://someurl.com/\(IDTxt)")
}
}
struct MYView: View {
#ObservedObject var fetch: Fetch
#Binding var IDTxt: Int
init(IDTxt: Binding<Int>) {
self._IDTxt = IDTxt
self.fetch = Fetch(id: IDTxt.wrappedValue)
}
var body: some View{
//some code here
Color.red
}
}
If you want to observe IDTxt text then use #Published in class.
public class Fetch: ObservableObject {
#Published var IDTxt: Int
There's no need for Bindings if the property you are trying to inject into Fetch is coming from a parent view. You can simply inject the value in the init.
Also, if you are creating an ObservableObject inside a View, you need to declare it as #StateObject. #ObservedObject should only be used when injecting the object into the view.
public class Fetch: ObservableObject {
init(id: Int) {
load(id: id)
}
func load(id: Int) {
let url = URL(string: "http://someurl.com/\(id)")
}
struct MYView: View {
#StateObject private var fetch: Fetch
init(id: Int) {
self._fetch = StateObject(wrappedValue: Fetch(id: id))
}
var body: some View{
EmptyView()
}
}

Init for a SwiftUI class with a #Binding var

I have a class which I want to initialize with a Binding var that is set in another View.
View ->
struct CoverPageView: View {
#State var numberOfNumbers:Int
var body: some View {
NavigationView {
GeometryReader { geometry in
VStack(alignment: .center, spacing: 0){
TextField("Multiplication Upto:", value: self.$numberOfNumbers, formatter: NumberFormatter())
}
}
}
}
CLASS WHICH NEEDS TO BE INITIALIZED USING THE #Binding var $numberofNumbers -
import SwiftUI
class MultiplicationPractice:ObservableObject {
#Binding var numberOfNumbers:Int
var classNumofNumbers:Int
init() {
self.classNumofNumbers = self.$numberOfNumbers
}
}
The init statement obviously gives the error that self is not initialized and the instance var is being used to initialize which is not allowed.
How do I circumvent this? The class needs to be initialized with the number the user enters on the first view. I have written approx. code here so ignore any typos please.
Typically you'd initialize MultiplicationPractice in CoverPageView with a starting value:
#ObservedObject var someVar = MultiplicationPractice(NoN:123)
And of course, add a supporting init statement:
class MultiplicationPractice:ObservableObject {
init(NoN: Int) {
self.numberOfNumbers = val
}
and you wouldn't want to wrap your var with #Binding, instead wrap it with #Published:
class MultiplicationPractice:ObservableObject {
#Published var numberOfNumbers:Int
...
In your particular case I would even drop the numberOfNumbers var in your CoverPageView, and instead use the direct variable of the above someVar:
struct CoverPageView: View {
//removed #State var numberOfNumbers:Int
#ObservedObject var someVar = MultiplicationPractice(123)
...
TextField("Multiplication Upto:", value: self.$someVar.numberOfNumbers, formatter: NumberFormatter())
You'll notice that I passed in the sub-var of the #ObservedObject as a binding. We can do this with ObservableObjects.
Edit
I see now what you're trying to do, you want to pass a binding along across your ViewModel, and establish an indirect connection between your view and model. While this may not be the way I'd personally do it, I can still provide a working example.
Here is a simple example using your struct names:
struct MultiplicationGame {
#Binding var maxNumber:String
init(maxNumber: Binding<String>) {
self._maxNumber = maxNumber
print(self.maxNumber)
}
}
class MultiplicationPractice:ObservableObject {
var numberOfNumbers: Binding<String>
#Published var MulGame:MultiplicationGame
init(numberOfNumbers: Binding<String> ) {
self.numberOfNumbers = numberOfNumbers
self.MulGame = MultiplicationGame(maxNumber: numberOfNumbers)
}
}
struct ContentView: View {
#State var someText: String
#ObservedObject var mulPractice: MultiplicationPractice
init() {
let state = State(initialValue: "")
self._someText = state
self.mulPractice = MultiplicationPractice(numberOfNumbers: state.projectedValue)
}
var body: some View {
TextField("put your text here", text: $someText)
}
}
Okay, I don't really understand your question so I'm just going to list a few examples and hopefully one of them will be what you're looking for.
struct SuperView: some View {
#State var value: Int = 0
var body: some View {
SubView(value: self.$value)
}
}
struct SubView: View {
#Binding var value: Int
// This is the same as the compiler-generated memberwise initializer
init(value: Binding<Int>) {
self._value = value
}
var body: some View {
Text("\(value)")
}
}
If I misunderstood and you're just trying to get the current value, do this
struct SuperView: some View {
#State var value: Int = 0
var body: some View {
SubView(value: self.value)
}
}
struct SubView: View {
let value: Int
// This is the same as the compiler-generated memberwise initializer
init(value: Int) {
self.value = value
}
var body: some View {
Text("\(value)")
}
}

How to pass a value from an EnvironmentObject to a class instance in SwiftUI?

I'm trying to assign the value from an EnvironmentObject called userSettings to a class instance called categoryData, I get an error when trying to assign the value to the class here ObserverCategory(userID: self.userSettings.id)
Error says:
Cannot use instance member 'userSettings' within property initializer; property initializers run before 'self' is available
Here's my code:
This is my class for the environment object:
//user settings
final class UserSettings: ObservableObject {
#Published var name : String = String()
#Published var id : String = "12345"
}
And next is the code where I'm trying to assign its values:
//user settings
#EnvironmentObject var userSettings: UserSettings
//instance of observer object
#ObservedObject var categoryData = ObserverCategory(userID: userSettings.id)
class ObserverCategory : ObservableObject {
let userID : String
init(userID: String) {
let db = Firestore.firestore().collection("users/\(userID)/categories") //
db.addSnapshotListener { (snap, err) in
if err != nil {
print((err?.localizedDescription)!)
return
}
for doc in snap!.documentChanges {
//code
}
}
}
}
Can somebody guide me to solve this error?
Thanks
Because the #EnvironmentObject and #ObservedObject are initializing at the same time. So you cant use one of them as an argument for another one.
You can make the ObservedObject more lazy. So you can associate it the EnvironmentObject when it's available. for example:
struct CategoryView: View {
//instance of observer object
#ObservedObject var categoryData: ObserverCategory
var body: some View { ,,, }
}
Then pass it like:
struct ContentView: View {
//user settings
#EnvironmentObject var userSettings: UserSettings
var body: some View {
CategoryView(categoryData: ObserverCategory(userID: userSettings.id))
}
}