Skip to content

Commit 64d4ffc

Browse files
authored
4664: Moved CustomSelectorActivity to ViewBinding (#5065)
1 parent 10b025c commit 64d4ffc

File tree

2 files changed

+64
-52
lines changed

2 files changed

+64
-52
lines changed

app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/CustomSelectorActivity.kt

+54-36
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.SHOULD_
2020
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
2121
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
2222
import fr.free.nrw.commons.customselector.model.Image
23+
import fr.free.nrw.commons.databinding.ActivityCustomSelectorBinding
24+
import fr.free.nrw.commons.databinding.CustomSelectorBottomLayoutBinding
25+
import fr.free.nrw.commons.databinding.CustomSelectorToolbarBinding
2326
import fr.free.nrw.commons.filepicker.Constants
2427
import fr.free.nrw.commons.media.ZoomableActivity
2528
import fr.free.nrw.commons.theme.BaseActivity
2629
import fr.free.nrw.commons.upload.FileUtilsWrapper
2730
import fr.free.nrw.commons.utils.CustomSelectorUtils
28-
import kotlinx.android.synthetic.main.custom_selector_bottom_layout.*
2931
import kotlinx.coroutines.*
3032
import java.io.File
3133
import javax.inject.Inject
@@ -34,7 +36,14 @@ import javax.inject.Inject
3436
/**
3537
* Custom Selector Activity.
3638
*/
37-
class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectListener {
39+
class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectListener {
40+
41+
/**
42+
* ViewBindings
43+
*/
44+
private lateinit var binding: ActivityCustomSelectorBinding
45+
private lateinit var toolbarBinding: CustomSelectorToolbarBinding
46+
private lateinit var bottomSheetBinding: CustomSelectorBottomLayoutBinding
3847

3948
/**
4049
* View model.
@@ -60,7 +69,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
6069
/**
6170
* View Model Factory.
6271
*/
63-
@Inject lateinit var customSelectorViewModelFactory: CustomSelectorViewModelFactory
72+
@Inject
73+
lateinit var customSelectorViewModelFactory: CustomSelectorViewModelFactory
6474

6575
/**
6676
* NotForUploadStatus Dao class for database operations
@@ -77,8 +87,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
7787
/**
7888
* Coroutine Dispatchers and Scope.
7989
*/
80-
private val scope : CoroutineScope = MainScope()
81-
private var ioDispatcher : CoroutineDispatcher = Dispatchers.IO
90+
private val scope: CoroutineScope = MainScope()
91+
private var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
8292

8393
/**
8494
* Image Fragment instance
@@ -90,23 +100,27 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
90100
*/
91101
override fun onCreate(savedInstanceState: Bundle?) {
92102
super.onCreate(savedInstanceState)
93-
setContentView(R.layout.activity_custom_selector)
103+
binding = ActivityCustomSelectorBinding.inflate(layoutInflater)
104+
toolbarBinding = CustomSelectorToolbarBinding.bind(binding.root)
105+
bottomSheetBinding = CustomSelectorBottomLayoutBinding.bind(binding.root)
106+
val view = binding.root
107+
setContentView(view)
94108

95-
prefs = applicationContext.getSharedPreferences("CustomSelector", MODE_PRIVATE)
109+
prefs = applicationContext.getSharedPreferences("CustomSelector", MODE_PRIVATE)
96110
viewModel = ViewModelProvider(this, customSelectorViewModelFactory).get(
97111
CustomSelectorViewModel::class.java
98112
)
99113

100114
setupViews()
101115

102-
if(prefs.getBoolean("customSelectorFirstLaunch", true)) {
116+
if (prefs.getBoolean("customSelectorFirstLaunch", true)) {
103117
// show welcome dialog on first launch
104118
showWelcomeDialog()
105119
prefs.edit().putBoolean("customSelectorFirstLaunch", false).apply()
106120
}
107121

108122
// Open folder if saved in prefs.
109-
if(prefs.contains(FOLDER_ID)){
123+
if (prefs.contains(FOLDER_ID)) {
110124
val lastOpenFolderId: Long = prefs.getLong(FOLDER_ID, 0L)
111125
val lastOpenFolderName: String? = prefs.getString(FOLDER_NAME, null)
112126
val lastItemId: Long = prefs.getLong(ITEM_ID, 0)
@@ -120,7 +134,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
120134
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
121135
super.onActivityResult(requestCode, resultCode, data)
122136
if (requestCode == Constants.RequestCodes.RECEIVE_DATA_FROM_FULL_SCREEN_MODE &&
123-
resultCode == Activity.RESULT_OK) {
137+
resultCode == Activity.RESULT_OK
138+
) {
124139
val selectedImages: ArrayList<Image> =
125140
data!!
126141
.getParcelableArrayListExtra(CustomSelectorConstants.NEW_SELECTED_IMAGES)!!
@@ -156,19 +171,19 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
156171
* Set up bottom layout
157172
*/
158173
private fun setUpBottomLayout() {
159-
val done : Button = findViewById(R.id.upload)
174+
val done: Button = findViewById(R.id.upload)
160175
done.setOnClickListener { onDone() }
161176

162-
val notForUpload : Button = findViewById(R.id.not_for_upload)
163-
notForUpload.setOnClickListener{ onClickNotForUpload() }
177+
val notForUpload: Button = findViewById(R.id.not_for_upload)
178+
notForUpload.setOnClickListener { onClickNotForUpload() }
164179
}
165180

166181
/**
167182
* Gets selected images and proceed for database operations
168183
*/
169184
private fun onClickNotForUpload() {
170185
val selectedImages = viewModel.selectedImages.value
171-
if(selectedImages.isNullOrEmpty()) {
186+
if (selectedImages.isNullOrEmpty()) {
172187
markAsNotForUpload(arrayListOf())
173188
return
174189
}
@@ -207,7 +222,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
207222
private fun insertIntoNotForUpload(images: ArrayList<Image>) {
208223
scope.launch {
209224
var allImagesAlreadyNotForUpload = true
210-
images.forEach{
225+
images.forEach {
211226
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
212227
it.uri,
213228
ioDispatcher,
@@ -254,7 +269,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
254269
}
255270

256271
imageFragment!!.refresh()
257-
val bottomLayout : ConstraintLayout = findViewById(R.id.bottom_layout)
272+
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
258273
bottomLayout.visibility = View.GONE
259274
}
260275
}
@@ -270,8 +285,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
270285
* Change the title of the toolbar.
271286
*/
272287
private fun changeTitle(title: String) {
273-
val titleText = findViewById<TextView>(R.id.title)
274-
if(titleText != null) {
288+
val titleText = findViewById<TextView>(R.id.title)
289+
if (titleText != null) {
275290
titleText.text = title
276291
}
277292
}
@@ -280,7 +295,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
280295
* Set up the toolbar, back listener, done listener.
281296
*/
282297
private fun setUpToolbar() {
283-
val back : ImageButton = findViewById(R.id.back)
298+
val back: ImageButton = findViewById(R.id.back)
284299
back.setOnClickListener { onBackPressed() }
285300
}
286301

@@ -303,24 +318,27 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
303318
/**
304319
* override Selected Images Change, update view model selected images and change UI.
305320
*/
306-
override fun onSelectedImagesChanged(selectedImages: ArrayList<Image>,
307-
selectedNotForUploadImages: Int) {
321+
override fun onSelectedImagesChanged(
322+
selectedImages: ArrayList<Image>,
323+
selectedNotForUploadImages: Int
324+
) {
308325
viewModel.selectedImages.value = selectedImages
309326

310327
if (selectedNotForUploadImages > 0) {
311-
upload.isEnabled = false
312-
upload.alpha = 0.5f
328+
bottomSheetBinding.upload.isEnabled = false
329+
bottomSheetBinding.upload.alpha = 0.5f
313330
} else {
314-
upload.isEnabled = true
315-
upload.alpha = 1f
331+
bottomSheetBinding.upload.isEnabled = true
332+
bottomSheetBinding.upload.alpha = 1f
316333
}
317334

318-
not_for_upload.text = when (selectedImages.size == selectedNotForUploadImages) {
335+
bottomSheetBinding.notForUpload.text =
336+
when (selectedImages.size == selectedNotForUploadImages) {
319337
true -> getString(R.string.unmark_as_not_for_upload)
320338
else -> getString(R.string.mark_as_not_for_upload)
321-
}
339+
}
322340

323-
val bottomLayout : ConstraintLayout = findViewById(R.id.bottom_layout)
341+
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
324342
bottomLayout.visibility = if (selectedImages.isEmpty()) View.GONE else View.VISIBLE
325343
}
326344

@@ -334,7 +352,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
334352
selectedImages: ArrayList<Image>
335353
) {
336354
val intent = Intent(this, ZoomableActivity::class.java)
337-
intent.putExtra(CustomSelectorConstants.PRESENT_POSITION, position);
355+
intent.putExtra(CustomSelectorConstants.PRESENT_POSITION, position)
338356
intent.putParcelableArrayListExtra(
339357
CustomSelectorConstants.TOTAL_SELECTED_IMAGES,
340358
selectedImages
@@ -349,8 +367,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
349367
*/
350368
fun onDone() {
351369
val selectedImages = viewModel.selectedImages.value
352-
if(selectedImages.isNullOrEmpty()) {
353-
finishPickImages(arrayListOf())
370+
if (selectedImages.isNullOrEmpty()) {
371+
finishPickImages(arrayListOf())
354372
return
355373
}
356374
var i = 0
@@ -384,7 +402,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
384402
override fun onBackPressed() {
385403
super.onBackPressed()
386404
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
387-
if(fragment != null && fragment is FolderFragment){
405+
if (fragment != null && fragment is FolderFragment) {
388406
isImageFragmentOpen = false
389407
changeTitle(getString(R.string.custom_selector_title))
390408
}
@@ -395,7 +413,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
395413
* If image fragment is open, overwrite its attributes otherwise discard the values.
396414
*/
397415
override fun onDestroy() {
398-
if(isImageFragmentOpen){
416+
if (isImageFragmentOpen) {
399417
prefs.edit().putLong(FOLDER_ID, bucketId).putString(FOLDER_NAME, bucketName).apply()
400418
} else {
401419
prefs.edit().remove(FOLDER_ID).remove(FOLDER_NAME).apply()
@@ -404,8 +422,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
404422
}
405423

406424
companion object {
407-
const val FOLDER_ID : String = "FolderId"
408-
const val FOLDER_NAME : String = "FolderName"
409-
const val ITEM_ID : String = "ItemId"
425+
const val FOLDER_ID: String = "FolderId"
426+
const val FOLDER_NAME: String = "FolderName"
427+
const val ITEM_ID: String = "ItemId"
410428
}
411429
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
xmlns:app="http://schemas.android.com/apk/res-auto">
5+
android:layout_height="match_parent">
66

7-
<include
8-
layout="@layout/custom_selector_toolbar"
9-
android:id="@+id/toolbar"
10-
/>
7+
<include layout="@layout/custom_selector_toolbar" />
118

12-
<androidx.fragment.app.FragmentContainerView
13-
android:id="@+id/fragment_container"
14-
android:layout_width="match_parent"
15-
android:layout_height="0dp"
16-
app:layout_constraintBottom_toTopOf="@id/bottom_layout"
17-
app:layout_constraintTop_toBottomOf="@+id/toolbar_layout"/>
9+
<androidx.fragment.app.FragmentContainerView
10+
android:id="@+id/fragment_container"
11+
android:layout_width="match_parent"
12+
android:layout_height="0dp"
13+
app:layout_constraintBottom_toTopOf="@id/bottom_layout"
14+
app:layout_constraintTop_toBottomOf="@+id/toolbar_layout" />
1815

19-
<include
20-
layout="@layout/custom_selector_bottom_layout"
21-
android:id="@+id/bottom_sheet"
22-
/>
16+
<include layout="@layout/custom_selector_bottom_layout" />
2317

2418
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)