TakePictureViewModel.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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<Boolean> torchEnabled = new MutableLiveData<>(Boolean.FALSE);
  37. @NonNull
  38. private final MutableLiveData<Boolean> lowResolutionEnabled = new MutableLiveData<>(Boolean.FALSE);
  39. @NonNull
  40. private final MutableLiveData<Boolean> cropEnabled = new MutableLiveData<>(Boolean.FALSE);
  41. @NonNull
  42. public CameraSelector getCameraSelector() {
  43. return this.cameraSelector;
  44. }
  45. public void toggleCameraSelector() {
  46. if (this.cameraSelector == DEFAULT_BACK_CAMERA) {
  47. this.cameraSelector = DEFAULT_FRONT_CAMERA;
  48. if (this.torchEnabled.getValue()) {
  49. toggleTorchEnabled();
  50. }
  51. } else {
  52. this.cameraSelector = DEFAULT_BACK_CAMERA;
  53. }
  54. }
  55. public void toggleTorchEnabled() {
  56. //noinspection ConstantConditions
  57. this.torchEnabled.postValue(!this.torchEnabled.getValue());
  58. }
  59. public void toggleLowResolutionEnabled() {
  60. //noinspection ConstantConditions
  61. this.lowResolutionEnabled.postValue(!this.lowResolutionEnabled.getValue());
  62. }
  63. public void toggleCropEnabled() {
  64. //noinspection ConstantConditions
  65. this.cropEnabled.postValue(!this.cropEnabled.getValue());
  66. }
  67. public LiveData<Boolean> isTorchEnabled() {
  68. return this.torchEnabled;
  69. }
  70. public LiveData<Boolean> isLowResolutionEnabled() {
  71. return this.lowResolutionEnabled;
  72. }
  73. public LiveData<Boolean> isCropEnabled() {
  74. return this.cropEnabled;
  75. }
  76. public LiveData<Integer> getTorchToggleButtonImageResource() {
  77. return Transformations.map(isTorchEnabled(), enabled -> enabled
  78. ? R.drawable.ic_baseline_flash_off_24
  79. : R.drawable.ic_baseline_flash_on_24);
  80. }
  81. public LiveData<Integer> getLowResolutionToggleButtonImageResource() {
  82. return Transformations.map(isLowResolutionEnabled(), enabled -> enabled
  83. ? R.drawable.ic_high_quality
  84. : R.drawable.ic_low_quality);
  85. }
  86. public LiveData<Integer> getCropToggleButtonImageResource() {
  87. return Transformations.map(isCropEnabled(), enabled -> enabled
  88. ? R.drawable.ic_crop_4_3
  89. : R.drawable.ic_crop_16_9);
  90. }
  91. }