Skip to content

Instantly share code, notes, and snippets.

View harshvishu's full-sized avatar
🍥
Learning

हर्ष • Harsh • ハラシャ harshvishu

🍥
Learning
View GitHub Profile
struct User: ResponseObjectSerializable, ResponseCollectionSerializable, CustomStringConvertible {
let username: String
let name: String
var description: String {
return "User: { username: \(username), name: \(name) }"
}
init?(response: HTTPURLResponse, representation: Any) {
guard
extension DataRequest{
/// @Returns - DataRequest
/// completionHandler handles JSON Object T
@discardableResult func responseObject<T: Decodable> (
queue: DispatchQueue? = nil ,
completionHandler: @escaping (DataResponse<T>) -> Void ) -> Self{
let responseSerializer = DataResponseSerializer<T> { request, response, data, error in
guard error == nil else {return .failure(BackendError.network(error: error!))}
struct User: Decodable, CustomStringConvertible {
let username: String
let name: String
/// This is the key part
/// If parameters and variable name differ
/// you can specify custom key for mapping "eg. 'user_name'"
enum CodingKeys: String, CodingKey {
case username = "user_name"
// 1
Alamofire.request(Router.readUser("mattt")).responseObject{ (response: DataResponse<User>) in
// Process userResponse, of type DataResponse<User>:
if let user = response.value {
print("User: { username: \(user.username), name: \(user.name) }")
}
}
// 2
package com.example.com;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
@harshvishu
harshvishu / CircleImageView.kt
Last active November 8, 2019 07:38
CircleImageView Kotlin
package com.example.android
import android.content.Context
import android.graphics.*
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
@harshvishu
harshvishu / ResponseSerializer.swift
Last active April 26, 2018 07:43
Alamofire ResponseSerializer for Decodable
extension DataRequest {
private func decodableResponseSerializer<T: Decodable>() -> DataResponseSerializer<T> {
return DataResponseSerializer { _, response, data, error in
guard error == nil else { return .failure(error!) }
guard let data = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
}
@harshvishu
harshvishu / Date+Extension.swift
Created December 14, 2018 18:22
UTC to local
extension Date {
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert UTC (or GMT) to local time
@harshvishu
harshvishu / Package.swift
Last active January 7, 2019 19:36
Swift-Blockchain-Package.swift
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Swift-Blockchain",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.executable(
struct Block: Codable {
var index: Int64
var timestamp: Date
var transactions: [Transaction]
var proof: Int64
var previous_hash: String
}
struct Transaction: Codable {
var sender: String