Skip to content

Instantly share code, notes, and snippets.

@blork
blork / extensions.m
Last active May 20, 2021 14:25
Dealing with various extension data types
NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
// Shared plain text is stored here. Content varies wildly based on app.
NSString *sharedPlainText = [item.attributedContentText string];
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList
options:nil
completionHandler:^(NSDictionary *item, NSError *error) {
@blork
blork / Prrprocessor.js
Created September 5, 2014 19:58
iOS Extension Web Page Preprocessor
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
// Pass the baseURI of the webpage to the extension.
arguments.completionFunction({"URL": document.URL, "pageSource": document.documentElement.outerHTML, "title": document.title, "selection": window.getSelection().toString()});
}
};
// The JavaScript file must contain a global object named "ExtensionPreprocessingJS".
var ExtensionPreprocessingJS = new MyPreprocessor;
@blork
blork / Debouncer.swift
Last active March 20, 2019 14:36
Simple Debouncer in Swift 4
//
// Debouncer.swift
//
// Created by Sam Oakley on 20/03/2019.
//
import Foundation
class Debouncer {
package ...;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
@blork
blork / SquareImageView.java
Created May 21, 2013 13:19
Android imageview subclass which maintains a square aspect ratio.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell.accessoryView isKindOfClass:[UITextField class]] && CGRectIsEmpty(cell.accessoryView.frame)) {
cell.accessoryView.frame = CGRectMake(cell.accessoryView.bounds.origin.x, cell.accessoryView.bounds.origin.y, CGRectGetWidth(cell.frame) / 2, CGRectGetHeight(cell.frame));
cell.accessoryView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
} else {
//[cell.accessoryView sizeToFit];
}
}
+ (void) insertOrUpdate:(NSArray*)dictArray
forUniqueKey:(NSString*)key
withBlock:(void (^) (NSDictionary* dictionary, NSManagedObject* object))block
inStore:(Store *) store
error:(NSError*)error
{
NSManagedObjectContext* context = [store privateContext];
__block NSError *localError = nil;
[context performBlockAndWait:^{
/*
* TypefaceTextView.java
* Simple
*
* Copyright 2012 Simple Finance Corporation (https://www.simple.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
@blork
blork / dp2px.java
Created May 21, 2013 14:49
Convert DP (display independent pixels) to pixels.
public static int dp2px(final float dps, final Resources res) {
final float scale = res.getDisplayMetrics().density;
final int pixels = (int) ((dps * scale) + 0.5f);
return pixels;
}
@blork
blork / isIntentAvailable.java
Created May 21, 2013 13:54
Check if an intent is available.
public static boolean isIntentAvailable(Context context, Intent intent) {
final PackageManager packageManager = context.getPackageManager();
List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) {
return true;
}
return false;
}