damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

ImageView.swift (3043B)


      1 //
      2 //  ImageView.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-03-23.
      6 //
      7 
      8 import SwiftUI
      9 
     10 struct ImageView: View {
     11     let video_controller: VideoController
     12     let urls: [MediaUrl]
     13     
     14     @Environment(\.presentationMode) var presentationMode
     15     
     16     @State var showMenu = true
     17     
     18     let settings: UserSettingsStore
     19     @Binding var selectedIndex: Int
     20     
     21     var tabViewIndicator: some View {
     22         HStack(spacing: 10) {
     23             ForEach(urls.indices, id: \.self) { index in
     24                 Capsule()
     25                     .fill(index == selectedIndex ? Color(UIColor.label) : Color.secondary)
     26                     .frame(width: 7, height: 7)
     27                     .onTapGesture {
     28                         selectedIndex = index
     29                     }
     30             }
     31         }
     32         .padding()
     33         .background(.regularMaterial)
     34         .clipShape(Capsule())
     35     }
     36     
     37     var body: some View {
     38         ZStack {
     39             Color(.systemBackground)
     40                 .ignoresSafeArea()
     41             
     42             TabView(selection: $selectedIndex) {
     43                 ForEach(urls.indices, id: \.self) { index in
     44                     ZoomableScrollView {
     45                         ImageContainerView(video_controller: video_controller, url: urls[index], settings: settings)
     46                             .aspectRatio(contentMode: .fit)
     47                             .padding(.top, Theme.safeAreaInsets?.top)
     48                             .padding(.bottom, Theme.safeAreaInsets?.bottom)
     49                     }
     50                     .modifier(SwipeToDismissModifier(minDistance: 50, onDismiss: {
     51                         presentationMode.wrappedValue.dismiss()
     52                     }))
     53                     .ignoresSafeArea()
     54                     .tag(index)
     55                 }
     56             }
     57             .ignoresSafeArea()
     58             .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
     59             .gesture(TapGesture(count: 2).onEnded {
     60                 // Prevents menu from hiding on double tap
     61             })
     62             .gesture(TapGesture(count: 1).onEnded {
     63                 showMenu.toggle()
     64             })
     65             .overlay(
     66                 GeometryReader { geo in
     67                     VStack {
     68                         if showMenu {
     69                             NavDismissBarView()
     70                             Spacer()
     71                             
     72                             if (urls.count > 1) {
     73                                 tabViewIndicator
     74                             }
     75                         }
     76                     }
     77                     .animation(.easeInOut, value: showMenu)
     78                     .padding(.bottom, geo.safeAreaInsets.bottom == 0 ? 12 : 0)
     79                 }
     80             )
     81         }
     82     }
     83 }
     84 
     85 struct ImageView_Previews: PreviewProvider {
     86     static var previews: some View {
     87         let url: MediaUrl = .image(URL(string: "https://jb55.com/red-me.jpg")!)
     88         ImageView(video_controller: test_damus_state.video, urls: [url], settings: test_damus_state.settings, selectedIndex: Binding.constant(0))
     89     }
     90 }