Skip to content

Instantly share code, notes, and snippets.

View jamesgathu's full-sized avatar
🎯
Focusing

James Gathu jamesgathu

🎯
Focusing
View GitHub Profile
@jamesgathu
jamesgathu / Pull to Refresh.md
Last active June 12, 2018 10:18
To add pull to refresh on a TableView

Adding Pull to Refresh To iOS tableView

Follow the code below for adding pull to refresh to a tableview

          value       
iOS version 11.2
Swift 4
@jamesgathu
jamesgathu / AudioMerge.swift
Last active August 11, 2024 15:34
**Merging Multiple Audio Files in iOS**
/**
provide an array of audio files urls and get them merged
Its important to note that the process is asynchronous and that one would need to show user some sort of a progress indicator
so that the process does not get interupted
- parameter audioFileUrls: an array of audio file urls
- returns String representing the newly merged file or nil for a failure
*/
func mergeAudioFiles(audioFileUrls: [URL]) -> String? {
let composition = AVMutableComposition()
@jamesgathu
jamesgathu / CircularImageView.swift
Last active February 7, 2019 08:26
Make sure the used image is square,
import UIKit
@IBDesignable class CircularImageView: UIImageView {
@IBInspectable var lineWidth: CGFloat = 2
@IBInspectable var color: UIColor = UIColor.red
override func layoutSubviews() {
super.layoutSubviews()
@jamesgathu
jamesgathu / UITextView.swift
Created March 7, 2019 12:14
Adding Placeholder to UITextView
@IBDesignable
class PlaceHolderTextView: UITextView {
@IBInspectable var placeholder: String = "" {
didSet{
updatePlaceHolder()
}
}
@IBInspectable var placeholderColor: UIColor = UIColor.gray {
@jamesgathu
jamesgathu / App.js
Created March 25, 2019 23:10
ReactJS add Sidebar
import React, {Component} from 'react';
import './App.css';
import SideDrawer from "./components/Sidedrawer/side_drawer";
class App extends Component {
state = {
sideDrawerOpen: false
};
drawerToggleClickHandler = () => {
@jamesgathu
jamesgathu / auth.py
Created May 8, 2019 08:07
Incase you are using `djangorestframework-simplejwt` for authentication,
class JwtTokenAuthMiddleware(BaseMiddleware):
"""
JWT token authorization middleware for Django Channels 2
"""
def get_validated_token(self, raw_token):
"""
Validates an encoded JSON web token and returns a validated token
wrapper object.
"""
@jamesgathu
jamesgathu / _JwtTokenAuthMiddleware.md
Last active May 8, 2019 08:15
Simple Jwt Token AuthMiddleware for channels 2.0

Incase you are using djangorestframework-simplejwt for authentication and Would like to use channels==2.0.

On web sending token over headers is not possible if using js WebSocket available API. To go around this you can send your token on the url as indicated below wws://example.com?token=[a_valid_token]

The middleware above can be added on routing.py to supply a valid user to the consumer.

NB :: Securitywise this is not advisable to do.

@jamesgathu
jamesgathu / Viewcontroller.swift
Last active May 14, 2019 10:04
iOS add placeholder to UITextView
class Viewcontroller : UIViewController, UITextViewDelegate{
override func viewDidLoad(){
super.viewDidLoad()
textView.text = "Placeholder"
textView.textColor = UIColor.lightGray
}
func textViewDidBeginEditing(_ textView: UITextView) {
@jamesgathu
jamesgathu / sample_loading_base_.html
Last active October 22, 2024 04:39
Simple page pre-loader using html, css and js
<!DOCTYPE>
<html lang='en'>
<head>
<title> Loader example </title>
<style>
.loading-gif {
max-width: 500px;
}
.pre-loader {
@jamesgathu
jamesgathu / middlewares.py
Created February 10, 2020 19:33
Auth Middleware to work with browser sessions and jwt tokens as well
from django.conf import settings
from django.contrib.auth import (
BACKEND_SESSION_KEY,
HASH_SESSION_KEY,
SESSION_KEY,
_get_backends,
get_user_model,
load_backend,
user_logged_in,
user_logged_out,