Skip to content

Instantly share code, notes, and snippets.

View Bradleycorn's full-sized avatar

Brad Ball Bradleycorn

View GitHub Profile
@Bradleycorn
Bradleycorn / Theme.kt
Last active November 26, 2021 18:10
An Example Theme for Jetpack Compose that uses the built-in Material Theme, but adds a single extra color, "Tertiary".
package net.bradball.android.androidapptemplate.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
private val DarkColorPalette = AppColors(
materialColors = darkColors(
primary = purple200,
@Bradleycorn
Bradleycorn / NetworkLayer.kt
Last active June 26, 2020 20:34
Example of abstracting Java based network responses, since they can always have null values
/**
A class that makes api calls to the backend, process responses, and returns proper data models
*/
class NetworkLayer(val api: CdiApi) {
fun fetchProgramData(track: String, race: Number): List<ProgramEntry> {
// Get a ProgramResponse object (the network model) back from the api
val response: ProgramResponse = api.fetchProgram(track, race)
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val isLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
if (isLandscape) {
supportActionBar?.hide()
supportActionBar?.setDisplayShowTitleEnabled(false)
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
} else { // turn OFF full screen
supportActionBar?.show()
class MainActivity : AppCompatActivity() {
private var currentPlaybackState: MainActivityViewModel.VideoPlaybackState? = null
private var currentLayoutState: MainActivityViewModel.VideoLayoutState? = null
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.options_menu, menu)
return true
}
class MainActivity : AppCompatActivity() {
private val viewModel: MainActivityViewModel by viewModels()
private val videoPlayerCallbacks = object: ControlledVideoView.IVideoListener {
override fun onPipToggleClicked() {
viewModel.onPipToggled()
}
override fun onVideoCloseClicked() {
class MainActivityViewModel: ViewModel() {
val videoUrl = "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8"
enum class VideoPlaybackState {
STOPPED,
PLAYING
}
enum class VideoLayoutState {
class MainActivityViewModel: ViewModel() {
private val orientationHandler = Handler()
private var orientationLock: Int = Configuration.ORIENTATION_UNDEFINED
fun onDeviceOrientationChange(deviceOrientation: Int, activityOrientation: Int?) {
currentOrientation = when (deviceOrientation) {
in 0..20, in 160..200, in 340..359 -> Configuration.ORIENTATION_PORTRAIT
class MainActivity : AppCompatActivity() {
lateinit var orientationListener: OrientationEventListener
override fun onCreate(savedInstanceState: Bundle?) {
orientationListener = object: OrientationEventListener(this) {
override fun onOrientationChanged(orientation: Int) {
viewModel.onOrientationChange(orientation, resources.configuration.orientation)
}
}.apply { disable() }
@Bradleycorn
Bradleycorn / MainActivity.kt
Last active February 3, 2019 21:19
MotionLayout - MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = getString(R.string.app_name)
setupVideo()
}
@Bradleycorn
Bradleycorn / MainActivityViewModel.kt
Last active January 22, 2019 01:39
MotionLayout - ViewModel
class MainActivityViewModel: ViewModel() {
//...
private fun setOrientation(requestedOrientation: Int = -1) {
val newOrientation = requestedOrientation.takeUnless { it == -1 } ?: currentOrientation
_layoutState.value = when (newOrientation) {
Configuration.ORIENTATION_LANDSCAPE -> VideoLayoutState.FULLSCREEN