Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update iOS Example #2852

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 0 additions & 10 deletions examples/swift/Podfile

This file was deleted.

3 changes: 0 additions & 3 deletions examples/swift/Podfile.lock

This file was deleted.

53 changes: 31 additions & 22 deletions examples/swift/Shared/ContentView.swift
@@ -1,46 +1,55 @@
import SwiftUI
import BuilderIO

class BuilderContentWrapper: ObservableObject {
var content: BuilderContent? = nil;
init(content: BuilderContent? = nil) {
self.content = content
}

func changeContent(_ newValue: BuilderContent?) {
self.content = newValue;
self.objectWillChange.send();
}
}

struct ContentView: View {
@ObservedObject var content: BuilderContentWrapper = BuilderContentWrapper();

/**
var headingText: String;
var detailText: String;
var ctaText: String;
var image: String;
*/
init() {
registerComponent(name: "HeroComponent", factory: { (options, styles) in
return HeroComponent(headingText: options["headingText"].stringValue, ctaText: options["ctaText"].stringValue)
});
registerComponent(component: BuilderCustomComponent(name: "HeroComponent",
inputs: [BuilderInput(name: "headingText", type: "string"),
BuilderInput(name: "detailText", type: "string"),
BuilderInput(name: "ctaText", type: "string"),
BuilderInput(name: "image", type: "string")]),
factory: { (options, styles, _) in
return HeroComponent(headingText: options["headingText"].stringValue, detailText: options["detailText"].stringValue, ctaText: options["ctaText"].stringValue, image: options["image"].stringValue)
}, apiKey: "e084484c0e0241579f01abba29d9be10");
fetchContent()
}

func fetchContent() {
Content.getContent(model: "page", apiKey: "e084484c0e0241579f01abba29d9be10", url: "/custom-components", locale: "", preview: "") { content in
Content.getContent(model: "page", apiKey: "e084484c0e0241579f01abba29d9be10", url: "/buttons", locale: "", preview: "") { content in
DispatchQueue.main.async {
self.content.changeContent(content);
}
}
}

var body: some View {
ScrollView {
if $content.content.wrappedValue != nil {
RenderContent(content: $content.content.wrappedValue!)
VStack {
ScrollView {
if $content.content.wrappedValue != nil {
RenderContent(content: $content.content.wrappedValue!, apiKey: "e084484c0e0241579f01abba29d9be10") { text, urlString in
print("Some one clicked a button!!", text, urlString ?? "empty url");
}
}
// HeroComponent(headingText: "HEADING TEXT", detailText: "Some details here", ctaText: "Click me", image: "https://cdn.builder.io/api/v1/image/assets/TEMP/5f780b17-6959-4cb2-84a3-f9f8fd20292e?placeholderIfAbsent=true")
}.frame(idealWidth: .infinity, maxWidth: .infinity)

if (Content.isPreviewing()) {
Button("Reload") {
fetchContent();
}.onReceive(NotificationCenter.default.publisher(for: deviceDidShakeNotification)) { _ in
fetchContent()
}
}
}

Button("Reload") {
fetchContent()
}
}
}

Expand Down
89 changes: 60 additions & 29 deletions examples/swift/Shared/HeroComponent.swift
Expand Up @@ -2,39 +2,70 @@ import SwiftUI
import BuilderIO

struct HeroComponent: View {

var headingText: String;
var detailText: String;
var ctaText: String;

var body: some View {
Text(headingText)
.font(.system(size: 25).weight(Font.Weight.heavy))
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.foregroundColor(.black)
.background(RoundedRectangle(cornerRadius: 10).fill(.white));

Button(action: {
print("Button tapped");
}, label: {
Text(ctaText)
.font(.system(size: 12).weight(Font.Weight.medium))
.foregroundColor(.white)
.padding(10)

})
// rgb(74, 144, 226)
.foregroundColor(.white)
//.buttonStyle(.borderedProminent)
.background(RoundedRectangle(cornerRadius: 10).fill(Color(red: 74 / 255, green: 144 / 255, blue: 226 / 255, opacity: 1)))
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)

}
var image: String;

var body: some View {
VStack {
BackportAsyncImage(url: URL(string: image), scale: 1) { phase in
if let image = phase.image {
image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(minWidth: 0, idealWidth: .infinity, maxWidth: .infinity)
} else if phase.error != nil {
Color.red
} else {
Color.blue
}
}.padding(20)
.cornerRadius(20)

Text(headingText)
.font(.system(size: 25).weight(Font.Weight.heavy))
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.foregroundColor(.white)
.textCase(.uppercase)
.padding(.bottom, 10)

Text(detailText)
.font(.system(size: 12))

.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)
.foregroundColor(.white)


Button(action: {
print("Button tapped");
}, label: {
Text(ctaText)
.font(.system(size: 12).weight(Font.Weight.medium))
.foregroundColor(Color(red: 27/256, green: 27/256, blue: 27/256))
.padding(5)

})
.foregroundColor(.white)
// CAEA03
.padding(5)
.background(Color(red: 202/256, green: 234/256, blue: 3/256))
.padding(10)
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity, alignment: .center)

}.background(.black)
}
}

struct HeroComponent_Previews: PreviewProvider {
static var previews: some View {
HeroComponent(headingText: "Default Heading", ctaText: "Default CTA Text")
}
HeroComponent(
headingText: "Default Heading",
detailText: "I am detail text",
ctaText: "Default CTA Text",
image: "https://cdn.builder.io/api/v1/image/assets%2FYJIGb4i01jvw0SRdL5Bt%2F72c80f114dc149019051b6852a9e3b7a"
) }
}
48 changes: 48 additions & 0 deletions examples/swift/Shared/NavigationViews.swift
@@ -0,0 +1,48 @@
//
// NavigationViews.swift
// SwiftExample (iOS)
//
// Created by Shyam Seshadri on 9/4/23.
//

import SwiftUI

struct NavigationViews: View {
var body: some View {
TabView {
ContentView()
.tabItem {
Image(systemName: "house")
Text("Home")
}
Text("Search Screen")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Search")
}
Text("Bag Screen")
.tabItem {
Image(systemName: "bag.fill")
Text("Bag")
}
Text("Profile Screen")
.tabItem {
Image(systemName: "person.crop.circle")
Text("Profile")
}
}
// NavigationView {
// NavigationLink(destination: ContentView()) {
// Text("Navigate to Home Screen")
// }
// .navigationBarTitle("Login!")
// }
//
}
}

struct NavigationViews_Previews: PreviewProvider {
static var previews: some View {
NavigationViews()
}
}
2 changes: 1 addition & 1 deletion examples/swift/Shared/testingApp.swift
Expand Up @@ -12,7 +12,7 @@ import SwiftUI
struct testingApp: App {
var body: some Scene {
WindowGroup {
ContentView()
NavigationViews()
}
}
}