How to login with Apple in SwiftUI

Issue #808 Make SignInWithAppleButton Wrap ASAuthorizationAppleIDButton inside UIViewRepresentable import SwiftUI import UIKit import AuthenticationServices struct SignInWithAppleButton: View { @Environment(\.colorScheme) private var colorScheme: ColorScheme var body: some View { ButtonInside(colorScheme: colorScheme) .frame(width: 280, height: 45) } } private struct ButtonInside: UIViewRepresentable { let colorScheme: ColorScheme func makeUIView(context: Context) -> ASAuthorizationAppleIDButton { switch colorScheme { case .dark: return ASAuthorizationAppleIDButton(type: .signIn, style: .white) default: return ASAuthorizationAppleIDButton(type: .signIn, style: .black) } } func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) { // No op } } struct SignInWithAppleButton_Previews: PreviewProvider { static var previews: some View { SignInWithAppleButton() } } Handle logic in ViewModel import SwiftUI import AuthenticationServices import Resolver import Firebase import FirebaseAuth final class AuthViewModel: NSObject, ObservableObject { enum RequestState: String { case signIn case link case reauth } let firebaseService: FirebaseService var window: UIWindow?...

June 25, 2021 路 3 min 路 Khoa Pham

How to use SwiftLint in SPM project

Issue #807 Don鈥檛 add SwiftLint via SPM, but just add a Run Script Build phrase if which swiftlint >/dev/null; then swiftlint else echo "warning: SwiftLint not installed, download from" fi Install swiftlint locally brew install swiftlint Add .swiftlint.yml with some initial disabled rules. Gradually opt-in rules to have more checks included: - MyApp - MyAppTests excluded: - Tests/SwiftLintFrameworkTests/Resources analyzer_rules: - unused_declaration - unused_import disabled_rules: - line_length - trailing_whitespace - vertical_whitespace_opening_braces - vertical_whitespace_closing_braces - void_return - identifier_name - file_name - number_separator - sorted_imports - unneeded_parentheses_in_closure_argument - redundant_optional_initialization - todo - first_where - closure_spacing - vertical_parameter_alignment_on_call - unused_optional_binding - operator_usage_whitespace - large_tuple - vertical_whitespace - multiple_closures_with_trailing_closure - control_statement - statement_position - closure_end_indentation - opening_brace - empty_count - type_body_length - force_try - force_cast opt_in_rules: - anyobject_protocol - array_init - attributes - collection_alignment - contains_over_filter_count - contains_over_filter_is_empty - contains_over_first_not_nil - contains_over_range_nil_comparison - discouraged_none_name - discouraged_object_literal - empty_collection_literal - empty_string - empty_xctest_method - enum_case_associated_values_count - explicit_init - extension_access_modifier - fallthrough - fatal_error_message - file_header - flatmap_over_map_reduce - identical_operands - joined_default_parameter - last_where - legacy_multiple - legacy_random - literal_expression_end_indentation - lower_acl_than_parent - modifier_order - nimble_operator - nslocalizedstring_key - number_separator - object_literal - overridden_super_call - override_in_extension - pattern_matching_keywords - prefer_self_type_over_type_of_self - private_action - private_outlet - prohibited_interface_builder - prohibited_super_call - quick_discouraged_call - quick_discouraged_focused_test - quick_discouraged_pending_test - reduce_into - redundant_nil_coalescing - redundant_type_annotation - single_test_class - sorted_first_last - static_operator - strong_iboutlet - test_case_accessibility - toggle_bool - unavailable_function - unowned_variable_capture - untyped_error_in_catch - xct_specific_matcher - yoda_condition file_name: excluded: - Constants....

June 24, 2021 路 2 min 路 Khoa Pham

How to show modal window in AppKit

Issue #806 Use runModal Blocks main queue A loop that uses this method is similar in some ways to a modal event loop run with runModal(for:), except with this method your code can do some additional work between method invocations. When you invoke this method, events for the NSWindow object of this session are dispatched as normal. This method returns when there are no more events. You must invoke this method frequently enough in your loop that the window remains responsive to events....

June 24, 2021 路 1 min 路 Khoa Pham

How to perform action during long press in SwiftUI

Issue #805 Use LongPressGesture to detect when long-press gesture has been recognized, and chain with DragGesture to check when pressing still occurs Image(systemName: SFSymbol.deleteLeft.rawValue) .imageScale(.medium) .onTapGesture { onKeyCommand(.backspace) } .gesture( LongPressGesture() .onEnded { _ in self.timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { _ in self.onKeyCommand(.backspace) } } .sequenced( before: DragGesture(minimumDistance: 0) .onEnded { _ in self.timer?.invalidate() self.timer = nil } ) )

June 20, 2021 路 1 min 路 Khoa Pham

How to fallback img in React

Issue #804 <img src={user.avatarUrl} onError={(e) => { = null; = "/images/default.png" }} /> Extract to React Component interface Props { src: string className: string fallback: string } export default function FallbackImg(props: Props) { return ( <img className={props.className} src={props.src} onError={(e) => { // @ts-ignore = props.fallback }} /> ) } How to fallback backgroundImage Use 2 url var el = document.createElement('div') el.className = 'marker' = `url('${user.avatarUrl}'), url('/images/user....

May 26, 2021 路 1 min 路 Khoa Pham

How to use videojs in React

Issue #803 import React from 'react'; import videojs from 'video.js' import 'video.js/dist/video-js.css'; export default class VideoPlayer extends React.Component { createPlayer() { // instantiate Video.js this.player = videojs(this.videoNode, this.props, function onPlayerReady() { //console.log('onPlayerReady', this) }); } disposePlayer() { if (this.player) { this.player.dispose() } } componentDidMount() { this.createPlayer() } componentWillUnmount() { this.disposePlayer() } componentDidUpdate(prevProps) { if (this.props.sources != prevProps.sources) { this.player.src(this.props.sources[0]) } } // wrap the player in a div with a `data-vjs-player` attribute // so videojs won't create additional wrapper in the DOM // see https://github....

May 20, 2021 路 1 min 路 Khoa Pham

How to use FontAwesome 5 in Nextjs

Issue #802 npm i --save @fortawesome/fontawesome-svg-core \ @fortawesome/free-solid-svg-icons \ @fortawesome/free-brands-svg-icons \ @fortawesome/react-fontawesome import { faApple, faGithub, faTwitter } from '@fortawesome/free-brands-svg-icons' import { faUser, faPowerOff, faAngleDown } from '@fortawesome/free-solid-svg-icons' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' <FontAwesomeIcon icon={faCoffee} />

May 19, 2021 路 1 min 路 Khoa Pham

How to use Bulma in Nextjs

Issue #801 Read npm install bulma sass In styles/index.scss @import '~bulma/bulma'; In _app.tsx import '../styles/index.scss'

May 19, 2021 路 1 min 路 Khoa Pham

Recommended resource for learning SwiftUI

Issue #800 I鈥檝e done some #SwiftUI lately for Push Hero and PastePal, and amazed by how easy and fun it is to make apps. If you want to get into SwiftUI but don鈥檛 know where to start, here are my recommended resources Twitter Thread reader

April 30, 2021 路 1 min 路 Khoa Pham

How to make your apps stand out on the AppStore

Issue #799 Besides coming up with a good idea and building the app, there are many other things you can do to boost your apps' visibility Twitter thread Thread reader

April 30, 2021 路 1 min 路 Khoa Pham

How to use SwiftGen and LocalizedStringKey in SwiftUI

Issue #798 swiftgen.yml strings: inputs: PastePal/Resources/Localizable/en.lproj outputs: - templatePath: swiftgen-swiftui-template.stencil output: PastePal/Resources/Strings.swift Template from swiftgen-swiftui-template.stencil // Generated using SwiftGen 鈥 {% if tables.count > 0 %} import SwiftUI // MARK: - Strings {% macro parametersBlock types %}{% filter removeNewlines:"leading" %} {% for type in types %} {% if type == "String" %} _ p{{forloop.counter}}: Any {% else %} _ p{{forloop.counter}}: {{type}} {% endif %} {{ ", " if not forloop....

April 27, 2021 路 2 min 路 Khoa Pham

How to localize SwiftUI projects

Issue #797 Reference

April 23, 2021 路 1 min 路 Khoa Pham

How to use ForEach with indices in SwiftUI

Issue #796 One seemingly obvious way to use ForEach on array with indices is using enumerated struct Book: Hashable, Identifiable { let id: UUID let name: String } struct BooksView: View { let books: [Book] var body: some View { List { ForEach(Array(books.enumerated()), id: \.element) { index, book in Text( .background(index % 2 == 0 ? : } } } } Reading the documentation for enumerated closely When you enumerate a collection, the integer part of each pair is a counter for the enumeration, but is not necessarily the index of the paired value....

April 20, 2021 路 2 min 路 Khoa Pham

How to resize NSImage with padding

Issue #795 extension NSImage { func resize(width: CGFloat, height: CGFloat, padding: CGFloat) -> NSImage { let img = NSImage(size: CGSize(width: width, height: height)) img.lockFocus() let ctx = NSGraphicsContext.current ctx?.imageInterpolation = .high self.draw( in: NSMakeRect(0, 0, width, height), from: NSMakeRect(0, -padding, size.width, size.height - padding), operation: .copy, fraction: 1 ) img.unlockFocus() return img } } So we can use like button.image = NSImage(named: NSImage.Name("pastePal"))?.resize(width: 22, height: 22, padding: -4)

April 15, 2021 路 1 min 路 Khoa Pham

How to convert from paid to freemium in SwiftUI with RevenueCat

Issue #794 I鈥檝e been distributing my apps PastePal and Push Hero as a paid upfront apps on Appstore. I鈥檝e quickly come to realize the importance of trials since many have requested a tryout before purchasing. Manual sending out trials comes with its own problem. I need to notarize and package the app as a dmg file, and not to even mention implement my own trial mechanism to check and notify of expired builds....

April 11, 2021 路 5 min 路 Khoa Pham

How to repeat array of object in Swift

Issue #793 To create array containing number of repeated value in Swift, we can use Array.init(repeating:count:) let fiveZs = Array(repeating: "Z", count: 5) print(fiveZs) // Prints "["Z", "Z", "Z", "Z", "Z"]" However, if we read Collection Types guide about Creating an Array with a Default Value Swift鈥檚 Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value....

March 22, 2021 路 2 min 路 Khoa Pham

How to use dynamic color in iOS

Issue #792 iOS 13 introduced Dark Mode with User Interface Style that makes it easy to support dark and light theme in our apps. Before we dive in, here are some official resources WWDC 2019 Implementing Dark Mode on iOS Supporting Dark Mode in Your Interface Choosing a Specific Interface Style for Your iOS App Adaptive color Like adaptive layout that adapts to different screen sizes, adaptive colors adapt to different user interface styles, for now they are light and dark mode....

March 13, 2021 路 7 min 路 Khoa Pham

How to use View protocol in SwiftUI

Issue #791 SwiftUI has View protocol which represents part of your app鈥檚 user interface and provides modifiers that you use to configure views. You create custom views by declaring types that conform to the View protocol. Implement the required body computed property to provide the content for your custom view. A typical example of View is using struct enum HeroType { case melee case range } struct ContentView: View { let heroType: HeroType var body: some View { switch heroType { case ....

March 10, 2021 路 1 min 路 Khoa Pham

How too save image to Photo library in iOS

Issue #790 Use UIImageWriteToSavedPhotosAlbum Adds the specified image to the user鈥檚 Camera Roll album. Let鈥檚 make an NSObject delegate class so we can perform target action to notify about completion import UIKit struct ImageService { final class Delegate: NSObject { let completion: (Error?) -> Void init(completion: @escaping (Error?) -> Void) { self.completion = completion } @objc func savedImage( _ im: UIImage, error: Error?, context: UnsafeMutableRawPointer? ) { DispatchQueue.main.async { self....

March 10, 2021 路 1 min 路 Khoa Pham

How to manage WindowGroup in SwiftUI for macOS

Issue #789 Using WindowGroup New in SwiftUI 2.0 for iOS 14 and macOS 11.0 is WindwGroup which is used in App protocol. WindowGroup is ideal for document based applications where you can open multiple windows for different content or files. For example if you鈥檙e developing a text editor or drawing apps, you can show multiple windows for different text file or drawing. All is handled automatically for you if you use WindowGroup...

March 7, 2021 路 6 min 路 Khoa Pham