Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Tulakshana's full-sized avatar

Tula Tulakshana

View GitHub Profile
@Tulakshana
Tulakshana / smartZip.sh
Created April 7, 2022 02:42
Script to create a zip file in macOS without .DS_Store and __MACOSX files. Run this script inside the folder you want to zip.
zip -r smart.zip . -x "*.DS_Store" -x "__MACOSX"
@Tulakshana
Tulakshana / androidDockerFile
Last active March 13, 2022 08:31
Docker steps to install packages using sdkmanager (Android)
ENV ANDROID_HOME_DIR=/opt/tools/android-sdk-linux
ENV SDK_MANAGER=$ANDROID_HOME/cmdline-tools/bin/sdkmanager
ENV TEMP_DIR=<Path to a temporary directory you download files to. e.g. /tmp/install>
ENV ANDROID_TOOLS_VERSION=30.0.3
ENV ANDROID_BUILD_VERSION=32
ADD <path to SDK tools. Refer command line tools in https://developer.android.com/studio#downloads> $TEMP_DIR/
RUN mkdir -p $ANDROID_HOME_DIR && \
cd $ANDROID_HOME_DIR && \
unzip $TEMP_DIR/<zip file name downloaded above> && \
@Tulakshana
Tulakshana / TouchDetectingView.swift
Created November 18, 2021 15:52
A sub class of UIView with delegate methods to detect pan gestures with direction and displacement.
import UIKit
protocol TouchDetectingViewDelegate: NSObjectProtocol {
func touchDetectingViewDidDetectPan(view: TouchDetectingView,
direction: TouchDetectingView.Direction,
displacement: CGFloat,
state: UIGestureRecognizer.State)
func touchDetectingViewPanEnded(view: TouchDetectingView)
}
@Tulakshana
Tulakshana / VideoHelper.swift
Created August 7, 2019 21:33
Use this to save a video to an album in the phone gallery. The method creates the album if it doesn't exists.
import Photos
class VideoHelper {
private static func createAlbum(name: String, completion: @escaping ((_ collection: PHAssetCollection?) -> Void)) {
//Get PHFetch Options
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", name)
let collection : PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
//Check return value - If found, then get the first album out
@Tulakshana
Tulakshana / NSAttributedString+Image.swift
Created June 6, 2019 18:41
Generate an UIImage from a NSAttributedString
import UIKit
extension NSAttributedString {
func image(size: CGSize) -> UIImage? {
UIGraphicsBeginImageContext(size)
self.draw(at: CGPoint.zero)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
@Tulakshana
Tulakshana / Dictionary.playground
Last active March 12, 2019 13:29
At one of the iOS developer interviews I went, I was asked to replicate the functionality of a Dictionary. I thought I would share what I came up with. Improvements are most welcome.
import Foundation
// This class simulates SWift's dictionary class
class MyDictionary <Key: Equatable, Value> {
private var values: [Value] = []
private var keys: [Key] = []
func set(value: Value, key: Key) {
if keys.contains(key), let index = keys.firstIndex(of: key) {
values[index] = value
@Tulakshana
Tulakshana / NSView+Color.swift
Created January 29, 2019 00:46
NSView's missing setter for background colour
extension NSView {
func setBackgroundColor(color: NSColor) {
let layer = CALayer.init()
layer.backgroundColor = color.cgColor
self.layer = layer
}
}
@Tulakshana
Tulakshana / WDMainActivity.java
Last active January 21, 2019 17:33
Create peer-to-peer connections over Wi-Fi (Android). Explained in more detailed at https://the-useful.blogspot.com/2019/01/create-peer-to-peer-connections-over-wi.html
public class WDMainActivity extends AppCompatActivity implements WifiP2pManager.ConnectionInfoListener {
private static final String TAG = "WDMainActivity";
private static final String KEY_BUDDY_NAME = "buddyname";
private final IntentFilter intentFilter = new IntentFilter();
private WifiP2pManager.Channel mChannel;
private WifiP2pManager mManager;
@Tulakshana
Tulakshana / File.swift
Created December 17, 2018 17:32
A helper function to update the modified date of a file
static func update(modifiedDate: Date, url: URL) {
do {
var values = try url.resourceValues(forKeys: [URLResourceKey.contentModificationDateKey])
values.contentModificationDate = modifiedDate
try url.setResourceValues(values)
} catch {
print(error.localizedDescription)
}
}
@Tulakshana
Tulakshana / Data+File.swift
Created November 14, 2018 23:38
An extension for Data to get the file extension
extension Data {
private static let mimeTypeSignatures: [UInt8 : String] = [
0xFF : "image/jpeg",
0x89 : "image/png",
0x47 : "image/gif",
0x49 : "image/tiff",
0x4D : "image/tiff",
0x25 : "application/pdf",
0xD0 : "application/vnd",
0x46 : "text/plain",