Skip to content

Instantly share code, notes, and snippets.

Avatar
🇺🇦

Denis Rudenko Skyyo

🇺🇦
View GitHub Profile
@Skyyo
Skyyo / ImageCompressor.kt
Created Oct 28, 2021
#compression #image_compression #optimization
View ImageCompressor.kt
object ImageCompressor {
/**
* @param context the application environment
* @param imageUri the input image uri. usually "content://..."
* @param imageFile file where the image was saved. For "photo from camera" scenarios. If it's
* null - we're creating the File inside the createFileAndCompress()
* @param compressFormat the output image file format
* @param maxWidth the output image max width
* @param maxHeight the output image max height
@Skyyo
Skyyo / startup-loop.sh
Created Oct 20, 2021
startup time loop #startup #cold_boot
View startup-loop.sh
for i in `seq 1 100`
do
adb shell am force-stop com.smth.smth
sleep 1
adb shell am start-activity -W -n com.smth.smth/.application.MainActivity | grep "TotalTime" | cut -d ' ' -f 2
done
View LaunchedEffect.isCurrentItemVisible.value.kt
LaunchedEffect(isCurrentItemVisible.value) {
if (!isCurrentItemVisible.value && playingItemIndex != null) {
viewModel.onPlayVideoClick(exoPlayer.currentPosition, playingItemIndex!!)
}
}
View LazyListState.visibleAreaContainsItem.kt
private fun LazyListState.visibleAreaContainsItem(
currentlyPlayedIndex: Int?,
videos: List<VideoItem>
): Boolean {
return when {
currentlyPlayedIndex == null -> false
videos.isEmpty() -> false
else -> {
layoutInfo.visibleItemsInfo.map { videos[it.index] }.contains(videos[currentlyPlayedIndex])
}
View VideosScreenPart2.kt
@Composable
fun VideosScreen(viewModel: VideosViewModel = hiltViewModel()) {
val listState = rememberLazyListState()
val playingItemIndex by viewModel.currentlyPlayingIndex.observeAsState()
val isCurrentItemVisible = remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
snapshotFlow {
listState.visibleAreaContainsItem(playingItemIndex, videos)
}.collect { isItemVisible ->
View DisposableEffect.exoPlayer.kt
DisposableEffect(exoPlayer) {
val lifecycleObserver = LifecycleEventObserver { _, event ->
if (playingItemIndex == null) return@LifecycleEventObserver
when (event) {
Lifecycle.Event.ON_START -> exoPlayer.play()
Lifecycle.Event.ON_STOP -> exoPlayer.pause()
}
}
lifecycleOwner.lifecycle.addObserver(lifecycleObserver)
View LaunchedEffect.playingItemIndex.kt
LaunchedEffect(playingItemIndex) {
if (playingItemIndex == null) {
exoPlayer.pause()
} else {
val video = videos[playingItemIndex]
exoPlayer.setMediaItem(MediaItem.fromUri(video.mediaUrl), video.lastPlayedPosition)
exoPlayer.prepare()
exoPlayer.playWhenReady = true
}
}
View VideoPlayer.kt
@Composable
fun VideoPlayer(
exoPlayer: ExoPlayer,
onControllerVisibilityChanged: (uiVisible: Boolean) -> Unit
) {
val context = LocalContext.current
val playerView = remember {
val layout = LayoutInflater.from(context).inflate(R.layout.video_player, null, false)
val playerView = layout.findViewById(R.id.playerView) as PlayerView
playerView.apply {
View VideoCard.kt
@Composable
fun VideoCard(
videoItem: VideoItem,
isPlaying: Boolean,
exoPlayer: ExoPlayer,
onClick: OnClick
) {
val isPlayerUiVisible = remember { mutableStateOf(false) }
val isPlayButtonVisible = if (isPlayerUiVisible.value) true else !isPlaying
View VideosViewModelStep1.kt
@HiltViewModel
class VideosViewModel @Inject constructor() : ViewModel() {
val videos = MutableLiveData<List<VideoItem>>()
val currentlyPlayingIndex = MutableLiveData<Int?>()
init {
populateVideosWithTestData()
}