-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
193d878
commit 5939811
Showing
3 changed files
with
51 additions
and
43 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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/enaboapps/switchify/service/switches/SwitchLongPressHandler.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,45 @@ | ||
package com.enaboapps.switchify.service.switches | ||
|
||
import android.content.Context | ||
import com.enaboapps.switchify.preferences.PreferenceManager | ||
import com.enaboapps.switchify.service.scanning.ScanningManager | ||
import com.enaboapps.switchify.switches.SwitchAction | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* This object manages the long press action on a switch. | ||
*/ | ||
object SwitchLongPressHandler { | ||
private var longPressJob: Job? = null | ||
private var longPressAction: SwitchAction? = null | ||
|
||
/** | ||
* Initiates the long press action. | ||
* @param context The context. | ||
* @param action The action to perform on long press. | ||
* @param scanningManager The manager responsible for scanning actions. | ||
*/ | ||
fun startLongPress(context: Context, action: SwitchAction, scanningManager: ScanningManager) { | ||
longPressAction = action | ||
val holdTime = | ||
PreferenceManager(context).getLongValue(PreferenceManager.PREFERENCE_KEY_SWITCH_HOLD_TIME) | ||
longPressJob = CoroutineScope(Dispatchers.Main).launch { | ||
delay(holdTime) | ||
longPressAction?.let { | ||
scanningManager.performAction(it) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Cancels the ongoing long press action. | ||
*/ | ||
fun stopLongPress() { | ||
longPressJob?.cancel() | ||
longPressJob = null | ||
} | ||
} |