Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@

import UIKit

protocol AddressViewDelegate: AnyObject {
func pushAddresSearchView()
}

class AddressView: UIView {

override init(frame: CGRect) {
super.init(frame: .zero
)
weak var delegate: AddressViewDelegate?

init() {
super.init(frame: .zero)

setupViews()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand All @@ -38,6 +43,7 @@ class AddressView: UIView {
button.layer.cornerRadius = 8
button.layer.borderColor = UIColor.systemBlue.cgColor
button.setAttributedTitle(attributeBtn, for: .normal)
button.addTarget(self, action: #selector(didTapEditButton), for: .touchUpInside)
return button
}()

Expand All @@ -51,6 +57,10 @@ class AddressView: UIView {
return stackView
}()

@objc func didTapEditButton () {
delegate?.pushAddresSearchView()
}

}

private extension AddressView {
Expand All @@ -74,6 +84,7 @@ private extension AddressView {

editButton.heightAnchor.constraint(equalToConstant: 32),
editButton.widthAnchor.constraint(equalToConstant: 60),

stackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 17),
stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 18),
stackView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -17)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CategoryCellView: UICollectionViewCell {
}()


func setupView(_ category:Category){
func setupView(_ category: Category){
self.image.image = UIImage(named:category.imageName )
self.label.text = category.name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ class RestaurantCellView: UITableViewCell {
super.init(style: style, reuseIdentifier: reuseIdentifier)

setupViews()
setupCell(restaurant: Restaurant.stub())
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private lazy var containerView: UIView = {
let view = UIView()

view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private lazy var restaurantImage: UIImageView = {
let image = UIImageView()

Expand All @@ -42,20 +34,23 @@ class RestaurantCellView: UITableViewCell {
let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false
label.font = .systemFont(ofSize: 15, weight: .semibold)
return label
}()

private lazy var categoryLabel: UILabel = {
let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false
label.font = .systemFont(ofSize: 13, weight: .light)
return label
}()

private lazy var deliveryTimeLabel: UILabel = {
let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false
label.font = .systemFont(ofSize: 13, weight: .light)
return label
}()

Expand All @@ -74,6 +69,7 @@ class RestaurantCellView: UITableViewCell {
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.spacing = 0
stackView.alignment = .leading
return stackView
}()

Expand All @@ -100,21 +96,17 @@ private extension RestaurantCellView {
}

func configureSubviews() {
self.addSubview(containerView)
containerView.addSubview(restaurantImage)
containerView.addSubview(stackView)
contentView.addSubview(restaurantImage)
contentView.addSubview(stackView)
}

func configureSubviewsConstraints() {

NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: contentView.topAnchor),
containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 23),
containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -23),
containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -16),

restaurantImage.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 8),
restaurantImage.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
restaurantImage.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8),
restaurantImage.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 24),
restaurantImage.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8),
restaurantImage.widthAnchor.constraint(equalToConstant: 48),
restaurantImage.heightAnchor.constraint(equalToConstant: 48),

Expand All @@ -130,7 +122,10 @@ import SwiftUI

struct RestaurantCellView_Preview: PreviewProvider {
static var previews: some View {
return RestaurantCellView().showPreview()
let view = RestaurantCellView()

view.setupCell(restaurant: .stub())
return view.showPreview()
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import UIKit

struct HomeViewConfiguration {

let restaurants: [String]
let restaurants: [Restaurant]
}

final class HomeView: UIView {
Expand All @@ -18,7 +18,7 @@ final class HomeView: UIView {

private var restaurants: [Restaurant] = []

private lazy var firstdDividerView: DividerView = {
private lazy var firstDividerView: DividerView = {
let view = DividerView()

view.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -32,19 +32,17 @@ final class HomeView: UIView {
return view
}()

private lazy var addressView: AddressView = {
lazy var addressView: AddressView = {
let view = AddressView()

view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private lazy var tableView: UITableView = {

let tableView = UITableView(frame: .zero)
lazy var tableView: RestaurantListView = {
let tableView = RestaurantListView()

tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.restaurantCellIdentifier)
tableView.dataSource = self
return tableView
}()

Expand All @@ -59,9 +57,8 @@ final class HomeView: UIView {
fatalError("init(coder:) has not been implemented")
}

func updateView(with restaurants: [Restaurant]) {
func updateView() {

self.restaurants = restaurants
self.tableView.reloadData()
}
}
Expand All @@ -78,7 +75,7 @@ private extension HomeView {

func configureSubviews() {

self.addSubview(self.firstdDividerView)
self.addSubview(self.firstDividerView)
self.addSubview(self.addressView)
self.addSubview(self.secondDividerView)
self.addSubview(self.tableView)
Expand All @@ -88,11 +85,11 @@ private extension HomeView {

NSLayoutConstraint.activate([

firstdDividerView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor),
firstdDividerView.widthAnchor.constraint(equalTo: self.widthAnchor),
firstDividerView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor),
firstDividerView.widthAnchor.constraint(equalTo: self.widthAnchor),


addressView.topAnchor.constraint(equalTo: firstdDividerView.bottomAnchor),
addressView.topAnchor.constraint(equalTo: firstDividerView.bottomAnchor),
addressView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
addressView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
addressView.bottomAnchor.constraint(equalTo: secondDividerView.topAnchor),
Expand All @@ -108,32 +105,13 @@ private extension HomeView {
}
}

extension HomeView: UITableViewDataSource {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.restaurants.count
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: self.restaurantCellIdentifier)!
cell.textLabel?.text = self.restaurants[indexPath.row].name
return cell
}
}

#if DEBUG
import SwiftUI

struct HomeView_Preview: PreviewProvider {
static var previews: some View {
let homeView = HomeView()
homeView.updateView(with: [
Restaurant.stub(),
Restaurant.stub(),
Restaurant.stub(),
])

return homeView.showPreview()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,52 @@
import UIKit

class HomeViewController: UIViewController {

private let deliveryApi = DeliveryApi()

private let searchController = UISearchController()

let deliveryApi = DeliveryApi()

private let homeView: HomeView = {
private lazy var homeView: HomeView = {

let homeView = HomeView()
homeView.addressView.delegate = self
return homeView
}()

private lazy var addressView: AddressView = {
let addressView = AddressView()

return addressView
}()

init() {
super.init(nibName: nil, bundle: nil)

navigationItem.title = "Delivery App"
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {

navigationController?.navigationBar.prefersLargeTitles = true


override func viewWillAppear(_ animated: Bool) {
deliveryApi.fetchRestaurants { restaurants in

guard let restaurants = restaurants else {
return
}

DispatchQueue.main.async {

self.homeView.updateView(with: restaurants)
let tableView = self.homeView.tableView

tableView.updateTableView(restaurants: restaurants)
}
}
}

override func viewDidLoad() {

navigationController?.navigationBar.prefersLargeTitles = true

configSearchBar()
}
Expand All @@ -58,6 +68,14 @@ class HomeViewController: UIViewController {
}
}

extension HomeViewController: AddressViewDelegate {

func pushAddresSearchView() {
let controller = AddressSearchViewController()
self.navigationController?.pushViewController(controller, animated: true)
}
}

#if DEBUG
import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,55 @@

import UIKit

class RestaurantListView: UIView {
class RestaurantListView: UITableView {

var restaurantList: [Restaurant] = []

func updateTableView(restaurants: [Restaurant]) {
self.restaurantList = restaurants

self.reloadData()
}

override init(frame: CGRect, style: UITableView.Style) {
super.init(frame: frame, style: style)

self.backgroundColor = .white

self.register(RestaurantCellView.self, forCellReuseIdentifier: "RestaurantCellView")
self.delegate = self
dataSource = self
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

}

extension RestaurantListView: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
restaurantList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = dequeueReusableCell(withIdentifier: "RestaurantCellView") as! RestaurantCellView

cell.setupCell(restaurant: restaurantList[indexPath.row])
return cell
}
}

#if DEBUG
import SwiftUI

struct RestaurantListView_Preview: PreviewProvider {
static var previews: some View {
let restaurantListView = RestaurantListView()

restaurantListView.updateTableView(restaurants: [.stub(), .stub(), .stub(), .stub(), .stub()])
return restaurantListView.showPreview()
}
}
#endif
Loading