Skip to content

Instantly share code, notes, and snippets.

@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",
@Tulakshana
Tulakshana / Beacon.swift
Last active October 11, 2018 22:31
Using this class you can make an iPhone behave like an iBeacon :)
import Foundation
import CoreBluetooth
import CoreLocation
class Beacon: NSObject {
static let shared = Beacon()
var localBeacon: CLBeaconRegion!
var beaconPeripheralData: [String: AnyObject]?
@Tulakshana
Tulakshana / CustomSlider.swift
Last active October 9, 2024 07:20
A subclass of UISlider which has a tool tip indicating the current value of the slider.
import UIKit
class CustomSlider: UISlider {
private var toolTip: ToolTipPopupView?
override func awakeFromNib() {
super.awakeFromNib()
self.initToolTip()
@Tulakshana
Tulakshana / MovieTransitionsVC.swift
Last active October 14, 2024 18:43 — forked from SheffieldKevin/movietransitions.swift
Make a movie with transitions with AVFoundation and swift. Add an UIActivityIndicatorView and an UIButton and connect appropriately. You will also need to attach few videos to the project.
//
// MovieTransitionsVC.swift
// VideoAnimations
//
// Created by Tula on 2018-06-14.
// Copyright © 2018 Tula. All rights reserved.
//
import UIKit
import AVFoundation
@Tulakshana
Tulakshana / ExitHandler.swift
Created May 11, 2018 21:50
Experimental code to exit an iOS app.
@IBAction func btnExit0Tapped (sender: UIButton) {
exit(0) // Not recommended by Apple https://developer.apple.com/library/content/qa/qa1561/_index.html and app crashes
}
@IBAction func btnSuspendTapped (sender: UIButton) {
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil) // App exits gracefully
}
@IBAction func btnFatalErrorTapped (sender: UIButton) {
fatalError() // App will crash
@Tulakshana
Tulakshana / StringReplace.swift
Last active November 22, 2017 15:55
This String extension contains a convenient method to replace characters in a CharacterSet with a given String.
func stringByReplacing(charSet: CharacterSet, with string: String) -> String {
let components = self.components(separatedBy: charSet)
return components.joined(separator: string)
}
@Tulakshana
Tulakshana / UIViewFirstResponder.swift
Created November 16, 2017 10:25
An UIView extension with a method to return the frame of the first responder.
extension UIView {
func frameOfFirstResponder() -> CGRect? {
if self.isFirstResponder {
return self.frame
}
for view in self.subviews {
if let frame = view.frameOfFirstResponder() {
return frame
}
@Tulakshana
Tulakshana / CustomButton.swift
Created November 13, 2017 12:55
A custom UIButton which could have different background colours for enabled and disabled states.
class CustomButton: UIButton {
@IBInspectable private var enabledBackgroundColor: UIColor?
@IBInspectable private var disabledBackgroundColor: UIColor?
override var isEnabled: Bool {
set {
if newValue {
self.backgroundColor = enabledBackgroundColor
} else {
self.backgroundColor = disabledBackgroundColor