(I'm a beginner)
What is the recommended/cleanest/most efficient way of implementing features that can be toggled on/off by users?
I'm planning to add some features to my app that the user can toggle on or off in a Settings page. E.g., haptic feedback when tapping certain Views. In my code, this View has a onClickListener that executes the haptic feedback and some other methods. In the Settings page for my app, I'll put something like "Enable haptic feedback" with a Switch. Example
The first idea that comes to mind is just putting if wherever the vibrate method is called. Something like...
myView.setOnClickListener(new View.OnClickListener) {
if (hapticsSwitch.isEnabled) {
vibrate(10) //10 ms feedback
}
//more methods...
}
However, this if would need to be repeated whenever I call any toggleable feature in my app. So perhaps a cleaner way would be putting this if inside those features' methods. E.g.
public void vibrate(Long duration) {
if (hapticsSwitch.isEnabled) {
// ...
}
}
But I have no experience with toggleable features. So, what is the recommended/cleanest/most efficient way of implementing toggleable features into my code?