Skip to content

Instantly share code, notes, and snippets.

@ChrisMash
ChrisMash / PlayerTimeObserver.swift
Created March 10, 2020 19:06
An observer for AVPlayer's currentTime that publishes it with Combine
import Combine
class PlayerTimeObserver {
let publisher = PassthroughSubject<TimeInterval, Never>()
private var timeObservation: Any?
init(player: AVPlayer) {
// Periodically observe the player's current time, whilst playing
timeObservation = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 0.5, preferredTimescale: 600), queue: nil) { [weak self] time in
guard let self = self else { return }
@ChrisMash
ChrisMash / PlayerTimeView.swift
Created March 10, 2020 19:09
A SwiftUI View to display an AVPlayer's currentTime from PlayerTimeObserver's publisher
import SwiftUI
import AVFoundation
struct PlayerTimeView: View {
let timeObserver: PlayerTimeObserver
@State private var currentTime: TimeInterval = 0
var body: some View {
Text("\(Utility.formatSecondsToHMS(currentTime))")
.onReceive(timeObserver.publisher) { time in
@ChrisMash
ChrisMash / VideoPlayer.swift
Last active June 27, 2021 14:00
iOS 14's new VideoPlayer View is here!
import SwiftUI
import AVKit
struct VideoView: View {
private let player = AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!)
var body: some View {
VideoPlayer(player: player)
.onAppear() {
// Start the player going, otherwise controls don't appear
@ChrisMash
ChrisMash / OCClassAndStruct.h
Created June 9, 2021 12:00
A very simple ObjC class and struct example
struct OCStruct {
NSInteger integer;
};
@interface OCClass : NSObject
- (NSInteger)getInt;
- (NSString* _Nonnull)intToStringWith:(NSInteger)integer;
- (NSString* _Nullable)tryIntToStringWith:(NSInteger)integer;
@ChrisMash
ChrisMash / SwiftClassAndStruct.swift
Last active June 9, 2021 12:05
A very simple Swift class and struct example
public struct SStruct {
public var integer: Int
}
public class SClass: NSObject {
@objc public func getInt() -> Int { ... }
@objc public func intToString(_ int: Int) -> String { ... }
@objc public func tryIntToString(_ int: Int) -> String? { ... }
}
@ChrisMash
ChrisMash / mailto.swift
Last active April 9, 2023 02:19
Send an email using the user's chosen default email app (e.g. Mail, Gmail, Outlook etc.) instead of using MFMailComposeViewController that requires the Mail app to be setup with an account
let address = "[email protected]"
let subject = "AwesomeApp Feedback"
// Example email body with useful info for bug reports
let body = "\n\nApp Version: \(appVersion)\nDevice: \(deviceType)\niOS: \(osVersion)"
// Build the URL from its components
var components = URLComponents()
components.scheme = "mailto"
components.path = address
// A representation of a playing card
struct Card: Identifiable, Equatable {
let id = UUID()
let number: Int
}
// A representation of a player's hand of cards
class Hand: ObservableObject, Identifiable {
let id = UUID()
@Published var cards = [Card]()
// A representation of the current state of the game,
// including which cards each player has and the deck
// of cards that can be picked up from
class GameState {
private(set) var deck = Hand()
private(set) var hands = [Hand]()
init() {
// Fill the deck with 20 cards, shuffled
// A view to represent a single card
struct CardView: View {
let card: Card
let namespace: Namespace.ID
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20,
style: .continuous)
struct CardView_Previews: PreviewProvider {
// Create a wrapper view that will let us hold a @Namespace to pass to the view
struct Wrapper: View {
@Namespace var animation
var body: some View {
CardView(card: Card(number: 1), namespace: animation)
.padding()
.background(Color.gray)
}
}