TakePictureViewModel.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Stefan Niedermann
  6. * Copyright (C) 2021 Andy Scherzinger <info@andy-scherzinger.de>
  7. * Copyright (C) 2021 Stefan Niedermann <info@niedermann.it>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.models;
  23. import com.nextcloud.talk.R;
  24. import androidx.annotation.NonNull;
  25. import androidx.camera.core.CameraSelector;
  26. import androidx.lifecycle.LiveData;
  27. import androidx.lifecycle.MutableLiveData;
  28. import androidx.lifecycle.Transformations;
  29. import androidx.lifecycle.ViewModel;
  30. import static androidx.camera.core.CameraSelector.DEFAULT_BACK_CAMERA;
  31. import static androidx.camera.core.CameraSelector.DEFAULT_FRONT_CAMERA;
  32. public class TakePictureViewModel extends ViewModel {
  33. @NonNull
  34. private CameraSelector cameraSelector = DEFAULT_BACK_CAMERA;
  35. @NonNull
  36. private final MutableLiveData<Integer> cameraSelectorToggleButtonImageResource =
  37. new MutableLiveData<>(R.drawable.ic_baseline_camera_front_24);
  38. @NonNull
  39. private final MutableLiveData<Boolean> torchEnabled = new MutableLiveData<>(false);
  40. @NonNull
  41. public CameraSelector getCameraSelector() {
  42. return this.cameraSelector;
  43. }
  44. public LiveData<Integer> getCameraSelectorToggleButtonImageResource() {
  45. return this.cameraSelectorToggleButtonImageResource;
  46. }
  47. public void toggleCameraSelector() {
  48. if (this.cameraSelector == DEFAULT_BACK_CAMERA) {
  49. this.cameraSelector = DEFAULT_FRONT_CAMERA;
  50. this.cameraSelectorToggleButtonImageResource.postValue(R.drawable.ic_baseline_camera_rear_24);
  51. } else {
  52. this.cameraSelector = DEFAULT_BACK_CAMERA;
  53. this.cameraSelectorToggleButtonImageResource.postValue(R.drawable.ic_baseline_camera_front_24);
  54. }
  55. }
  56. public void toggleTorchEnabled() {
  57. //noinspection ConstantConditions
  58. this.torchEnabled.postValue(!this.torchEnabled.getValue());
  59. }
  60. public LiveData<Boolean> isTorchEnabled() {
  61. return this.torchEnabled;
  62. }
  63. public LiveData<Integer> getTorchToggleButtonImageResource() {
  64. return Transformations.map(isTorchEnabled(), enabled -> enabled
  65. ? R.drawable.ic_baseline_flash_off_24
  66. : R.drawable.ic_baseline_flash_on_24);
  67. }
  68. }