SenderViewMain.swift 6.75 KB
Newer Older
Oliver Wiese's avatar
Oliver Wiese committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
//  SenderViewMain.swift
//  enzevalos_iphone
//
//  Created by fatimaaa96 on 12.03.20.
//  Copyright © 2020 fu-berlin. All rights reserved.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

import SwiftUI
import CoreLocation

Oliver Wiese's avatar
Oliver Wiese committed
25
struct SenderViewMain <M: DisplayMail, C: DisplayContact>: View {
Oliver Wiese's avatar
Oliver Wiese committed
26
    
Oliver Wiese's avatar
Oliver Wiese committed
27
    let mail: M
Oliver Wiese's avatar
Oliver Wiese committed
28 29 30 31 32 33
    
    @State var selectedLandmark: Landmark? = nil
    @State var showingLandmarkDetails = false
    
    
    var body: some View {
Oliver Wiese's avatar
Oliver Wiese committed
34 35
        ScrollView {
            map
36
            icon
Oliver Wiese's avatar
Oliver Wiese committed
37 38 39 40 41 42 43 44
            sender
                .offset(y: (-110/2 + 5))
            Divider()
            SmallContactListView(contacts: mail.tos, title: NSLocalizedString("To", comment: "To"))
            Divider()
            SmallContactListView(contacts: mail.ccs, title: NSLocalizedString("Cc", comment: "CC"))
            Divider()
            SmallContactListView(contacts: mail.bccs, title: NSLocalizedString("Bcc", comment: "BCC"))
Oliver Wiese's avatar
Oliver Wiese committed
45 46 47
        }
    }
    
Oliver Wiese's avatar
Oliver Wiese committed
48 49
    
    
Oliver Wiese's avatar
Oliver Wiese committed
50
    private func selectNextLandmark() {
51
        let landmarks = mail.routingStops
Oliver Wiese's avatar
Oliver Wiese committed
52 53 54 55 56 57 58
        /// This method identifies and moves to the next selected landmark
        if let selectedLandmark = selectedLandmark, let currentIndex = landmarks.firstIndex(where: { $0 == selectedLandmark }), currentIndex + 1 < landmarks.endIndex {
            self.selectedLandmark = landmarks[currentIndex + 1]
        } else {
            selectedLandmark = landmarks.first
        }
    }
59
    
Oliver Wiese's avatar
Oliver Wiese committed
60
    private func selectPrevLandmark() {
61
        let landmarks = mail.routingStops
Oliver Wiese's avatar
Oliver Wiese committed
62 63 64 65 66 67 68
        /// This method identifies and moves to the prevoius selected landmark
        if let selectedLandmark = selectedLandmark, let currentIndex = landmarks.firstIndex(where: { $0 == selectedLandmark }), currentIndex + -1 >= 0 {
            self.selectedLandmark = landmarks[currentIndex - 1]
        } else {
            selectedLandmark = landmarks.last
        }
    }
69 70 71 72 73 74 75 76 77 78 79
    
    private var icon: some View {
        mail.sender.myImage
            .resizable()
            .frame(width: 130, height: 110)
            .clipShape(Circle())
            .overlay(Circle().stroke(Color.white, lineWidth: 4))
            .shadow(radius: 10)
            .offset(y: -110/2)
            .padding(.bottom, -110/2)
            .onTapGesture {
Oliver Wiese's avatar
Oliver Wiese committed
80
                self.goToContact(contact: self.mail.sender)
81 82
            }
    }
Oliver Wiese's avatar
Oliver Wiese committed
83 84 85
    
    private var sender: some View {
        VStack (alignment: .leading, spacing: 10) {
Oliver Wiese's avatar
Oliver Wiese committed
86
            HStack {
Oliver Wiese's avatar
Oliver Wiese committed
87 88
                VStack (alignment: .leading) {
                    Text(NSLocalizedString("Sender", comment: "Sender"))
Oliver Wiese's avatar
Oliver Wiese committed
89 90
                        .font(.headline)
                        .padding(.bottom, 10)
Oliver Wiese's avatar
Oliver Wiese committed
91
                    Text(mail.sender.name)
Oliver Wiese's avatar
Oliver Wiese committed
92
                        .font(.subheadline)
Oliver Wiese's avatar
Oliver Wiese committed
93
                    Text(mail.sender.addr)
Oliver Wiese's avatar
Oliver Wiese committed
94
                        .foregroundColor(.gray)
Oliver Wiese's avatar
Oliver Wiese committed
95
                }
Oliver Wiese's avatar
Oliver Wiese committed
96 97 98 99 100 101 102
                Spacer()
                Button(action: {self.goToContact(contact: self.mail.sender)}){
                    Image(systemName: "chevron.right")
                }
            }
            .onTapGesture {
                self.goToContact(contact: self.mail.sender)
Oliver Wiese's avatar
Oliver Wiese committed
103
            }
Oliver Wiese's avatar
Oliver Wiese committed
104 105 106 107
            HStack{
                Text(String(format: NSLocalizedString("ReadView.Sender.Previous", comment: "100 previous received mails"), mail.sender.previousMails))
                    Spacer()
                 Text(String(format: NSLocalizedString("ReadView.Sender.Responses", comment: "5 previous sent mails"), mail.sender.previousResponses))
Oliver Wiese's avatar
Oliver Wiese committed
108 109
                // TODO: Add last mail date
                // TODO: Go to mailinglists?
Oliver Wiese's avatar
Oliver Wiese committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            }
        }
    .padding(10)
    }
    
    private var map: some View {
        ZStack {
            ZStack {
                MapView(landmarks: mail.routingStops,
                        selectedLandmark: $selectedLandmark,  showingLandmarkDetails: $showingLandmarkDetails)
                    .frame(height: 300)
                HStack {
                    Button(action: {
                        /// this button displays the previous selected landmark
                        self.selectPrevLandmark()
                    }) {
                        Text("prev")
                            .foregroundColor(.black)
                            .padding()
                            .background(Color.white)
                            .cornerRadius(6)
                            .shadow(radius: 3)
                            .padding(.top, 150)
Oliver Wiese's avatar
Oliver Wiese committed
133 134
                    }
                    
Oliver Wiese's avatar
Oliver Wiese committed
135 136 137 138 139 140 141 142 143 144 145 146 147
                    Spacer()
                    Button(action: {
                        /// this button displays the next selected landmark
                        self.selectNextLandmark()
                    }) {
                        Text("Next")
                            .foregroundColor(.black)
                            .padding()
                            .background(Color.white)
                            .cornerRadius(6)
                            .shadow(radius: 3)
                            .padding(.top, 150)
                    }
Oliver Wiese's avatar
Oliver Wiese committed
148
                }
Oliver Wiese's avatar
Oliver Wiese committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162
            } .alert(isPresented: $showingLandmarkDetails) {
                /// alert displays the landmark details and gets trigered when the user taps the information button of a landmark
                Alert(title: Text("Domain for this location"), message: Text(selectedLandmark?.domain ?? "Missing place information"), dismissButton: .default(Text("OK")) )
                    }
        }
    }
    
    private func goToContact(contact: DisplayContact) {
        guard let con = contact.keyRecord else {
            print("No record...")
            return
        }
        AppDelegate.getAppDelegate().readViewCoordinator?.pushContactView(contact: con)
    }
Oliver Wiese's avatar
Oliver Wiese committed
163 164
}

165 166 167 168 169 170 171 172 173
struct SenderView_Previews: PreviewProvider {
    static var previews: some View {
        let deviceNames: [String] = [
               "iPhone SE",
               "iPhone 11 Pro Max"//,
              // "iPad Pro (11-inch)"
           ]
           
       return ForEach(deviceNames, id: \.self) {deviceName in
Oliver Wiese's avatar
Oliver Wiese committed
174
        SenderViewMain<PseuoMail, PseudoContact>(mail: mail)
175
                .previewDisplayName(deviceName)
Oliver Wiese's avatar
Oliver Wiese committed
176 177
                .previewDevice(.init(rawValue: deviceName))
                //.colorScheme(.dark)
178 179 180 181
        }
        
    }
}