-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added video capture timer and fixed crashing bug when device is rotat…
…ed while recording video
- Loading branch information
Senior Ngenge
authored and
Senior Ngenge
committed
Feb 14, 2025
1 parent
08a7917
commit 5d5da5e
Showing
7 changed files
with
110 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
...d-libproofcam/src/main/java/org/witness/proofmode/camera/fragments/CameraLensViewModel.kt
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
android-libproofcam/src/main/java/org/witness/proofmode/camera/fragments/CameraViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.witness.proofmode.camera.fragments | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.camera.core.CameraSelector | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import java.util.Locale | ||
|
||
class CameraViewModel : ViewModel() { | ||
var lensFacing: MutableLiveData<Int> = MutableLiveData(CameraSelector.LENS_FACING_BACK) | ||
private set | ||
private val handler = Handler(Looper.getMainLooper()) | ||
private lateinit var timerRunnable: Runnable | ||
private var startTime: Long = 0 | ||
|
||
// LiveData to expose the elapsed time | ||
private val _elapsedTime = MutableLiveData<String>() | ||
val elapsedTime: LiveData<String> get() = _elapsedTime | ||
|
||
|
||
|
||
fun toggleLensFacing() { | ||
lensFacing.value = if (lensFacing.value == CameraSelector.LENS_FACING_BACK) { | ||
CameraSelector.LENS_FACING_FRONT | ||
} else { | ||
CameraSelector.LENS_FACING_BACK | ||
} | ||
} | ||
|
||
// Format the elapsed time | ||
private fun formatElapsedTime(elapsedTime: Long): String { | ||
val hours = (elapsedTime / 3600000).toInt() | ||
val minutes = (elapsedTime % 3600000 / 60000).toInt() | ||
val seconds = (elapsedTime % 60000 / 1000).toInt() | ||
|
||
return if (hours > 0) { | ||
String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, seconds) | ||
} else { | ||
String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds) | ||
} | ||
} | ||
|
||
fun stopTimer() { | ||
handler.removeCallbacks(timerRunnable) | ||
_elapsedTime.value = "00:00:00" // Reset the timer | ||
} | ||
fun startTimer() { | ||
startTime = System.currentTimeMillis() | ||
timerRunnable = object : Runnable { | ||
override fun run() { | ||
val timeDelta = System.currentTimeMillis() - startTime | ||
_elapsedTime.postValue(formatElapsedTime(timeDelta)) | ||
handler.postDelayed(this, 1000) | ||
|
||
} | ||
} | ||
handler.post(timerRunnable) | ||
} | ||
|
||
override fun onCleared() { | ||
handler.removeCallbacks(timerRunnable) | ||
super.onCleared() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters