Skip to content

Instantly share code, notes, and snippets.

/**
* Use [enable]/[disable] to register/unregister [ConnectivityManager]
* @property context
* @property onNetworkAvailableCallbacks callback to notify [context] about network status changes
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class ConnectionStateMonitor(
private val context: Context,
private val onNetworkAvailableCallbacks: OnNetworkAvailableCallbacks
) : ConnectivityManager.NetworkCallback() {
//..
import com.google.android.material.snackbar.BaseTransientBottomBar
class CustomSnackbar private constructor(
parent: ViewGroup, content: View,
callback: com.google.android.material.snackbar.ContentViewCallback
) : BaseTransientBottomBar<CustomSnackbar>(parent, content, callback) {
fun setText(text: CharSequence): CustomSnackbar {
//..
/**
* 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
//...
//Extend NetworkSensingBaseActivity class to override default behaviour of this activity
class MainActivity : NetworkSensingBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//...
}
}
@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
@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 / 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 / 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 / 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 / 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)