TakePictureViewModel.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. public CameraSelector getCameraSelector() {
  39. return this.cameraSelector;
  40. }
  41. public void toggleCameraSelector() {
  42. if (this.cameraSelector == DEFAULT_BACK_CAMERA) {
  43. this.cameraSelector = DEFAULT_FRONT_CAMERA;
  44. } else {
  45. this.cameraSelector = DEFAULT_BACK_CAMERA;
  46. }
  47. }
  48. public void toggleTorchEnabled() {
  49. //noinspection ConstantConditions
  50. this.torchEnabled.postValue(!this.torchEnabled.getValue());
  51. }
  52. public LiveData<Boolean> isTorchEnabled() {
  53. return this.torchEnabled;
  54. }
  55. public LiveData<Integer> getTorchToggleButtonImageResource() {
  56. return Transformations.map(isTorchEnabled(), enabled -> enabled
  57. ? R.drawable.ic_baseline_flash_off_24
  58. : R.drawable.ic_baseline_flash_on_24);
  59. }
  60. }