======================== CODE SNIPPETS ======================== TITLE: DesignCode SwiftUI Examples DESCRIPTION: A collection of UI examples and components inspired by the DesignCode tutorials, implemented in SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_17 LANGUAGE: swift CODE: ``` // Code for DesignCode SwiftUI Examples would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SwiftUIDownloadView Implementation DESCRIPTION: Provides an example of a custom download view built with SwiftUI, likely including progress indicators and controls. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_13 LANGUAGE: swift CODE: ``` // Code for SwiftUIDownloadView would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SwiftUI Currency Converter DESCRIPTION: Demonstrates a currency converter application built with SwiftUI, allowing users to convert between different currencies. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_15 LANGUAGE: swift CODE: ``` // Code for SwiftUI Currency Converter would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SpotlightSearch UI in SwiftUI DESCRIPTION: Showcases a Spotlight-style search interface implemented in SwiftUI, with support for both light and dark themes. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_22 LANGUAGE: swift CODE: ``` // Code for SpotlightSearch UI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Flux Architecture Example in SwiftUI DESCRIPTION: Demonstrates the use of the Flux architecture pattern for state management in a SwiftUI application. Flux helps in creating predictable data flows. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_11 LANGUAGE: swift CODE: ``` // Code for Flux Architecture would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: MTSlideToOpen-SwiftUI Component DESCRIPTION: Implements a 'slide to open' or unlock mechanism similar to iOS, built using SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_18 LANGUAGE: swift CODE: ``` // Code for MTSlideToOpen-SwiftUI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Currency Converter & Calculator (iOS) DESCRIPTION: Showcases an iOS application that functions as both a currency converter and a calculator, likely built with SwiftUI or UIKit. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_19 LANGUAGE: swift CODE: ``` // Code for Currency Converter & Calculator would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SwiftUI Side Menu Implementation DESCRIPTION: Showcases the creation of a side menu component using SwiftUI. This is a common navigation pattern in mobile applications. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_14 LANGUAGE: swift CODE: ``` // Code for SwiftUI Side Menu would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Jike App UI in SwiftUI DESCRIPTION: Showcases the UI implementation of a 'Jike' app, likely a social or content-sharing application, built with SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_10 LANGUAGE: swift CODE: ``` // Code for Jike App UI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: UIWebview Example in Swift DESCRIPTION: Demonstrates a basic implementation of UIWebview in SwiftUI using Swift. This snippet shows how to load a URL and display it within the application's interface. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/Webview/README.md#_snippet_0 LANGUAGE: swift CODE: ``` import SwiftUI import WebKit struct ContentView : View { var body: some View { NavigationView { WebView(request: URLRequest(url: URL(string: "https://www.google.com")!)) .navigationTitle("Webview") } } } struct WebView : UIViewRepresentable { let request: URLRequest func makeUIView(context: Context) -> WKWebView { return WKWebView() } func updateUIView(_ uiView: WKWebView, context: Context) { uiView.load(request) } } struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } ``` ---------------------------------------- TITLE: GitHub Search UI in SwiftUI DESCRIPTION: Showcases the implementation of a GitHub search interface using SwiftUI. This typically involves fetching data from the GitHub API and displaying it in a user-friendly manner. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_6 LANGUAGE: swift CODE: ``` // Code for GitHub Search UI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: FlipClock-SwiftUI Component DESCRIPTION: Demonstrates a flip clock UI component implemented in SwiftUI, featuring both light and dark mode appearances. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_20 LANGUAGE: swift CODE: ``` // Code for FlipClock-SwiftUI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Animating Views and Transitions in SwiftUI DESCRIPTION: Covers the implementation of animations for views and transitions between different states or screens in SwiftUI. This enhances the user experience with dynamic and engaging interfaces. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_9 LANGUAGE: swift CODE: ``` // Code for Animating Views and Transitions would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Async Image Loading in SwiftUI DESCRIPTION: Demonstrates how to load images asynchronously in SwiftUI. This is a common requirement for displaying network-fetched images or large local assets efficiently. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_4 LANGUAGE: swift CODE: ``` // Code for Async image loading would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: PureGenius Animation in SwiftUI DESCRIPTION: Features an animation or UI component named 'PureGenius', implemented using SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_12 LANGUAGE: swift CODE: ``` // Code for PureGenius Animation would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Growing TextView in SwiftUI DESCRIPTION: Demonstrates a growing text view component in SwiftUI, which automatically adjusts its height as the user types more content. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_24 LANGUAGE: swift CODE: ``` // Code for Growing TextView in SwiftUI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: MGFlipView Component in SwiftUI DESCRIPTION: Showcases the MGFlipView component, likely a view that can flip between different content or states, implemented in SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_25 LANGUAGE: swift CODE: ``` // Code for MGFlipView in SwiftUI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: Interfacing with UIKit in SwiftUI DESCRIPTION: Illustrates how to integrate UIKit components and functionalities within a SwiftUI application. This is crucial for leveraging existing UIKit code or specific UIKit features not yet available in SwiftUI. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_5 LANGUAGE: swift CODE: ``` // Code for interfacing with UIKit would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SwiftUI Weather App DESCRIPTION: Presents a weather application developed using SwiftUI, potentially integrating with weather APIs and displaying forecast information. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_16 LANGUAGE: swift CODE: ``` // Code for SwiftUI Weather App would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: CountdownFilmClutter-SwiftUI Component DESCRIPTION: Features a 'Countdown Film Clutter' component implemented in SwiftUI, likely for displaying countdowns with a specific visual style. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_21 LANGUAGE: swift CODE: ``` // Code for CountdownFilmClutter-SwiftUI would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SFSymbols with Custom Weight in SwiftUI DESCRIPTION: Demonstrates how to use SFSymbols for icons in SwiftUI, including the ability to customize the font weight. This allows for more flexible and visually consistent icon usage. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_2 LANGUAGE: swift CODE: ``` Image(systemName: show ? "slash.circle.fill" : "slash.circle") .font(Font.title.weight(.semibold)) ``` ---------------------------------------- TITLE: Drawing Paths and Shapes in SwiftUI DESCRIPTION: Explains how to draw custom paths and shapes using SwiftUI's drawing APIs. This is essential for creating custom UI elements, charts, or visual effects. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_8 LANGUAGE: swift CODE: ``` // Code for Drawing Paths and Shapes would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: SwiftUI Weather App with MVVM and CoreML DESCRIPTION: Details a SwiftUI weather application that utilizes the MVVM (Model-View-ViewModel) architectural pattern and integrates Core ML for potential machine learning features, such as weather prediction. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_23 LANGUAGE: swift CODE: ``` // Code for SwiftUI Weather App with MVVM and CoreML would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: State Management with @State in SwiftUI DESCRIPTION: Illustrates the use of the `@State` property wrapper in SwiftUI for managing local view state. Changes to a `@State` variable automatically trigger UI updates. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_3 LANGUAGE: swift CODE: ``` @State var show = false ``` ---------------------------------------- TITLE: Spring Animation in SwiftUI DESCRIPTION: Applies a spring animation to view changes, providing a natural and bouncy feel. This is often used in conjunction with state changes to animate transitions smoothly. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_1 LANGUAGE: swift CODE: ``` .animation(.spring()) ``` ---------------------------------------- TITLE: Time Travel Feature in SwiftUI DESCRIPTION: Demonstrates a 'Time Travel' feature, likely involving state management and animation to allow users to navigate through historical states of an application or data. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_7 LANGUAGE: swift CODE: ``` // Code for Time Travel feature would go here. // This snippet is a placeholder for the actual implementation. ``` ---------------------------------------- TITLE: 3D Rotation Effect in SwiftUI DESCRIPTION: Applies a 3D rotation effect to a view, commonly used for card-like interfaces. The rotation is conditional based on a state variable (`dragState.isActive`). SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/README.md#_snippet_0 LANGUAGE: swift CODE: ``` .rotation3DEffect( Angle(degrees: dragState.isActive ? 0 : 60), axis: (x: 10.0, y: 10.0, z: 10.0) ) ``` ---------------------------------------- TITLE: React cloneElement Example DESCRIPTION: This snippet shows how to use React's cloneElement to pass additional styles to a child component. It involves a parent component rendering a child with cloned content that includes new styling. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/CloneElement.md#_snippet_0 LANGUAGE: jsx CODE: ``` import React from "react"; export function PageElement() { return (
This is the page content
); } export function LayoutElement({ children }) { return (
This is the layout {React.cloneElement(children, { style: { fontWeight: "bold", fontSize: "2rem" } })}
); } ``` ---------------------------------------- TITLE: SwiftUI Children Composition (Text Specific) DESCRIPTION: An example of passing a `Text` view as content to a SwiftUI `Layout` struct. Currently, this implementation is limited to accepting only `Text` views and the author is seeking a way to generalize it to accept any view type. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/Children.md#_snippet_1 LANGUAGE: swift CODE: ``` import SwiftUI struct Page : View { var body: some View { Layout() { Text("This is the page content") } } } struct Layout : View { var content: () -> Text var body: some View { VStack { Text("This is the layout") content() } } } ``` ---------------------------------------- TITLE: React State Management Example DESCRIPTION: This React code demonstrates a parent component (ContentView) managing state (count) and passing it along with a function (increment) to a child component (ChildView). The child component displays the count and triggers the increment function. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/State.md#_snippet_0 LANGUAGE: jsx CODE: ``` import React from "react"; export function ContentView() { const [count, setCount] = useState(0); function increment() { setCount(count + 1); } return (
Press the button below
); } export function ChildView({ count, increment }) { return (
{coun t}
); } ``` ---------------------------------------- TITLE: SwiftUI State Management with Callbacks DESCRIPTION: This SwiftUI example shows a parent view (ContentView) managing state (count) and passing it to a child view (ChildView) along with a callback function (increment) to modify the state. The child view displays the count and calls the increment function when a button is pressed. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/State.md#_snippet_1 LANGUAGE: swift CODE: ``` import SwiftUI struct ContentView : View { @State var count = 0 var body: some View { VStack(spacing: 1.0) { Text("Press the button below") ChildView( counter: count, increment: increment ) } } func increment() { count += 1 } } struct ChildView : View { var counter: Int var increment: () -> Void var body: some View { VStack { Text("\(counter)") Button(action: increment) { Text("Increment") } } } } ``` ---------------------------------------- TITLE: SwiftUI State Management with @Binding DESCRIPTION: This SwiftUI example demonstrates using the @Binding property wrapper to allow a child view (ChildView) to directly modify the state managed by its parent view (ContentView). The parent passes a binding to its @State variable, enabling two-way data flow. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/State.md#_snippet_2 LANGUAGE: swift CODE: ``` import SwiftUI struct ContentView : View { @State var count = 0 var body: some View { VStack(spacing: 1.0) { Text("Press the button below") ChildView( counter: $count ) } } } struct ChildView : View { @Binding var counter: Int func increment () { counter += 1 } var body: some View { VStack { Text("\(counter)") Button(action: increment) { Text("Increment") } } } } ``` ---------------------------------------- TITLE: Swift Codestyle Marks DESCRIPTION: Demonstrates the use of 'MARK' comments in Swift for organizing code within classes, such as for initialization, lifecycle methods, layout, and helpers. It also mentions the possibility of adding new marks if needed. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/CONTRIBUTING.md#_snippet_0 LANGUAGE: swift CODE: ``` class Example { // MARK: - Init init() {} } ``` ---------------------------------------- TITLE: React Context Implementation DESCRIPTION: Demonstrates how to create and use context in React using React.createContext and useContext. It shows a simple session context with a name property. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/Context.md#_snippet_0 LANGUAGE: jsx CODE: ``` import React, { useContext } from "react"; const Session = React.createContext({ name: "" }); export function ContextProvider() { return ( ); } export function Parent() { return ; } export function Child() { const session = useContext(Session); return Hello {session.name}; } ``` ---------------------------------------- TITLE: SplitView Implementation DESCRIPTION: Provides a SwiftUI view wrapper for UISplitViewController, allowing for its management within a SwiftUI environment. This enables the creation of master-detail interfaces. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/SplitView/README.md#_snippet_0 LANGUAGE: swift CODE: ``` import SwiftUI import UIKit struct SplitView: UIViewControllerRepresentable { var masterView: AnyView var detailView: AnyView func makeUIViewController(context: Context) -> UISplitViewController { let splitViewController = UISplitViewController() splitViewController.delegate = context.coordinator let masterNavController = UINavigationController(rootView: masterView) let detailNavController = UINavigationController(rootView: detailView) splitViewController.viewControllers = [masterNavController, detailNavController] splitViewController.preferredDisplayMode = .oneBesideSecondary return splitViewController } func updateUIViewController(_ splitViewController: UISplitViewController, context: Context) { // Update view controllers if needed } func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, UISplitViewControllerDelegate { var parent: SplitView init(_ parent: SplitView) { self.parent = parent } func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool { // Return true to collapse the secondary view controller onto the primary true } } } struct SplitView_Previews: PreviewProvider { static var previews: some View { SplitView(masterView: AnyView(Text("Master")), detailView: AnyView(Text("Detail"))) } } ``` ---------------------------------------- TITLE: React componentDidMount/componentWillUnmount Equivalent DESCRIPTION: This snippet demonstrates how to achieve the equivalent of React's componentDidMount and componentWillUnmount using the useEffect hook. It logs messages when a component mounts and unmounts, mirroring the lifecycle behavior. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/ComponentDidMount.md#_snippet_0 LANGUAGE: jsx CODE: ``` import React from ""; export function Counter() { const [count, setCount] = React.useState(0); function increment() { setCount(count + 1); } return (
{(count < 10 || count > 12) && }
); } export function ChildCounter({ count }) { React.useEffect(() => { console.log("mounted"); return () => { console.log("unmounting"); }; }, []); return
{count}
; } ``` ---------------------------------------- TITLE: Swift Package for SF Symbols Enum DESCRIPTION: This Swift Package converts SF Symbols from strings to an enum, making them easier to use in SwiftUI. It was developed by simjp and announced on Twitter. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/SFSymbols/README.md#_snippet_0 LANGUAGE: swift CODE: ``` https://twitter.com/simjp/status/1135936933916336128 ``` ---------------------------------------- TITLE: React Children Composition DESCRIPTION: Demonstrates a common pattern in React for passing children to a layout component. The `Layout` component accepts `children` as a prop and renders them within its own structure. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/Children.md#_snippet_0 LANGUAGE: jsx CODE: ``` import React from "react"; function Page() { return ( This is the page content ); } function Layout({ children }) { return (
This is the layout {children}
); } ``` ---------------------------------------- TITLE: SwiftUI EnvironmentObject for Context DESCRIPTION: Shows how to implement a context pattern in SwiftUI using a class conforming to BindableObject and the @EnvironmentObject property wrapper. This allows child views to access and react to changes in the shared object. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/Context.md#_snippet_1 LANGUAGE: swift CODE: ``` import SwiftUI import Combine final class Session: BindableObject { let didChange = PassthroughSubject() var name: String { didSet { didChange.send(self) } } init(name: String) { self.name = name } } struct ContextProvider : View { var body: some View { Parent().environmentObject(Session(name: "Bento")) } } struct Parent : View { var body: some View { Child() } } struct Child : View { @EnvironmentObject var session: Session var body: some View { Text("Hello \(session.name)") } } ``` ---------------------------------------- TITLE: SwiftUI Equivalent of cloneElement DESCRIPTION: This snippet demonstrates the SwiftUI approach to achieve the functionality of React's cloneElement. It shows how to pass content to a child view and apply modifiers to it, mimicking the attribute injection. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/CloneElement.md#_snippet_1 LANGUAGE: swift CODE: ``` import SwiftUI struct PageElement : View { var body: some View { LayoutElement() { Text("This is the page content") } } } struct LayoutElement : View { var content: () -> Text var body: some View { VStack { Text("This is the layout") content() .bold() .font(.largeTitle) } } } ``` ---------------------------------------- TITLE: SwiftUI onAppear/onDisappear for View Lifecycle DESCRIPTION: This Swift code shows how to use the onAppear and onDisappear modifiers in SwiftUI to manage view lifecycle events, similar to React's componentDidMount. It logs messages when a child view appears and disappears based on a condition in the parent. SOURCE: https://github.com/ivanvorobei/swiftui/blob/master/Other Projects/React Meets SwiftUI/ComponentDidMount.md#_snippet_1 LANGUAGE: swift CODE: ``` import SwiftUI struct Counter : View { @State var count = 0 func increment () { count += 1 } var body: some View { VStack { Button(action: increment) { Text("Increment") } if count < 10 || count > 12 { ChildCounter(count: count) } } } } struct ChildCounter : View { var count: Int func onMount () { print("on mount") } func onUnmount () { print("on unmount") } var body: some View { Text("\(count)") .onAppear(perform: onMount) .onDisappear(perform: onUnmount) } } ``` LANGUAGE: swift CODE: ``` struct Counter : View { @State var count = 0 func increment () { count += 1 } func mount () { print("Child mount") } func unmount () { print("Child unmount") } func parentMount () { print("parent mount") } var body: some View { VStack { Button(action: increment) { Text("Increment") } if count < 10 || count > 12 { ChildCounter(count: count) .onAppear(perform: mount) .onDisappear(perform: unmount) } } .onAppear(perform: parentMount) } } ```