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 / CustomArrayAdapter.java
Last active April 3, 2020 08:25
ArrayAdapter with custom view using anonymous class
periodsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"loading..."}) {
View render(View convertView, int position) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.single_month, null);
}
// TODO set some data to views
return convertView;
}
@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,
@jamesgathu
jamesgathu / sample_loading_base_.html
Last active December 22, 2023 07:26
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 / 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 / _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 / 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 / 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 / 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 / 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 / AudioMerge.swift
Last active October 24, 2023 14:29
**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()