mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
4664: Moved CustomSelectorActivity to ViewBinding (#5065)
This commit is contained in:
parent
10b025c237
commit
64d4ffc5f1
2 changed files with 64 additions and 52 deletions
|
|
@ -20,12 +20,14 @@ import fr.free.nrw.commons.customselector.helper.CustomSelectorConstants.SHOULD_
|
||||||
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
|
import fr.free.nrw.commons.customselector.listeners.FolderClickListener
|
||||||
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
|
import fr.free.nrw.commons.customselector.listeners.ImageSelectListener
|
||||||
import fr.free.nrw.commons.customselector.model.Image
|
import fr.free.nrw.commons.customselector.model.Image
|
||||||
|
import fr.free.nrw.commons.databinding.ActivityCustomSelectorBinding
|
||||||
|
import fr.free.nrw.commons.databinding.CustomSelectorBottomLayoutBinding
|
||||||
|
import fr.free.nrw.commons.databinding.CustomSelectorToolbarBinding
|
||||||
import fr.free.nrw.commons.filepicker.Constants
|
import fr.free.nrw.commons.filepicker.Constants
|
||||||
import fr.free.nrw.commons.media.ZoomableActivity
|
import fr.free.nrw.commons.media.ZoomableActivity
|
||||||
import fr.free.nrw.commons.theme.BaseActivity
|
import fr.free.nrw.commons.theme.BaseActivity
|
||||||
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
import fr.free.nrw.commons.upload.FileUtilsWrapper
|
||||||
import fr.free.nrw.commons.utils.CustomSelectorUtils
|
import fr.free.nrw.commons.utils.CustomSelectorUtils
|
||||||
import kotlinx.android.synthetic.main.custom_selector_bottom_layout.*
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
@ -34,7 +36,14 @@ import javax.inject.Inject
|
||||||
/**
|
/**
|
||||||
* Custom Selector Activity.
|
* Custom Selector Activity.
|
||||||
*/
|
*/
|
||||||
class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectListener {
|
class CustomSelectorActivity : BaseActivity(), FolderClickListener, ImageSelectListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ViewBindings
|
||||||
|
*/
|
||||||
|
private lateinit var binding: ActivityCustomSelectorBinding
|
||||||
|
private lateinit var toolbarBinding: CustomSelectorToolbarBinding
|
||||||
|
private lateinit var bottomSheetBinding: CustomSelectorBottomLayoutBinding
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* View model.
|
* View model.
|
||||||
|
|
@ -60,7 +69,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
/**
|
/**
|
||||||
* View Model Factory.
|
* View Model Factory.
|
||||||
*/
|
*/
|
||||||
@Inject lateinit var customSelectorViewModelFactory: CustomSelectorViewModelFactory
|
@Inject
|
||||||
|
lateinit var customSelectorViewModelFactory: CustomSelectorViewModelFactory
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NotForUploadStatus Dao class for database operations
|
* NotForUploadStatus Dao class for database operations
|
||||||
|
|
@ -77,8 +87,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
/**
|
/**
|
||||||
* Coroutine Dispatchers and Scope.
|
* Coroutine Dispatchers and Scope.
|
||||||
*/
|
*/
|
||||||
private val scope : CoroutineScope = MainScope()
|
private val scope: CoroutineScope = MainScope()
|
||||||
private var ioDispatcher : CoroutineDispatcher = Dispatchers.IO
|
private var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Image Fragment instance
|
* Image Fragment instance
|
||||||
|
|
@ -90,7 +100,11 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
*/
|
*/
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_custom_selector)
|
binding = ActivityCustomSelectorBinding.inflate(layoutInflater)
|
||||||
|
toolbarBinding = CustomSelectorToolbarBinding.bind(binding.root)
|
||||||
|
bottomSheetBinding = CustomSelectorBottomLayoutBinding.bind(binding.root)
|
||||||
|
val view = binding.root
|
||||||
|
setContentView(view)
|
||||||
|
|
||||||
prefs = applicationContext.getSharedPreferences("CustomSelector", MODE_PRIVATE)
|
prefs = applicationContext.getSharedPreferences("CustomSelector", MODE_PRIVATE)
|
||||||
viewModel = ViewModelProvider(this, customSelectorViewModelFactory).get(
|
viewModel = ViewModelProvider(this, customSelectorViewModelFactory).get(
|
||||||
|
|
@ -99,14 +113,14 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
|
|
||||||
setupViews()
|
setupViews()
|
||||||
|
|
||||||
if(prefs.getBoolean("customSelectorFirstLaunch", true)) {
|
if (prefs.getBoolean("customSelectorFirstLaunch", true)) {
|
||||||
// show welcome dialog on first launch
|
// show welcome dialog on first launch
|
||||||
showWelcomeDialog()
|
showWelcomeDialog()
|
||||||
prefs.edit().putBoolean("customSelectorFirstLaunch", false).apply()
|
prefs.edit().putBoolean("customSelectorFirstLaunch", false).apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open folder if saved in prefs.
|
// Open folder if saved in prefs.
|
||||||
if(prefs.contains(FOLDER_ID)){
|
if (prefs.contains(FOLDER_ID)) {
|
||||||
val lastOpenFolderId: Long = prefs.getLong(FOLDER_ID, 0L)
|
val lastOpenFolderId: Long = prefs.getLong(FOLDER_ID, 0L)
|
||||||
val lastOpenFolderName: String? = prefs.getString(FOLDER_NAME, null)
|
val lastOpenFolderName: String? = prefs.getString(FOLDER_NAME, null)
|
||||||
val lastItemId: Long = prefs.getLong(ITEM_ID, 0)
|
val lastItemId: Long = prefs.getLong(ITEM_ID, 0)
|
||||||
|
|
@ -120,7 +134,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
super.onActivityResult(requestCode, resultCode, data)
|
super.onActivityResult(requestCode, resultCode, data)
|
||||||
if (requestCode == Constants.RequestCodes.RECEIVE_DATA_FROM_FULL_SCREEN_MODE &&
|
if (requestCode == Constants.RequestCodes.RECEIVE_DATA_FROM_FULL_SCREEN_MODE &&
|
||||||
resultCode == Activity.RESULT_OK) {
|
resultCode == Activity.RESULT_OK
|
||||||
|
) {
|
||||||
val selectedImages: ArrayList<Image> =
|
val selectedImages: ArrayList<Image> =
|
||||||
data!!
|
data!!
|
||||||
.getParcelableArrayListExtra(CustomSelectorConstants.NEW_SELECTED_IMAGES)!!
|
.getParcelableArrayListExtra(CustomSelectorConstants.NEW_SELECTED_IMAGES)!!
|
||||||
|
|
@ -156,11 +171,11 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
* Set up bottom layout
|
* Set up bottom layout
|
||||||
*/
|
*/
|
||||||
private fun setUpBottomLayout() {
|
private fun setUpBottomLayout() {
|
||||||
val done : Button = findViewById(R.id.upload)
|
val done: Button = findViewById(R.id.upload)
|
||||||
done.setOnClickListener { onDone() }
|
done.setOnClickListener { onDone() }
|
||||||
|
|
||||||
val notForUpload : Button = findViewById(R.id.not_for_upload)
|
val notForUpload: Button = findViewById(R.id.not_for_upload)
|
||||||
notForUpload.setOnClickListener{ onClickNotForUpload() }
|
notForUpload.setOnClickListener { onClickNotForUpload() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -168,7 +183,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
*/
|
*/
|
||||||
private fun onClickNotForUpload() {
|
private fun onClickNotForUpload() {
|
||||||
val selectedImages = viewModel.selectedImages.value
|
val selectedImages = viewModel.selectedImages.value
|
||||||
if(selectedImages.isNullOrEmpty()) {
|
if (selectedImages.isNullOrEmpty()) {
|
||||||
markAsNotForUpload(arrayListOf())
|
markAsNotForUpload(arrayListOf())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +222,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
private fun insertIntoNotForUpload(images: ArrayList<Image>) {
|
private fun insertIntoNotForUpload(images: ArrayList<Image>) {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
var allImagesAlreadyNotForUpload = true
|
var allImagesAlreadyNotForUpload = true
|
||||||
images.forEach{
|
images.forEach {
|
||||||
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
|
val imageSHA1 = CustomSelectorUtils.getImageSHA1(
|
||||||
it.uri,
|
it.uri,
|
||||||
ioDispatcher,
|
ioDispatcher,
|
||||||
|
|
@ -254,7 +269,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
}
|
}
|
||||||
|
|
||||||
imageFragment!!.refresh()
|
imageFragment!!.refresh()
|
||||||
val bottomLayout : ConstraintLayout = findViewById(R.id.bottom_layout)
|
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
|
||||||
bottomLayout.visibility = View.GONE
|
bottomLayout.visibility = View.GONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -271,7 +286,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
*/
|
*/
|
||||||
private fun changeTitle(title: String) {
|
private fun changeTitle(title: String) {
|
||||||
val titleText = findViewById<TextView>(R.id.title)
|
val titleText = findViewById<TextView>(R.id.title)
|
||||||
if(titleText != null) {
|
if (titleText != null) {
|
||||||
titleText.text = title
|
titleText.text = title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -280,7 +295,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
* Set up the toolbar, back listener, done listener.
|
* Set up the toolbar, back listener, done listener.
|
||||||
*/
|
*/
|
||||||
private fun setUpToolbar() {
|
private fun setUpToolbar() {
|
||||||
val back : ImageButton = findViewById(R.id.back)
|
val back: ImageButton = findViewById(R.id.back)
|
||||||
back.setOnClickListener { onBackPressed() }
|
back.setOnClickListener { onBackPressed() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -303,24 +318,27 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
/**
|
/**
|
||||||
* override Selected Images Change, update view model selected images and change UI.
|
* override Selected Images Change, update view model selected images and change UI.
|
||||||
*/
|
*/
|
||||||
override fun onSelectedImagesChanged(selectedImages: ArrayList<Image>,
|
override fun onSelectedImagesChanged(
|
||||||
selectedNotForUploadImages: Int) {
|
selectedImages: ArrayList<Image>,
|
||||||
|
selectedNotForUploadImages: Int
|
||||||
|
) {
|
||||||
viewModel.selectedImages.value = selectedImages
|
viewModel.selectedImages.value = selectedImages
|
||||||
|
|
||||||
if (selectedNotForUploadImages > 0) {
|
if (selectedNotForUploadImages > 0) {
|
||||||
upload.isEnabled = false
|
bottomSheetBinding.upload.isEnabled = false
|
||||||
upload.alpha = 0.5f
|
bottomSheetBinding.upload.alpha = 0.5f
|
||||||
} else {
|
} else {
|
||||||
upload.isEnabled = true
|
bottomSheetBinding.upload.isEnabled = true
|
||||||
upload.alpha = 1f
|
bottomSheetBinding.upload.alpha = 1f
|
||||||
}
|
}
|
||||||
|
|
||||||
not_for_upload.text = when (selectedImages.size == selectedNotForUploadImages) {
|
bottomSheetBinding.notForUpload.text =
|
||||||
|
when (selectedImages.size == selectedNotForUploadImages) {
|
||||||
true -> getString(R.string.unmark_as_not_for_upload)
|
true -> getString(R.string.unmark_as_not_for_upload)
|
||||||
else -> getString(R.string.mark_as_not_for_upload)
|
else -> getString(R.string.mark_as_not_for_upload)
|
||||||
}
|
}
|
||||||
|
|
||||||
val bottomLayout : ConstraintLayout = findViewById(R.id.bottom_layout)
|
val bottomLayout: ConstraintLayout = findViewById(R.id.bottom_layout)
|
||||||
bottomLayout.visibility = if (selectedImages.isEmpty()) View.GONE else View.VISIBLE
|
bottomLayout.visibility = if (selectedImages.isEmpty()) View.GONE else View.VISIBLE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -334,7 +352,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
selectedImages: ArrayList<Image>
|
selectedImages: ArrayList<Image>
|
||||||
) {
|
) {
|
||||||
val intent = Intent(this, ZoomableActivity::class.java)
|
val intent = Intent(this, ZoomableActivity::class.java)
|
||||||
intent.putExtra(CustomSelectorConstants.PRESENT_POSITION, position);
|
intent.putExtra(CustomSelectorConstants.PRESENT_POSITION, position)
|
||||||
intent.putParcelableArrayListExtra(
|
intent.putParcelableArrayListExtra(
|
||||||
CustomSelectorConstants.TOTAL_SELECTED_IMAGES,
|
CustomSelectorConstants.TOTAL_SELECTED_IMAGES,
|
||||||
selectedImages
|
selectedImages
|
||||||
|
|
@ -349,7 +367,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
*/
|
*/
|
||||||
fun onDone() {
|
fun onDone() {
|
||||||
val selectedImages = viewModel.selectedImages.value
|
val selectedImages = viewModel.selectedImages.value
|
||||||
if(selectedImages.isNullOrEmpty()) {
|
if (selectedImages.isNullOrEmpty()) {
|
||||||
finishPickImages(arrayListOf())
|
finishPickImages(arrayListOf())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -384,7 +402,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
override fun onBackPressed() {
|
override fun onBackPressed() {
|
||||||
super.onBackPressed()
|
super.onBackPressed()
|
||||||
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
|
val fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
|
||||||
if(fragment != null && fragment is FolderFragment){
|
if (fragment != null && fragment is FolderFragment) {
|
||||||
isImageFragmentOpen = false
|
isImageFragmentOpen = false
|
||||||
changeTitle(getString(R.string.custom_selector_title))
|
changeTitle(getString(R.string.custom_selector_title))
|
||||||
}
|
}
|
||||||
|
|
@ -395,7 +413,7 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
* If image fragment is open, overwrite its attributes otherwise discard the values.
|
* If image fragment is open, overwrite its attributes otherwise discard the values.
|
||||||
*/
|
*/
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
if(isImageFragmentOpen){
|
if (isImageFragmentOpen) {
|
||||||
prefs.edit().putLong(FOLDER_ID, bucketId).putString(FOLDER_NAME, bucketName).apply()
|
prefs.edit().putLong(FOLDER_ID, bucketId).putString(FOLDER_NAME, bucketName).apply()
|
||||||
} else {
|
} else {
|
||||||
prefs.edit().remove(FOLDER_ID).remove(FOLDER_NAME).apply()
|
prefs.edit().remove(FOLDER_ID).remove(FOLDER_NAME).apply()
|
||||||
|
|
@ -404,8 +422,8 @@ class CustomSelectorActivity: BaseActivity(), FolderClickListener, ImageSelectLi
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val FOLDER_ID : String = "FolderId"
|
const val FOLDER_ID: String = "FolderId"
|
||||||
const val FOLDER_NAME : String = "FolderName"
|
const val FOLDER_NAME: String = "FolderName"
|
||||||
const val ITEM_ID : String = "ItemId"
|
const val ITEM_ID: String = "ItemId"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,24 +1,18 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent">
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
|
||||||
|
|
||||||
<include
|
<include layout="@layout/custom_selector_toolbar" />
|
||||||
layout="@layout/custom_selector_toolbar"
|
|
||||||
android:id="@+id/toolbar"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<androidx.fragment.app.FragmentContainerView
|
<androidx.fragment.app.FragmentContainerView
|
||||||
android:id="@+id/fragment_container"
|
android:id="@+id/fragment_container"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0dp"
|
android:layout_height="0dp"
|
||||||
app:layout_constraintBottom_toTopOf="@id/bottom_layout"
|
app:layout_constraintBottom_toTopOf="@id/bottom_layout"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/toolbar_layout"/>
|
app:layout_constraintTop_toBottomOf="@+id/toolbar_layout" />
|
||||||
|
|
||||||
<include
|
<include layout="@layout/custom_selector_bottom_layout" />
|
||||||
layout="@layout/custom_selector_bottom_layout"
|
|
||||||
android:id="@+id/bottom_sheet"
|
|
||||||
/>
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue