112 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Swift
		
	
	
	
	
	
//
 | 
						|
//  SectionEventBannerView.swift
 | 
						|
//  SodaLive
 | 
						|
//
 | 
						|
//  Created by klaus on 2023/08/09.
 | 
						|
//
 | 
						|
 | 
						|
import SwiftUI
 | 
						|
import Kingfisher
 | 
						|
 | 
						|
struct SectionEventBannerView: View {
 | 
						|
    
 | 
						|
    @State private var currentIndex = 0
 | 
						|
    @State private var timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
 | 
						|
    @AppStorage("token") private var token: String = UserDefaults.string(forKey: UserDefaultsKey.token)
 | 
						|
    
 | 
						|
    let items: [EventItem]
 | 
						|
    
 | 
						|
    var body: some View {
 | 
						|
        VStack(spacing: 16) {
 | 
						|
            TabView(selection: $currentIndex) {
 | 
						|
                ForEach(0..<items.count, id: \.self) { index in
 | 
						|
                    let item = items[index]
 | 
						|
                    if let url = item.thumbnailImageUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
 | 
						|
                        KFImage(URL(string: url))
 | 
						|
                            .cancelOnDisappear(true)
 | 
						|
                            .resizable()
 | 
						|
                            .scaledToFill()
 | 
						|
                            .frame(
 | 
						|
                                width: screenSize().width,
 | 
						|
                                height: screenSize().width * 300 / 1000,
 | 
						|
                                alignment: .center
 | 
						|
                            )
 | 
						|
                            .tag(index)
 | 
						|
                            .onTapGesture {
 | 
						|
                                if !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
 | 
						|
                                    if let _ = item.detailImageUrl {
 | 
						|
                                        AppState.shared.setAppStep(step: .eventDetail(event: item))
 | 
						|
                                    } else if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
 | 
						|
                                        UIApplication.shared.open(url)
 | 
						|
                                    }
 | 
						|
                                } else {
 | 
						|
                                    AppState.shared.setAppStep(step: .login)
 | 
						|
                                }
 | 
						|
                            }
 | 
						|
                    } else {
 | 
						|
                        KFImage(URL(string: item.thumbnailImageUrl))
 | 
						|
                            .cancelOnDisappear(true)
 | 
						|
                            .resizable()
 | 
						|
                            .scaledToFill()
 | 
						|
                            .frame(
 | 
						|
                                width: screenSize().width,
 | 
						|
                                height: screenSize().width * 300 / 1000,
 | 
						|
                                alignment: .center
 | 
						|
                            )
 | 
						|
                            .tag(index)
 | 
						|
                            .onTapGesture {
 | 
						|
                                if !token.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
 | 
						|
                                    if let _ = item.detailImageUrl {
 | 
						|
                                        AppState.shared.setAppStep(step: .eventDetail(event: item))
 | 
						|
                                    } else if let link = item.link, link.trimmingCharacters(in: .whitespaces).count > 0, let url = URL(string: link), UIApplication.shared.canOpenURL(url) {
 | 
						|
                                        UIApplication.shared.open(url)
 | 
						|
                                    }
 | 
						|
                                } else {
 | 
						|
                                    AppState.shared.setAppStep(step: .login)
 | 
						|
                                }
 | 
						|
                            }
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
 | 
						|
            .frame(
 | 
						|
                width: screenSize().width,
 | 
						|
                height: screenSize().width * 300 / 1000,
 | 
						|
                alignment: .center
 | 
						|
            )
 | 
						|
            
 | 
						|
            HStack(spacing: 8) {
 | 
						|
                ForEach(0..<items.count, id: \.self) { index in
 | 
						|
                    Capsule()
 | 
						|
                        .foregroundColor(index == currentIndex ? .button : .gray77)
 | 
						|
                        .frame(width: 10, height: 10)
 | 
						|
                        .tag(index)
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        .onAppear {
 | 
						|
            timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
 | 
						|
        }
 | 
						|
        .onDisappear {
 | 
						|
            timer.upstream.connect().cancel()
 | 
						|
        }
 | 
						|
        .onReceive(timer) { _ in
 | 
						|
            DispatchQueue.main.async {
 | 
						|
                withAnimation {
 | 
						|
                    if currentIndex == items.count - 1 {
 | 
						|
                        currentIndex = 0
 | 
						|
                    } else {
 | 
						|
                        currentIndex += 1
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
struct SectionEventBannerView_Previews: PreviewProvider {
 | 
						|
    static var previews: some View {
 | 
						|
        SectionEventBannerView(items: [])
 | 
						|
    }
 | 
						|
}
 |