This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CategoriesView.swift
85 lines (74 loc) 路 2.81 KB
/
CategoriesView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// CategoriesView.swift
// Revill
//
// Created by aluno on 30/09/19.
// Copyright 漏 2019 Revill. All rights reserved.
//
import SwiftUI
/// List of categories Available
struct CategoriesView: View {
@State private var selected: Int = 0 // Selected button on the menu bar
let categories: [Categories] = Categories.allCases // Array with all categories
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(self.categories, id: \.self) { category in // Displays each category on the menu bar
CategoryButton(selection: self.$selected, category: category)
.padding(.trailing, 10)
}
}
.padding(.leading)
}
}
}
/// View of the category buttons
struct CategoryButton: View {
@Binding var selection: Int // Selected menu button binding
let category: Categories // Category of the button
var selected: Bool {
return selection == category.index // Checks if this button is the selected one
}
// Layout variables
let color: Color = Color.purple
let cornerRadius: CGFloat = 16
let borderWidth: CGFloat = 2
let shadowOpacity: Double = 0.25
var body: some View {
Button(action: {
self.selection = self.category.index // Updates the selected button when tapped
}, label: {
Text(self.category.rawValue)
.shadow(color: Color.black.opacity(self.selected ? 0 : shadowOpacity), radius: 4, x: 0, y: 4) // Text shadow (if selected, doesnt appear)
.padding(.vertical, 10)
.padding(.horizontal, cornerRadius)
.background(color)
.cornerRadius(cornerRadius)
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: cornerRadius) // Overlay to display round border
.stroke(Color.white, lineWidth: self.selected ? borderWidth : 0) // Border (only appears if selected)
)
.shadow(color: Color.black.opacity(self.selected ? shadowOpacity : 0), radius: 4, x: 0, y: 4) // Background shadow (only appears if selected)
})
.padding(.vertical)
}
}
/// Mocked dates dates come to API
enum Categories: String, CaseIterable {
case upComing = "UPComing"
case topList = "Top List"
case adventure = "Adventure"
case tripleA = "AAA"
}
/// Extension to be able to get the index of a Categories
extension CaseIterable where Self: Equatable {
var index: Self.AllCases.Index {
return Self.allCases.firstIndex { self == $0 } ?? Self.allCases.endIndex
}
}
struct CategoriesView_Previews: PreviewProvider {
static var previews: some View {
CategoriesView()
}
}