Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Previews #41

Closed
damartin1 opened this issue Aug 3, 2022 · 15 comments
Closed

Using Previews #41

damartin1 opened this issue Aug 3, 2022 · 15 comments

Comments

@damartin1
Copy link

Good morning,

Am I missing something here? I am trying to feed a User array into my preview, but for the life of me I cannot get this thing to work. Below is a snippet, the error is "Type Twift has no member User", however this a nearly exact copy of the results received from the api call. Thoughts.

struct FollowingView_Previews: PreviewProvider {
static var previews: some View {
let users: [User] = [Twift.User(id: "1445771133337235470", name: "Oppenheimer", username: "OppenheimerFilm", createdAt: Date(), protected: false, withheld: WithheldInformation, location: "nowhere", pinnedTweetId: "", url: "", description: "", verified: "", entities: "", profileImageUrl: "", publicMetrics: "")]
FollowingView(users: users)
}
}

@damartin1
Copy link
Author

anyone? someone? help :)

@emin-grbo
Copy link
Contributor

Is the let users: [User] using your own user or Twift user? If it is your own, maybe use a different name?
Also, I might be off the mark here, but Twift.User can be used in declaring the type, not when initializing it? Basically replace these two, see if it helps: let users: [Twift.User] = [User(id:...

@damartin1
Copy link
Author

Thanks for the reply. I tried that but get a "User' is not a member type of class 'Twift.Twift'". I also did the import struct Twift.User, but get the same error.

@emin-grbo
Copy link
Contributor

If you delete Twift. a true error will reveals itself :)
User inside Twift has no initialiser, so you cannot really mock data like so.

Not sure if you can try to extend it and try if that works, or, the way I did it...store it in UserDefaults when fetching real data and use that for some offline tests :)

@damartin1
Copy link
Author

When I take off Twift I get the "Missing argument for parameter 'from' in call insert 'from: <#Decoder#>, '". Can you expand upon using UserDefaults? I am wanting to be able to see the preview live for UI modifications and just pass it some random data to create it.

@emin-grbo
Copy link
Contributor

emin-grbo commented Aug 9, 2022

Since you need to use mock data to have it in the preview, and as it is easier to actually have real data there, you can log in once, save user data, and then in the preview fetch that data through userDefaults.

I made this to encode/decode stuff for userData. Just replace MODEL and KEY with proper stuff you are trying to encode/decode :) Hope this helps!

extension UserDefaults {
    static func encodeITEM(item: MODEL) {
        Task {
            let encoder = JSONEncoder()
            if let data = try? encoder.encode(item) {
                UserDefaults.standard.set(data, forKey: KEY)
            }
        }
        
    }
  
  static func decodeITEM() -> MODEL? {
    let decoder = JSONDecoder()
    if let data = UserDefaults.standard.data(forKey: KEY),
       var decodedMODEL = try? decoder.decode(MODEL.self, from: data) {
      return decodedMODEL
    }
      return nil
  }
  
}

@damartin1
Copy link
Author

Thanks will give this a whirl!

@daneden
Copy link
Owner

daneden commented Aug 10, 2022

As @roblack pointed out, Twift’s models don’t have initialisers besides decoding from JSON payloads. I originally designed it this way thinking that you'd only need to instantiate models for Tweets and related objects (such as Polls), and created specific models for those instantiations (MutableTweet), but SwiftUI previews are a good example of where it would be useful to instantiate your own models for other types. I can revisit this design decision!

@damartin1
Copy link
Author

I have generated the data itself, but feeding that back into a User model so that way I can see what the userRow looks like instead of having to make changes then push device has proved cumbersome for me. All I want is an Array of Users (Twift) but I can’t seem to get this to work. What would you do in this situation?

@damartin1
Copy link
Author

So if I have an array of User Data from .getFollowing that I am returning, how does this fit into the Model/Key piece?

@emin-grbo
Copy link
Contributor

If you have that data, save it in UserDefaults when fetched. Then, in the preview, or on the next app launch, just use that data instead.
That is my assumption tho...not 100% confident you can get defaults data in the preview. I would hope so? 🤷‍♂️

@emin-grbo
Copy link
Contributor

or...and I am just spitballing here...how about you get that JSON and save it in a file? Then decode that instead?!

That has to work!

https://www.hackingwithswift.com/example-code/system/how-to-decode-json-from-your-app-bundle-the-easy-way

@damartin1
Copy link
Author

Thank! I'll give this a shot! Appreciate the guidance!

@daneden
Copy link
Owner

daneden commented Aug 10, 2022

Yeah I wouldn't use UserDefaults for this; a JSON file that you decode is probably the right approach (which is what I do for other apps, for example Zeitgeist). But if it feels more natural to use the model(s) directly I'm open to changing Twift to enable this if there's enough demand!

@daneden daneden closed this as completed Aug 10, 2022
@damartin1
Copy link
Author

Thanks everyone, got it working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants