Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipleEventCutter to prevent rapid click on capture button #62

Merged
merged 7 commits into from
Oct 17, 2023
3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.jetpackcamera.feature.preview

/**
* A helper class that prevents multiple clicks.
*/
internal class MultipleEventsCutter {
private val now: Long
get() = System.currentTimeMillis()

private var lastEventTimeMs: Long = 0

fun processEvent(event: () -> Unit) {
if (now - lastEventTimeMs >= DURATION_BETWEEN_CLICKS_MS) {
event.invoke()
}
lastEventTimeMs = now
}

companion object {
private const val DURATION_BETWEEN_CLICKS_MS = 300L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,12 @@ fun PreviewScreen(onNavigateToSettings: () -> Unit, viewModel: PreviewViewModel
)
}
}
val multipleEventsCutter = remember { MultipleEventsCutter() }
/*todo: close quick settings on start record/image capture*/
CaptureButton(
onClick = { viewModel.captureImage() },
onClick = {
multipleEventsCutter.processEvent { viewModel.captureImage() }
},
onLongPress = { viewModel.startVideoRecording() },
onRelease = { viewModel.stopVideoRecording() },
videoRecordingState = previewUiState.videoRecordingState
Expand Down
Loading