Skip to content

Instantly share code, notes, and snippets.

View ryanmasondavies's full-sized avatar

Ryan Mason-Davies ryanmasondavies

  • Marks and Spencer
  • Hampshire, UK
View GitHub Profile
@ryanmasondavies
ryanmasondavies / ManagedObjectTableController.swift
Created September 8, 2018 21:47
TableController for Core Data managed objects of any type. Represents a single cell type.
class TableController<EntityType: NSManagedObject>: NSObject, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {
let tableView: UITableView
let managedObjectContext: NSManagedObjectContext
let fetchRequest: NSFetchRequest<EntityType>
let cellIdentifier: String
let configureCell: (UITableViewCell, EntityType) -> Void
init(tableView: UITableView, managedObjectContext: NSManagedObjectContext, fetchRequest: NSFetchRequest<EntityType>, cellIdentifier: String, configureCell: @escaping (UITableViewCell, EntityType) -> Void) {
self.tableView = tableView
self.managedObjectContext = managedObjectContext
struct Book {
let title: String
}
protocol SegueHandling {
func handle(segue: UIStoryboardSegue) -> Bool
}
struct SegueHandler<ViewControllerType>: SegueHandling {
let identifier: String
@ryanmasondavies
ryanmasondavies / freshbooks_round_hours.js
Created May 3, 2017 17:15
On the 'new invoice' page, run this JS to round hours.
var nodes = document.getElementsByName("qty[]");
var index;
for (index = 0; index < nodes.length; ++index) {
var node = nodes[index];
if (node.getAttribute("title") == "Task hours") {
var nonRoundedValueAsString = node.getAttribute("value");
var nonRoundedValue = Number(nonRoundedValueAsString);
var roundedValue = (Math.round(nonRoundedValue * 4) / 4).toFixed(2);
console.log(nonRoundedValue + " => " + roundedValue);
node.value = roundedValue.toString();
@ryanmasondavies
ryanmasondavies / AppDelegate.swift
Created January 23, 2016 20:30
App delegate without storyboards
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var window: UIWindow? = {
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.backgroundColor = UIColor.whiteColor()
window.rootViewController = ViewController()
return window
}()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
@ryanmasondavies
ryanmasondavies / LoadingView.h
Created January 13, 2016 16:28
Generic loading view: UIView with centred UIActivityViewIndicator
#import <UIKit/UIKit.h>
@interface LoadingView : UIView
@property (nonatomic, weak, readonly) UILabel *loadingLabel;
@property (nonatomic, weak, readonly) UIActivityIndicatorView *activityIndicator;
@end
@ryanmasondavies
ryanmasondavies / playlist.rb
Created January 13, 2016 16:27
Rename mp3 files for burning to disk (for playing in car stereo)
path = ‘/path/to/music/folder’
puts "Renaming files..."
Dir.foreach(path) do |item|
next if item == '.' or item == '..'
new_name = File.basename(item, ".*")
# car works better with a-z 0-9 A-Z only
@ryanmasondavies
ryanmasondavies / apple_watch.sh
Last active January 13, 2016 16:26
Update build number from Subversion
# Needs to be placed after the usual versioning script in the WatchKit extension to copy the extension’s build number over to the app.
extension_plist="${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
extension_build_path="${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}"
app_name=`cd "$extension_build_path"; ls -d *app`
app_plist="${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}/$app_name/Info.plist"
extension_build_number=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$extension_plist")
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $extension_build_number" "$app_plist"
if [ ${CONFIGURATION} == "AppStore" ]; then
buildNumber=$(git rev-list HEAD | wc -l | tr -d ' ')
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/AppName/AppName-Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/AppName WatchKit App/Info.plist"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/AppName WatchKit Extension/Info.plist"
fi;
if [ ${CONFIGURATION} == "Release" ]; then
buildNumber=$(git rev-list HEAD | wc -l | tr -d ' ')
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/AppName/AppName-Info.plist"
@ryanmasondavies
ryanmasondavies / SearchDisplayController.swift
Created March 19, 2015 18:48
Custom subclass of UISearchDisplayController that overrides the default behaviour of hiding the navigation bar when becoming active.
import UIKit
/** Custom subclass of UISearchDisplayController that overrides the default behaviour of hiding the navigation bar when becoming active. */
class SearchDisplayController: UISearchDisplayController {
override func setActive(visible: Bool, animated: Bool) {
if self.active == visible {
return
}
searchContentsController.edgesForExtendedLayout = .Bottom
@ryanmasondavies
ryanmasondavies / absolute_radio_poller.rb
Last active August 29, 2015 14:16
This is a script for retrieving the songs played by Absolute Radio from their main website. It writes the songs out to a log and opens up a browser with an alert if they play the same song twice. I wrote this to test their 'no repeat guarantee'. They weren't lying!
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'date'
require 'fileutils'
def show_alert
File.open('alert_file.html', 'w') do |file|
file.puts "<html><head><title>PAY ATTENTION</title></head><body onload=\"alert('They played the same song! Check the logs!');\"></body></html>"
end