Skip to content

Instantly share code, notes, and snippets.

View DineshKachhot's full-sized avatar
🎯
Focusing

Dinesh Kachhot DineshKachhot

🎯
Focusing
View GitHub Profile
@DineshKachhot
DineshKachhot / Array+FilterUniqueByKey.swift
Created July 12, 2018 05:14
Array extension to return the unique list of objects based on a given key.
extension Array {
func unique<T:Hashable>(by: ((Element) -> (T))) -> [Element] {
var set = Set<T>() //Keep unique list in a Set for fast retrieval
var orderedArray = [Element]() //Keeping the unique list of elements but ordered
for value in self {
if !set.contains(by(value)) {
set.insert(by(value))
orderedArray.append(value)
}
}
@DineshKachhot
DineshKachhot / template.dart
Created August 30, 2018 06:20 — forked from nkraev/template.dart
This gist shows the GestureDetector usage on latest dart 2 preview version
import 'package:flutter/material.dart';
class SignaturePainter extends CustomPainter {
SignaturePainter(this.points);
final List<Offset> points;
void paint(Canvas canvas, Size size) {
Paint paint = new Paint()
..color = Colors.black
@DineshKachhot
DineshKachhot / time_ago_since_now.dart
Created March 1, 2019 06:08
Flutter Time ago calculator
static String timeAgoSinceDate(String dateString, {bool numericDates = true}) {
DateTime date = DateTime.parse(dateString);
final date2 = DateTime.now();
final difference = date2.difference(date);
if ((difference.inDays / 365).floor() >= 2) {
return '${(difference.inDays / 365).floor()} years ago';
} else if ((difference.inDays / 365).floor() >= 1) {
return (numericDates) ? '1 year ago' : 'Last year';
} else if ((difference.inDays / 30).floor() >= 2) {
@DineshKachhot
DineshKachhot / MemoryAddress.swift
Created May 28, 2019 12:54
Check memory address of Value type and Reference type in Swift 5
// Get memory address of value type
func address(of value: UnsafeRawPointer) -> Int {
return unsafeBitCast(value, to: Int.self)
}
var num1 = 55
var num2 = 55
print(NSString(format: "%p", address(of:&num1))) // -> "0x11fc3c5a0"
print(NSString(format: "%p", address(of:&num2))) // -> "0x11fc3c5a8"
num2 = 77
@DineshKachhot
DineshKachhot / ForceUpdateAppVersion.swift
Last active October 29, 2023 06:33
Force Update iOS App while API has major update
import Foundation
enum VersionError: Error {
case invalidResponse, invalidBundleInfo
}
class ForceUpdateAppVersion {
class func isForceUpdateRequire(apiVersion:Int) -> Bool {
func update() {
@DineshKachhot
DineshKachhot / NSTimeInterval+AudioDurationFormat.swift
Created August 29, 2019 06:49
Audio/Video Duration formate in Swift 4.2
import Foundation
extension TimeInterval {
struct DateComponents {
static let formatterPositional: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour,.minute,.second]
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
return formatter
@DineshKachhot
DineshKachhot / NSURLSession POST request
Created September 7, 2019 02:06
NSURLSession POST request - Swift 4.2
guard let urlEncodedString = (AppConstants.URL.sendDeviceTokekn).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
return
}
let url = URL(string: urlEncodedString)!
let urlRequest = NSMutableURLRequest(url: url, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60.0)
urlRequest.setValue("Application/json", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("Application/json", forHTTPHeaderField: "Accept")
urlRequest.httpMethod = "POST"
let params: [String : Any] = ["txDeviceToken":token, "tDeviceOs":2]
@DineshKachhot
DineshKachhot / ImageResponse.swift
Created September 7, 2019 02:09
NSURLSession GET Request - Swift 4.2
struct ImageResponse: Codable {
var message:String
var totalRecord:Int
var totalPage:Int
var nextPage:String
var images:[Image]
enum CodingKeys: String, CodingKey {
case message
case totalRecord
@DineshKachhot
DineshKachhot / iterm2.md
Created January 1, 2020 10:19 — forked from squarism/iterm2.md
iterm2 cheatsheet

Tabs and Windows

Function Shortcut
New Tab + T
Close Tab or Window + W (same as many mac apps)
Go to Tab + Number Key (ie: ⌘2 is 2nd tab)
Go to Split Pane by Direction + Option + Arrow Key
Cycle iTerm Windows + backtick (true of all mac apps and works with desktops/mission control)
//
// FormViewSocial.swift
// SwiftUI_Practice
//
// Created by Dinesh Kachhot on 05/03/20.
// Copyright © 2020 KD. All rights reserved.
//
import SwiftUI