top of page

High Times: Building a Marijuana Strain Review App in SwiftUI 🌿😄

Writer's picture: Di Nerd AppsDi Nerd Apps

Updated: Mar 25, 2023

Hello, fellow iOS developers! Are you ready to create a fun and engaging marijuana strain review app using SwiftUI? In this laid-back article, we’ll guide you through the process of building an app where users can add their favorite cannabis strains, write reviews, and upload a photo (optional). No need to worry about sign-up or sign-in — just sit back, relax, and let’s get started! 🚀🌈

The Basics: Let’s Get Rolling! 🚬

First, let’s create a simple data model for our marijuana strains:

struct Strain: Identifiable {
    let id = UUID()
    let name: String
    let review: String
    let image: UIImage?
}

Our Strain model is now ready to be blazing fast! 🔥

The Strain List: Your Weed Library 📚

Next, let’s create a view to display a list of strains:

struct StrainListView: View {
    @State private var strains: [Strain] = []

    var body: some View {
        NavigationView {
            List(strains) { strain in
                NavigationLink(destination: StrainDetailView(strain: strain)) {
                    Text(strain.name)
                }
            }
            .navigationTitle("Strains")
            .navigationBarItems(trailing: Button(action: {
                // Add strain action
            }) {
                Image(systemName: "plus")
            })
        }
    }
}

Now we have a list to keep track of our strains — it’s like having a little green garden in your pocket! 🌱



The Strain Detail View: High Praise 🌟

Let’s create a view to show the details of a selected strain:

struct StrainDetailView: View {
    let strain: Strain

    var body: some View {
        VStack {
            if let image = strain.image {
                Image(uiImage: image)
                    .resizable()
                    .scaledToFit()
            }
            Text(strain.name)
                .font(.largeTitle)
            Text(strain.review)
                .padding()
        }
    }
}

Our detail view is now ready to showcase our favorite strains in all their glory! 🍃

Adding Strains: It’s Growing! 🌳

Now, let’s create a form to add new strains to the list. But before that, we need to implement a PhotoPicker view using the new PhotosPicker:

import SwiftUI
import PhotosUI

struct PhotoPicker: UIViewControllerRepresentable {
    @Binding var selectedImage: UIImage?

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> PHPickerViewController {
        var config = PHPickerConfiguration()
        config.selectionLimit = 1
        config.filter = .images
        let controller = PHPickerViewController(configuration: config)
        controller.delegate = context.coordinator
        return controller
    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { }

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        private let parent: PhotoPicker

        init(_ parent: PhotoPicker) {
            self.parent = parent
        }

        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
            parent.selectedImage = results.first?.uiImage
            picker.dismiss(animated: true)
        }
    }
}

Adding Strains: It’s Growing! 🌳 (continued)

Now that we have our PhotoPicker in place, let's update the AddStrainView to use it:

struct AddStrainView: View {
    @Binding var strains: [Strain]
    @State private var name: String = ""
    @State private var review: String = ""
    @State private var image: UIImage?
    @State private var showPhotoPicker: Bool = false

    var body: some View {
        NavigationView {
            Form {
                TextField("Name", text: $name)
                TextEditor(text: $review)
                    .frame(height: 200)

                Button(action: {
                    showPhotoPicker = true
                }) {
                    Text("Add Photo")
                }
                .sheet(isPresented: $showPhotoPicker) {
                    PhotoPicker(selectedImage: $image)
                }

                if let image = image {
                    Image(uiImage: image)
                        .resizable()
                        .scaledToFit()
                }
                
                Button(action: {
                    let newStrain = Strain(name: name, review: review, image: image)
                    strains.append(newStrain)
                }) {
                    Text("Save")
                }
            }
            .navigationTitle("Add Strain")
        }
    }
}

And that’s it! Users can now add photos of their favorite marijuana strains using the new PhotosPicker in SwiftUI. Happy snapping! 📸🌿

Wrapping Up: The High Life 🌈

You’ve just created a fun and engaging marijuana strain review app using SwiftUI. Users can now easily add their favorite strains, complete with reviews and optional photos, without the hassle of signing up or signing in. So go ahead and share the love of cannabis with the world — it’s a joint effort! 😉🌿

Give Support or Tip👋🏿

Give a Tip with CashApp: https://cash.app/$DiAlcatic

7 views0 comments

Recent Posts

See All

Comments


bottom of page