2
\$\begingroup\$

Inspired by Mike Boyd's 'Dopamine Box' video: link, I decided it would be a good project to pursue by myself in swift. The end goal is to make it look like the box in the video, with switches and a chrome exterior as well as being able to add custom icons underneath the switches.

My main question is: Have I laid out my work in a way that makes sense for what I am setting out to do?

This is what I've managed to build so far, it is a list that you can add tasks to:

Dopamine_BoxApp.swift

import SwiftUI

@main
struct Dopamine_BoxApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView().environmentObject(Library())
        }
    }
}

Goal.swift

Defines the class for the goal object, it has a title property

import Combine
import Foundation

class Goal: ObservableObject{
    @Published var title: String
    
    init(title: String = ""){
        self.title = title
    }
}

Library.swift

Defines the class for the library object which contains an array of goals and adds new goals at the start of library

import Combine
import Foundation

class Library: ObservableObject {
    var sortedGoal: [Goal] { goalsCache }
    
    
    /// Add a new goal at the start of the library
    func addNewGoal(_ goal: Goal) {
        goalsCache.insert(goal, at: 0)
    }
    
    @Published private var goalsCache: [Goal] = [
        .init(title: "Go for a run"),
        .init(title: "Do 1 hr of reading"),
        .init(title: "Eat cod liver tablets"),
        .init(title: "Make lunch"),
        .init(title: "Do 3 hrs of coding"),
        .init(title: "1 hr learning the piano"),
    ]
}

ContentView.swift

The first view the user sees that leads to the menu view, is it redundant to have a content view that simply leads to a menu view and nothing else?

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var library: Library
    
    var body: some View {
        NavigationView{
            MenuView()
        }
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        Group{
            ContentView()
                .environmentObject(Library())
        }
    }
}

MenuView.swift

The menu view contains a list of goal items in the library objects, as well as a add new daily task view

import SwiftUI

struct MenuView: View {
    @State var showDetail = false
    @State var addingNewGoal = false
    @EnvironmentObject var library: Library
    
    
    
    var body: some View{
        VStack {
            List(library.sortedGoal, id: \.title){ goal in
                Goal.GoalRow(goal: goal)
            }
            
            NavigationLink(destination: AddDailyTaskView()){
                Text("Add Daily Task")
            }
        }
    }
}
        

DetailView.swift

when you click on a goal in menu view, you get led to a view that for now contains the title of the goal that was clicked on

   import SwiftUI
    
    struct DetailView: View {
        let goal: Goal
        
        
        var body: some View {
            VStack {
                Text(goal.title)
            }
        }
    }
    

AddDailyTaskView.swift

adds a new goal to the library object

import SwiftUI
//
struct AddDailyTaskView: View {
    @ObservedObject var goal = Goal()
    @EnvironmentObject var library: Library
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    
    var body: some View{
        NavigationView{
            VStack {
                HStack {
                    Text("Name")
                    TextField("Goal Name", text: $goal.title)
                    
                }
            }
            .toolbar{
                ToolbarItem(placement: .status){
                    Button("Add to Library") {
                        library.addNewGoal(goal)
                        mode.wrappedValue.dismiss()
                    }
                    .disabled([goal.title].contains(where:\.isEmpty))
                }
            }
        }
    }
}

GoalViews.swift

goal views contains extensions to goal, GoalRow displays the goal title and links to the detail view of that goal

import SwiftUI

extension Goal{
    
    struct GoalRow: View {
        let goal:Goal
        
        var body: some View{
            NavigationLink(
                destination: DetailView(goal: goal)
            ){
                Text(goal.title)
            }
        }
    } 
}
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to the Code Review Community. While you have added a link to a description of the algorithm it would help us greatly with the review if you also put a text description of what the code is trying to accomplish in the question. Links have been known to go bad. \$\endgroup\$ – pacmaninbw Dec 12 '20 at 13:12
  • \$\begingroup\$ @pacmaninbw Thank you I've added a description for each of the swift files \$\endgroup\$ – PhillipT Dec 12 '20 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.