Skip to content

Instantly share code, notes, and snippets.

@realpacific
realpacific / ts-to-json.py
Created May 15, 2020 15:11
Convert Typescript object to json
import re
import sys
import os
filename=sys.argv[1]
newline = ""
with open(filename, 'r') as file:
for line in file.readlines():
@realpacific
realpacific / app.py
Created May 10, 2020 14:01
Python Google Sheets as backend
from __future__ import print_function
import json
import time
import os
from flask import Flask
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
@realpacific
realpacific / AnimationUtils.kt
Created May 25, 2019 05:50
Handles Circular Reveal animation on Fragment's view
/**
* Starts circular reveal animation
* [fromLeft] if `true` then start animation from the bottom left of the [View] else start from the bottom right
*/
fun View.startCircularReveal(fromLeft: Boolean) {
addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
override fun onLayoutChange(v: View, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int,
oldRight: Int, oldBottom: Int) {
v.removeOnLayoutChangeListener(this)
@realpacific
realpacific / OneFragment.kt
Created May 22, 2019 17:58
The Fragment that will exit with circular reveal animation implements the ExitWithAnimation Interface
class OneFragment : Fragment(), ExitWithAnimation {
override var posX: Int? = null
override var posY: Int? = null
override fun isToBeExitedWithAnimation(): Boolean = true
companion object {
@JvmStatic
fun newInstance(exit: IntArray? = null): OneFragment = OneFragment().apply {
@realpacific
realpacific / LocalCacheDataSourceFactory.kt
Created May 17, 2019 06:19
Implementing cache in ExoPlayer
class LocalCacheDataSourceFactory(private val context: Context) : DataSource.Factory {
private val defaultDataSourceFactory: DefaultDataSourceFactory
//TODO Needs to be a singleton
private val simpleCache: SimpleCache = SimpleCache(
File(context.cacheDir, "media"),
LeastRecentlyUsedCacheEvictor(MAX_CACHE_SIZE)
)
private val cacheDataSink: CacheDataSink = CacheDataSink(simpleCache, MAX_FILE_SIZE)
@realpacific
realpacific / BindingAdapters.kt
Created May 17, 2019 06:03
BindingAdapter code for binding xml values to PlayerView
@BindingAdapter("video_url", "on_state_change")
fun PlayerView.loadVideo(url: String, callback: PlayerStateChange) {
if (url == null) return
val player = ExoPlayerFactory.newSimpleInstance(
context, DefaultRenderersFactory(context), DefaultTrackSelector(),
DefaultLoadControl()
)
player.playWhenReady = true
player.repeatMode = Player.REPEAT_MODE_ALL
@realpacific
realpacific / PlayerStateCallback.kt
Created May 17, 2019 05:46
Data binding in RecyclerView's Adapter and implementing the listener
interface PlayerStateCallback {
/**
* Callback to when the [PlayerView] has fetched the duration of video
**/
fun onVideoDurationRetrieved(duration: Long, player: Player)
fun onVideoBuffering(player: Player)
fun onStartedPlaying(player: Player)
@realpacific
realpacific / item_video.xml
Last active May 17, 2019 05:41
Layout for video player UI
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="url"
type="String"/>
<variable
//...
//Extend NetworkSensingBaseActivity class to override default behaviour of this activity
class MainActivity : NetworkSensingBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
}
}
//..
/**
* Extend this activity whenever you want the activity to react to network status changes
*/
@SuppressLint("Registered")
open class NetworkSensingBaseActivity : AppCompatActivity(), ConnectionStateMonitor.OnNetworkAvailableCallbacks {
var snackbar: CustomSnackbar? = null
private var connectionStateMonitor: ConnectionStateMonitor? = null