article

Monday, June 28, 2021

SwiftUI Search Bar Todo List

SwiftUI Search Bar Todo List
ContentView.swift
 
//
//  ContentView.swift
//  Test
//
//  Created by Cairocoders
//

import SwiftUI


struct ContentView: View {
        
    private var todoItems = [ ToDoItem(name: "Learn SwiftUI"),
                              ToDoItem(name: "Learn Programming language"),
                              ToDoItem(name: "Learn Python"),
                              ToDoItem(name: "Coding"),
                              ToDoItem(name: "Read Books"),
                              ToDoItem(name: "Play basketball"),
                              ToDoItem(name: "Coding"),
                              ToDoItem(name: "Buy some stuff"),
                              ToDoItem(name: "Create Project App"),
                              ToDoItem(name: "Play games")
                                ]
    
    @State private var searchText = ""
    @State var show = false
    var body: some View {
        
        ZStack {
            
            VStack {
                
                HStack {
                    Text("ToDo List")
                        .font(.system(size: 30, weight: .black, design: .rounded))
                        
                    Spacer()
                    
                    Button(action: {
                        show.toggle()
                    }) {
                        Image(systemName: "plus.circle.fill")
                            .font(.largeTitle)
                            .foregroundColor(.purple)
                    }
                }
                .padding()
                
                SearchBar(text: $searchText)
                    .padding(.top, 10)
                
                List(todoItems.filter({ searchText.isEmpty ? true : $0.name.contains(searchText) })) { item in
                    Text(item.name)
                }
                .padding(.top, 20)
                
            }//End VStack
            .sheet(isPresented: $show, content: {
                Addnew()
            })
    
        }
    }

}

struct Addnew: View {

    @State private var newTodo = ""
    var body: some View {
        
        HStack { //Horizontal Stack
            TextField("Add todo...", text: $newTodo)
              .textFieldStyle(RoundedBorderTextFieldStyle())
             
            Button(action: {
            }) {
                Image(systemName: "plus")
            }
            .padding(.leading, 5)
        }.padding() //end horizontal stack
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
SearchBar.swift
 
//
//  SearchBar.swift
//  Test
//
//  Created by Cairocoders
//

import SwiftUI

struct SearchBar: View {
    @Binding var text: String

    @State private var isEditing = false
        
    var body: some View {
        HStack {
            
            TextField("Search ...", text: $text)
                .padding(7)
                .padding(.horizontal, 25)
                .background(Color(.systemGray6))
                .cornerRadius(8)
                .overlay(
                    HStack {
                        Image(systemName: "magnifyingglass")
                            .foregroundColor(.gray)
                            .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                            .padding(.leading, 8)
                        
                        if isEditing {
                            Button(action: {
                                self.text = ""
                                
                            }) {
                                Image(systemName: "multiply.circle.fill")
                                    .foregroundColor(.gray)
                                    .padding(.trailing, 8)
                            }
                        }
                    }
                )
                .padding(.horizontal, 10)
                .onTapGesture {
                    self.isEditing = true
                }
            
            if isEditing {
                Button(action: {
                    self.isEditing = false
                    self.text = ""
                    
                    // Dismiss the keyboard
                    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                }) {
                    Text("Cancel")
                }
                .padding(.trailing, 10)
                .transition(.move(edge: .trailing))
                .animation(.default)
            }
        }
    }
}

struct SearchBar_Previews: PreviewProvider {
    static var previews: some View {
        SearchBar(text: .constant(""))
    }
}
ToDoItem.swift
 
//
//  ToDoItem.swift
//  Test
//
//  Created by Cairocoders
//

import Foundation
import CoreData

enum Priority: Int {
    case low = 0
    case normal = 1
    case high = 2
}

struct ToDoItem: Identifiable {
    var id = UUID()
    var name: String = ""
    var priorityNum: Priority = .normal
    var isComplete: Bool = false
}

Related Post