Skip to content

Instantly share code, notes, and snippets.

@fishkingsin
Created September 2, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fishkingsin/bba9e2b1aa51e7d1d495b877fe946e3a to your computer and use it in GitHub Desktop.
Save fishkingsin/bba9e2b1aa51e7d1d495b877fe946e3a to your computer and use it in GitHub Desktop.
SwiftUI + ViewModel + Redux
import SwiftUI
// Move me to MDL
struct CardView: View {
let record: Record
var body: some View {
VStack(alignment: .leading) {
Text(record.title)
.font(.headline)
Spacer()
if !record.bannerURL.isEmpty, let url = URL(string: record.bannerURL) {
// TODO: https://sarunw.com/posts/how-to-resize-swiftui-image-and-keep-aspect-ratio/
AsyncImage(url: url,
placeholder: { Text("Loading ...") },
image: {
Image(uiImage: $0)
.resizable()
})
.frame(width: 100, height: 100, alignment: .center)
.aspectRatio(contentMode: .fit)
.scaledToFit()
}
HStack {
if #available(iOS 14.0, *) {
Label("\(record.title)", systemImage: "person.3")
} else {
// Fallback on earlier versions
}
Spacer()
if #available(iOS 14.0, *) {
Label("\(record.seqNo)", systemImage: "clock")
.padding(.trailing, 20)
} else {
// Fallback on earlier versions
}
}
}.border(.green)
}
}
struct CardView_Previews: PreviewProvider {
static var record = Record(seqNo: 0, title: "", subtitle: "", bannerURL: "", websiteURL: "", shareContent: "", startTime: "", endTime: "", remainTimeText: "", showRemainTimeText: false, weight: 0, tags: "")
static var previews: some View {
CardView(record: record)
.previewLayout(.fixed(width: 400, height: 60))
}
}
import SwiftUI
struct ExploreCenterLandingView: View {
@StateObject private var viewModel = ViewModel()
var body: some View {
List(viewModel.records, id: \.name) { records in
VStack {
Text(records.name).padding()
ScrollView(.horizontal,showsIndicators: false) {
HStack(spacing: 15) {
ForEach(records.records, id: \.seqNo) { record in
HStack {
CardView(record: record)
}
}
}
.padding(.top, 10)
}
.frame(height: 150)
}
}
}
}
struct Previews_ExploreCenterLandingView_Previews: PreviewProvider {
static var previews: some View {
ExploreCenterLandingView()
}
}
import SwiftUI
import Resolver
extension ExploreCenterLandingView {
@MainActor class ViewModel: ObservableObject, ExploreCenterView {
@MainActor weak var exploreCenterRedux: ExploreCenterReduxImplementation!
@Published private(set) var records: [ExploresRecord] = []
init() {
exploreCenterRedux = Resolver.optional(ExploreCenterReduxImplementation.self, name: "ExploreCenterReduxImplementation", args: nil)
exploreCenterRedux.register(self)
exploreCenterRedux.viewDidLoad(self)
getExplores()
}
func getExplores() {
exploreCenterRedux?.dispatch(.getExplores)
}
func updateExplores(exploreEntity: ExploreCenterEntity) {
debugLog(exploreEntity)
if let records = exploreEntity.explores?.object.records {
self.records = records
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment