Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.

Commit 07b173a

Browse files
authored
Merge pull request #19 from meganz/release/2.0.5
release/2.0.5
2 parents e1e3c36 + 0deee22 commit 07b173a

7 files changed

Lines changed: 78 additions & 40 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
3-
versionCode = 11
4-
versionName = "2.0.4"
3+
versionCode = 12
4+
versionName = "2.0.5"
55
compileSdkVersion = 29
66
minSdkVersion = 21
77
group = "com.github.meganz"

documentscanner/src/main/java/nz/mega/documentscanner/DocumentScannerViewModel.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.graphics.Bitmap
55
import android.net.Uri
66
import android.util.Log
7+
import androidx.camera.core.ImageCapture.FLASH_MODE_AUTO
78
import androidx.core.net.toUri
89
import androidx.lifecycle.LiveData
910
import androidx.lifecycle.MutableLiveData
@@ -42,6 +43,7 @@ class DocumentScannerViewModel : ViewModel() {
4243
private val saveDestinations: MutableLiveData<Array<String>> = MutableLiveData()
4344
private val currentPagePosition: MutableLiveData<Int> = MutableLiveData(0)
4445
private val resultDocument: MutableLiveData<Uri?> = MutableLiveData()
46+
private val flashMode: MutableLiveData<Int> = MutableLiveData(FLASH_MODE_AUTO)
4547
private var retakePosition: Int = NO_POSITION
4648

4749
fun getResultDocument(): LiveData<Uri?> =
@@ -84,7 +86,7 @@ class DocumentScannerViewModel : ViewModel() {
8486
}
8587

8688
fun setDocumentTitle(title: String?) {
87-
if (title.isNullOrBlank() || document.value?.title == title) return
89+
if (title == null || document.value?.title == title) return
8890

8991
document.value?.title = title
9092
document.notifyObserver()
@@ -116,6 +118,13 @@ class DocumentScannerViewModel : ViewModel() {
116118
saveDestinations.value = destinations
117119
}
118120

121+
fun setFlashMode(flashMode: Int) {
122+
this.flashMode.value = flashMode
123+
}
124+
125+
fun getFlashMode(): LiveData<Int> =
126+
flashMode
127+
119128
/**
120129
* Add page to the scan.
121130
*
@@ -250,7 +259,12 @@ class DocumentScannerViewModel : ViewModel() {
250259
fun deletePage(position: Int = currentPagePosition.value ?: 0) {
251260
viewModelScope.launch {
252261
document.value?.deletePage(position)
253-
document.notifyObserver()
262+
263+
if (document.value?.pages?.size == 0) {
264+
document.value = Document()
265+
} else {
266+
document.notifyObserver()
267+
}
254268
}
255269
}
256270

@@ -260,7 +274,7 @@ class DocumentScannerViewModel : ViewModel() {
260274
fun resetDocument() {
261275
viewModelScope.launch {
262276
document.value?.deletePages()
263-
document.notifyObserver()
277+
document.value = Document()
264278
}
265279
}
266280

documentscanner/src/main/java/nz/mega/documentscanner/camera/CameraFragment.kt

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import androidx.camera.core.Camera
1313
import androidx.camera.core.CameraSelector
1414
import androidx.camera.core.ImageAnalysis
1515
import androidx.camera.core.ImageCapture
16+
import androidx.camera.core.ImageCapture.FLASH_MODE_AUTO
17+
import androidx.camera.core.ImageCapture.FLASH_MODE_OFF
18+
import androidx.camera.core.ImageCapture.FLASH_MODE_ON
1619
import androidx.camera.core.ImageCaptureException
1720
import androidx.camera.core.ImageProxy
1821
import androidx.camera.core.Preview
@@ -63,6 +66,7 @@ class CameraFragment : Fragment() {
6366
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
6467
super.onViewCreated(view, savedInstanceState)
6568
setupView()
69+
setupObservers()
6670
}
6771

6872
override fun onDestroy() {
@@ -85,6 +89,23 @@ class CameraFragment : Fragment() {
8589
}
8690
}
8791

92+
private fun setupObservers() {
93+
viewModel.getFlashMode().observe(viewLifecycleOwner, ::setFlashMode)
94+
}
95+
96+
private fun setFlashMode(flashMode: Int) {
97+
val btnIcon = when (flashMode) {
98+
FLASH_MODE_ON -> R.drawable.ic_baseline_flash_on_24
99+
FLASH_MODE_AUTO -> R.drawable.ic_baseline_flash_auto_24
100+
else -> R.drawable.ic_baseline_flash_off_24
101+
}
102+
103+
binding.btnFlash.setImageResource(btnIcon)
104+
if (flashMode != imageCapture?.flashMode) {
105+
imageCapture?.flashMode = flashMode
106+
}
107+
}
108+
88109
private fun setUpCamera() {
89110
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
90111
cameraProviderFuture.addListener({
@@ -93,7 +114,7 @@ class CameraFragment : Fragment() {
93114
imageCapture = ImageCapture.Builder()
94115
.setTargetAspectRatio(screenAspectRatio)
95116
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
96-
.setFlashMode(ImageCapture.FLASH_MODE_AUTO)
117+
.setFlashMode(viewModel.getFlashMode().value ?: FLASH_MODE_AUTO)
97118
.build()
98119

99120
// TODO Disabled until further improvement
@@ -159,28 +180,13 @@ class CameraFragment : Fragment() {
159180
}
160181

161182
private fun toggleFlash() {
162-
imageCapture?.let { imageCapture ->
163-
val btnIcon: Int
164-
val flashMode: Int
165-
166-
when (imageCapture.flashMode) {
167-
ImageCapture.FLASH_MODE_ON -> {
168-
flashMode = ImageCapture.FLASH_MODE_OFF
169-
btnIcon = R.drawable.ic_baseline_flash_off_24
170-
}
171-
ImageCapture.FLASH_MODE_AUTO -> {
172-
flashMode = ImageCapture.FLASH_MODE_ON
173-
btnIcon = R.drawable.ic_baseline_flash_on_24
174-
}
175-
else -> {
176-
flashMode = ImageCapture.FLASH_MODE_AUTO
177-
btnIcon = R.drawable.ic_baseline_flash_auto_24
178-
}
179-
}
180-
181-
imageCapture.flashMode = flashMode
182-
binding.btnFlash.setImageResource(btnIcon)
183+
val newFlashMode = when (imageCapture?.flashMode) {
184+
FLASH_MODE_ON -> FLASH_MODE_OFF
185+
FLASH_MODE_AUTO -> FLASH_MODE_ON
186+
else -> FLASH_MODE_AUTO
183187
}
188+
189+
viewModel.setFlashMode(newFlashMode)
184190
}
185191

186192
private fun showProgress(show: Boolean) {

documentscanner/src/main/java/nz/mega/documentscanner/save/SaveFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import nz.mega.documentscanner.R
1616
import nz.mega.documentscanner.data.Document
1717
import nz.mega.documentscanner.databinding.FragmentSaveBinding
1818
import nz.mega.documentscanner.databinding.ItemDestinationBinding
19+
import nz.mega.documentscanner.utils.ViewUtils.selectLastCharacter
1920

2021
class SaveFragment : Fragment() {
2122

@@ -52,7 +53,7 @@ class SaveFragment : Fragment() {
5253
}
5354

5455
binding.imgEdit.setOnClickListener {
55-
binding.editFileName.requestFocus()
56+
binding.editFileName.selectLastCharacter()
5657
}
5758

5859
binding.chipGroupFileType.setOnCheckedChangeListener { _, checkedId ->

documentscanner/src/main/java/nz/mega/documentscanner/scan/ScanFragment.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class ScanFragment : Fragment() {
106106
}
107107
}
108108
} else {
109+
viewModel.resetDocument()
109110
navigateBack()
110111
}
111112
}

documentscanner/src/main/java/nz/mega/documentscanner/utils/ViewUtils.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package nz.mega.documentscanner.utils
22

33
import android.app.Activity
4+
import android.content.Context
45
import android.util.DisplayMetrics
56
import android.view.Display
67
import android.view.View
78
import android.view.inputmethod.InputMethodManager
9+
import android.widget.EditText
810
import androidx.camera.core.AspectRatio
911
import androidx.core.view.ViewCompat
1012
import androidx.viewpager2.widget.ViewPager2
@@ -43,4 +45,17 @@ object ViewUtils {
4345
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
4446
imm.hideSoftInputFromWindow(windowToken, 0)
4547
}
48+
49+
fun EditText.selectLastCharacter() {
50+
requestFocus()
51+
setSelection(text?.length ?: 0)
52+
showSoftKeyboard()
53+
}
54+
55+
fun View.showSoftKeyboard() {
56+
if (requestFocus()) {
57+
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
58+
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
59+
}
60+
}
4661
}

documentscanner/src/main/res/layout/fragment_save.xml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@
4848

4949
<com.google.android.material.textfield.TextInputLayout
5050
android:id="@+id/input_file_name"
51-
android:layout_width="wrap_content"
51+
android:layout_width="0dp"
5252
android:layout_height="wrap_content"
53-
android:layout_marginStart="76dp"
53+
android:layout_marginStart="72dp"
5454
android:layout_marginEnd="44dp"
5555
app:errorEnabled="true"
5656
app:layout_constrainedWidth="true"
5757
app:layout_constraintEnd_toEndOf="parent"
5858
app:layout_constraintHorizontal_bias="0.0"
5959
app:layout_constraintStart_toStartOf="parent"
6060
app:layout_constraintTop_toBottomOf="@id/txt_file_name"
61+
app:layout_constraintWidth_default="wrap"
6162
app:suffixTextColor="?colorOnSurface"
6263
tools:suffixText=".pdf">
6364

@@ -66,8 +67,7 @@
6667
android:layout_width="match_parent"
6768
android:layout_height="wrap_content"
6869
android:inputType="text"
69-
android:maxLength="27"
70-
android:digits="_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
70+
android:lines="1"
7171
android:maxLines="1"
7272
android:paddingStart="0dp"
7373
tools:text="Scanned_20200804" />
@@ -78,21 +78,22 @@
7878
android:id="@+id/img_file_type"
7979
android:layout_width="wrap_content"
8080
android:layout_height="wrap_content"
81-
android:layout_margin="16dp"
81+
android:layout_marginStart="16dp"
82+
android:baselineAlignBottom="true"
83+
android:paddingBottom="7dp"
8284
android:src="@drawable/ic_pdf"
83-
app:layout_constraintBottom_toBottomOf="@id/input_file_name"
84-
app:layout_constraintStart_toStartOf="parent"
85-
app:layout_constraintTop_toTopOf="@id/input_file_name" />
85+
app:layout_constraintBaseline_toBaselineOf="@id/input_file_name"
86+
app:layout_constraintStart_toStartOf="parent" />
8687

8788
<ImageView
8889
android:id="@+id/img_edit"
8990
android:layout_width="wrap_content"
9091
android:layout_height="wrap_content"
91-
android:layout_margin="16dp"
92+
android:layout_marginEnd="16dp"
93+
android:baselineAlignBottom="true"
9294
android:src="@drawable/ic_rename"
93-
app:layout_constraintBottom_toBottomOf="@id/input_file_name"
94-
app:layout_constraintEnd_toEndOf="parent"
95-
app:layout_constraintTop_toTopOf="@id/input_file_name" />
95+
app:layout_constraintBaseline_toBaselineOf="@id/input_file_name"
96+
app:layout_constraintEnd_toEndOf="parent" />
9697

9798
<View
9899
android:id="@+id/separator_file_name"

0 commit comments

Comments
 (0)