ok, thanks for helping. Below is the complete project code for files. At the very end, there is a link to the full project in the archive.
This is a RoomsApp.swift file code:
import SwiftUI
@main
struct RoomsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
This is ContentView.swift code:
import SwiftUI
struct ContentView: View {
@ObservedObject var store = RoomStore()
var body: some View {
NavigationView {
List {
Button(action: addRoom) {
Text("Add Room")
}
ForEach(store.rooms) { room in
RoomCell(room: room)
}
}
.navigationBarTitle(Text("Rooms"))
}
}
func addRoom() {
store.rooms.append(Room(name: "Hall 2", capacity: 2000))
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(store: RoomStore(rooms: testData))
}
}
struct RoomCell: View {
var room: Room
var body: some View {
NavigationLink(destination: RoomDetail(room: room)) {
Image(room.thumbnailName)
.cornerRadius(8)
VStack(alignment: .leading) {
Text(room.name)
Text("\(room.capacity) people")
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
RoomDetail.swift file code:
import SwiftUI
struct RoomDetail: View {
var room: Room
@State private var zoomed = false
var body: some View {
ZStack(alignment: .topLeading) {
Image(room.imageName)
.resizable()
.aspectRatio(contentMode: zoomed ? .fill : .fit)
.navigationBarTitle(room.name, displayMode: .inline )
.onTapGesture { // Instead of TapAction
withAnimation(.easeIn(duration: 1)) { zoomed.toggle() } // .easyIn is instead of basic(duration)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
if room.hasVideo && !zoomed {
Image(systemName: "video.fill")
.font(.title)
.padding(.all)
.transition(.move(edge: .leading))
}
}
}
}
struct RoomDetail_Previews: PreviewProvider {
static var previews: some View {
Group {
NavigationView{RoomDetail(room: testData[0])}
NavigationView{RoomDetail(room: testData[1])}
}
}
}
Room
import SwiftUI
struct Room: Identifiable {
var id = UUID()
var name: String
var capacity: Int
var hasVideo: Bool = false
var imageName: String {
return name
}
var thumbnailName: String {
return name + "Thumb"
}
}
let testData = [
Room(name: "room-1", capacity: 6, hasVideo: true),
Room(name: "room-2", capacity: 8, hasVideo: false),
Room(name: "room-3", capacity: 16, hasVideo: true),
Room(name: "room-4", capacity: 10, hasVideo: true),
Room(name: "room-5", capacity: 12, hasVideo: false),
Room(name: "room-6", capacity: 8, hasVideo: false),
Room(name: "room-7", capacity: 10, hasVideo: true),
Room(name: "room-8", capacity: 7, hasVideo: false),
Room(name: "room-9", capacity: 11, hasVideo: false)
]
RoomStore
import SwiftUI
import Combine
class RoomStore: ObservableObject { // Instead of BindableObject
var rooms: [Room] {
didSet { didChange.send() }
}
init(rooms: [Room] = []) {
self.rooms = rooms
}
var didChange = PassthroughSubject<Void, Never>()
}
This is a link to the complete project in the archive.
https://drive.google.com/file/d/1Sis3r57S9n9Uf4Pd8dFSyMoM-Z0vsj9K/view?usp=sharing