Skip to content

Instantly share code, notes, and snippets.

@tfedyanin
Created February 9, 2020 19:27
Show Gist options
  • Save tfedyanin/41fc7a1af4ef430a96e125c1879ea8be to your computer and use it in GitHub Desktop.
Save tfedyanin/41fc7a1af4ef430a96e125c1879ea8be to your computer and use it in GitHub Desktop.
Context menu problem
//
// AdCardView.swift
// dr-haus-front-swift
//
// Created by Тимофей Федянин on 22.01.2020.
// Copyright © 2020 Тимофей Федянин. All rights reserved.
//
import Foundation
import SwiftUI
import URLImage
struct AdCardView: View, Identifiable, Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func ==(lhs: AdCardView, rhs: AdCardView) -> Bool {
return lhs.id == rhs.id
}
var id: UUID = UUID()
var ad: Ad
let width: CGFloat
let height: CGFloat
let aspectRatio: CGFloat
let price: String
let url: URL
let lowUrl: URL
init(ad: Ad) {
self.ad = ad
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
self.price = formatter.string(for: ad.price)!
self.url = URL(string: ad.bestImageMetadata!.url!)!
self.lowUrl = URL(string: ad.bestImageMetadata!.ultraLowUrl!)!
self.aspectRatio = CGFloat(ad.bestImageMetadata!.width!) / CGFloat(ad.bestImageMetadata!.height!)
self.width = UIScreen.main.bounds.width
self.height = UIScreen.main.bounds.width / aspectRatio
}
var body: some View {
VStack {
NavigationLink(destination: AdDetailView(ad)) {
VStack {
FullWidthImageView(ad)
HStack {
Text("\(self.price) \(self.ad.currency!)")
.font(.headline)
Spacer()
SwiftUI.Image(systemName: "heart")
}
.padding([.top, .leading, .trailing], 10.0)
.contextMenu(ContextMenu {
Button("A"){}
Button("B") {}
})
HStack {
Text("\(self.ad.roomsCount!, specifier: "%.0f") комн.")
Text("\u{2022}")
Text("\(self.ad.totalArea!, specifier: "%.1f") м") + Text("\u{00B2}")
Text("\u{2022}")
Text(" \(self.ad.floorNumber!, specifier: "%.0f") этаж")
Spacer()
}
.padding(.all, 10.0)
Divider()
.padding(.horizontal)
HStack {
Text("\(self.ad.address!)")
.foregroundColor(Color.gray)
.lineLimit(1)
Spacer()
SwiftUI.Image(systemName: "ellipsis")
}
.padding(10.0)
}
}
.background(Color(.white))
.padding(.top, 10)
}
.background(Color(hex: "C5C3C6")) //Mercury
.listRowInsets(EdgeInsets())
}
}
extension Color {
init(hex: String) {
let scanner = Scanner(string: hex)
var rgbValue: UInt64 = 0
scanner.scanHexInt64(&rgbValue)
let r = (rgbValue & 0xff0000) >> 16
let g = (rgbValue & 0xff00) >> 8
let b = rgbValue & 0xff
self.init(red: Double(r) / 0xff, green: Double(g) / 0xff, blue: Double(b) / 0xff)
}
}
import Foundation
import SwiftUI
import URLImage
struct FullWidthImageView: View {
let url: URL
let lowUrl: URL
let width: CGFloat
let height: CGFloat
let aspectRatio: CGFloat
init(_ ad: Ad) {
self.url = URL(string: ad.bestImageMetadata!.url!)!
self.lowUrl = URL(string: ad.bestImageMetadata!.ultraLowUrl!)!
self.aspectRatio = CGFloat(ad.bestImageMetadata!.width!) / CGFloat(ad.bestImageMetadata!.height!)
self.width = UIScreen.main.bounds.width
self.height = UIScreen.main.bounds.width / aspectRatio
}
var body: some View {
URLImage(url,
delay: 0.5,
expireAfter: Date(timeIntervalSinceNow: 31_556_926.0),
placeholder: { _ in
URLImage(self.lowUrl,
delay: 0.2,
expireAfter: Date(timeIntervalSinceNow: 31_556_926.0),
placeholder: { _ in
Rectangle()
.fill(Color(hex: "4C5C68"))
},
content: { proxy in
proxy.image
.resizable()
.scaledToFill()
})
.frame(width: self.width, height: self.height)
},
content: { proxy in
proxy.image
.resizable()
.scaledToFill()
})
.frame(width: self.width, height: self.height)
}
}
import Foundation
import SwiftUI
struct AdCardListView: View {
@ObservedObject var model: AdListViewModel = AdListViewModel()
var body: some View {
List {
ForEach(self.model.adArray) { ad in
AdCardView(ad: ad)
.onAppear {
DispatchQueue.main.async {
print("On apear element ", ad.id!)
self.model.fetch(ad)
}
}
}
if (!model.endIsReached) {
VStack {
HStack {
Spacer()
Text("Загрузка...")
Spacer()
}
.background(Color.white)
.padding(.top, 10)
}
.listRowInsets(EdgeInsets())
.background(Color(hex: "C5C3C6")) //Mercury
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment