Skip to content

Instantly share code, notes, and snippets.

View xmhafiz's full-sized avatar
🏠
Working from home

Mohd Hafiz xmhafiz

🏠
Working from home
View GitHub Profile
@xmhafiz
xmhafiz / App\Http\Controllers\Controller.php
Last active November 8, 2017 14:40
Laravel 5.4 - Custom default validation error output and status code to 400
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Validation\Validator;
@xmhafiz
xmhafiz / ViewControllerExtension.swift
Last active May 11, 2017 04:07
To simplify open Facebook page based on page id and name using swift
import UIKit
extension UIViewController {
func openFacebook(pageId: String, pageName: String) {
guard let facebookURL = URL(string: "fb://page/?id=\(pageId)") else {
return
}
if (UIApplication.shared.canOpenURL(facebookURL)) {
@xmhafiz
xmhafiz / Bash alias
Last active November 6, 2017 01:38
Some lazy bash aliases (append to ~/.bash_profile)
#laravel
alias pa="php artisan"
#git
alias gi="git init"
alias gs="git status"
alias ga="git add"
alias gc="git commit -m"
alias gac="git add .;git commit -m"
alias gp="git push"
@xmhafiz
xmhafiz / PhoneHelper.swift
Created October 20, 2019 23:36
To get country prefix phone code using Swift 5
import Foundation
import CoreTelephony
struct PhoneHelper {
static func getCountryCode() -> String {
guard let carrier = CTTelephonyNetworkInfo().subscriberCellularProvider, let countryCode = carrier.isoCountryCode else { return "+" }
let prefixCodes = ["AF": "93", "AE": "971", "AL": "355", "AN": "599", "AS":"1", "AD": "376", "AO": "244", "AI": "1", "AG":"1", "AR": "54","AM": "374", "AW": "297", "AU":"61", "AT": "43","AZ": "994", "BS": "1", "BH":"973", "BF": "226","BI": "257", "BD": "880", "BB": "1", "BY": "375", "BE":"32","BZ": "501", "BJ": "229", "BM": "1", "BT":"975", "BA": "387", "BW": "267", "BR": "55", "BG": "359", "BO": "591", "BL": "590", "BN": "673", "CC": "61", "CD":"243","CI": "225", "KH":"855", "CM": "237", "CA": "1", "CV": "238", "KY":"345", "CF":"236", "CH": "41", "CL": "56", "CN":"86","CX": "61", "CO": "57", "KM": "269", "CG":"242", "CK": "682", "CR": "506", "CU":"53", "CY":"537","CZ": "420", "DE": "49", "DK": "45", "DJ":"253", "DM": "1", "DO": "1", "DZ": "213", "EC": "593"
@xmhafiz
xmhafiz / CustomUserDefaults.swift
Last active April 4, 2021 06:12
CustomUserDefaults.swift (Less)
// CustomUserDefaults.swift - Less
import Foundation
class CustomUserDefaults {
// define all keys needed
enum DefaultsKey: String, CaseIterable {
case name
case email
case isUserLogin
case userLevel
// save name and email
let fullName = "John Smith"
CustomUserDefaults.shared.set(fullName, key: .name)
let userEmail = "[email protected]"
CustomUserDefaults.shared.set(userEmail, key: .email)
// print stored name and email
let savedName = CustomUserDefaults.shared.get(key: .name)
print(savedName) // result: Optional(John Smith)
if let savedName = CustomUserDefaults.shared.get(key: .name) as? String {
print(savedName) // result: [email protected]
}
@xmhafiz
xmhafiz / CustomUserDefaultsFull.swift
Created April 4, 2021 06:40
CustomUserDefaultsFull.swift
// CustomUserDefaults.swift (full)
import Foundation
class CustomUserDefaults {
// define all keys needed
enum DefaultsKey: String, CaseIterable {
case name
case email
case isUserLogin
case userLevel
@xmhafiz
xmhafiz / UIViewControllerExtensionPreview.swift
Created April 5, 2021 04:25
Enable UIViewController Live Preview
import UIKit
import SwiftUI
extension UIViewController {
// enable preview for UIKit
// source: https://fluffy.es/xcode-previews-uikit/
@available(iOS 13, *)
private struct Preview: UIViewControllerRepresentable {
// this variable is used for injecting the current view controller
let viewController: UIViewController
@xmhafiz
xmhafiz / UIViewExtensionPreview.swift
Created April 5, 2021 04:42
UIView Extension for Live Preview
import UIKit
import SwiftUI
extension UIView {
// enable preview for UIKit
// source: https://dev.to/gualtierofr/preview-uikit-views-in-xcode-3543
@available(iOS 13, *)
private struct Preview: UIViewRepresentable {
typealias UIViewType = UIView
let view: UIView