Skip to content

Commit 36154fb

Browse files
author
Majid Arabi
committed
bugs fixed - selected items visibility,
added usage sample
1 parent a0b4267 commit 36154fb

File tree

8 files changed

+171
-45
lines changed

8 files changed

+171
-45
lines changed

app/build.gradle

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ android {
2929
kotlinOptions {
3030
jvmTarget = '1.8'
3131
}
32+
buildFeatures {
33+
viewBinding true
34+
}
3235
}
3336

3437
dependencies {
35-
3638
implementation project(':file-picker')
3739
implementation 'androidx.core:core-ktx:1.7.0'
3840
implementation 'androidx.appcompat:appcompat:1.4.1'
39-
41+
implementation 'androidx.recyclerview:recyclerview:1.2.1'
42+
implementation 'com.google.android.material:material:1.5.0'
4043
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package ir.one_developer.filepickerlibrary
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.core.net.toUri
6+
import androidx.recyclerview.widget.DiffUtil
7+
import androidx.recyclerview.widget.ListAdapter
8+
import androidx.recyclerview.widget.RecyclerView
9+
import com.github.file_picker.model.Media
10+
import ir.one_developer.filepickerlibrary.databinding.FileLayoutBinding
11+
import java.io.File
12+
13+
class FileAdapter : ListAdapter<Media, FileAdapter.VH>(Comparator) {
14+
15+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
16+
VH(FileLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
17+
18+
override fun onBindViewHolder(holder: VH, position: Int) {
19+
holder.bind(getItem(position).file)
20+
}
21+
22+
inner class VH(
23+
private val binding: FileLayoutBinding
24+
) : RecyclerView.ViewHolder(binding.root) {
25+
fun bind(file: File) {
26+
binding.ivImagePreview.setImageURI(file.toUri())
27+
}
28+
}
29+
30+
companion object {
31+
private val Comparator = object : DiffUtil.ItemCallback<Media>() {
32+
override fun areItemsTheSame(oldItem: Media, newItem: Media): Boolean {
33+
return oldItem.id == newItem.id
34+
}
35+
36+
override fun areContentsTheSame(oldItem: Media, newItem: Media): Boolean {
37+
return oldItem == newItem
38+
}
39+
}
40+
}
41+
42+
}

app/src/main/java/ir/one_developer/filepickerlibrary/MainActivity.kt

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,51 @@ package ir.one_developer.filepickerlibrary
22

33
import android.os.Bundle
44
import android.util.Log
5-
import android.widget.TextView
65
import androidx.appcompat.app.AppCompatActivity
76
import androidx.core.content.ContextCompat
8-
import com.github.file_picker.FilePicker
9-
import com.github.file_picker.FileType
10-
import com.github.file_picker.ListDirection
7+
import com.github.file_picker.model.Media
8+
import com.github.file_picker.showFilePicker
9+
import ir.one_developer.filepickerlibrary.databinding.ActivityMainBinding
1110

1211
class MainActivity : AppCompatActivity() {
12+
13+
private lateinit var adapter: FileAdapter
14+
private val selectedFiles = arrayListOf<Media>()
15+
private lateinit var binding: ActivityMainBinding
16+
17+
companion object {
18+
private const val TAG = "MainActivity"
19+
}
20+
1321
override fun onCreate(savedInstanceState: Bundle?) {
1422
super.onCreate(savedInstanceState)
15-
setContentView(R.layout.activity_main)
16-
17-
var video = false
18-
val button = findViewById<TextView>(R.id.btn_show_files)
19-
button.setOnClickListener {
20-
FilePicker.show(
21-
activity = this,
22-
gridSpanCount = 3,
23-
limitItemSelection = 5,
24-
listDirection = ListDirection.RTL,
25-
fileType = if (video) FileType.VIDEO else FileType.IMAGE,
26-
titleTextColor = ContextCompat.getColor(this, R.color.black),
27-
submitTextColor = ContextCompat.getColor(this, R.color.white),
28-
accentColor = ContextCompat.getColor(this, R.color.purple_200),
29-
) {
30-
Log.e("Files", "$it")
31-
}
32-
video = !video
33-
button.text = if (video) "Show Videos" else "Show Images"
23+
binding = ActivityMainBinding.inflate(layoutInflater)
24+
setContentView(binding.root)
25+
setupViews()
26+
}
27+
28+
private fun setupViews() = binding.apply {
29+
adapter = FileAdapter()
30+
rvFiles.adapter = adapter
31+
btnOpenFiles.setOnClickListener {
32+
showFiles()
3433
}
34+
}
3535

36+
private fun showFiles(): Unit = showFilePicker(
37+
limitItemSelection = 2,
38+
selectedFiles = selectedFiles,
39+
accentColor = ContextCompat.getColor(this@MainActivity, R.color.purple_700),
40+
titleTextColor = ContextCompat.getColor(this@MainActivity, R.color.purple_700)
41+
) {
42+
adapter.submitList(it)
43+
updateSelectedFiles(it)
44+
Log.i(TAG, "FilePicker:SelectedItems: $selectedFiles")
3645
}
46+
47+
private fun updateSelectedFiles(files: List<Media>) {
48+
selectedFiles.clear()
49+
selectedFiles.addAll(files)
50+
}
51+
3752
}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
6-
android:background="@color/white"
7-
android:gravity="center"
87
android:orientation="vertical"
98
tools:context=".MainActivity">
109

11-
<androidx.appcompat.widget.AppCompatButton
12-
android:id="@+id/btn_show_files"
13-
android:layout_width="wrap_content"
10+
<androidx.recyclerview.widget.RecyclerView
11+
android:id="@+id/rv_files"
12+
android:layout_width="match_parent"
13+
android:layout_height="0dp"
14+
android:layout_weight="1"
15+
android:clipToPadding="false"
16+
android:layoutDirection="rtl"
17+
android:padding="2dp"
18+
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
19+
app:spanCount="1"
20+
tools:itemCount="5"
21+
tools:listitem="@layout/file_layout" />
22+
23+
<com.google.android.material.button.MaterialButton
24+
android:id="@+id/btn_open_files"
25+
android:layout_width="match_parent"
1426
android:layout_height="wrap_content"
15-
android:text="Show Images"
16-
android:textSize="22sp" />
27+
android:layout_marginHorizontal="16dp"
28+
android:layout_marginVertical="8dp"
29+
android:gravity="center"
30+
android:paddingVertical="16dp"
31+
android:text="Show Files"
32+
android:textColor="@color/white"
33+
android:textSize="22sp"
34+
app:backgroundTint="@color/purple_700"
35+
app:cornerRadius="16dp" />
1736

1837
</androidx.appcompat.widget.LinearLayoutCompat>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="wrap_content">
7+
8+
<androidx.cardview.widget.CardView
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:layout_margin="2dp"
12+
app:cardElevation="0dp"
13+
app:cardPreventCornerOverlap="false">
14+
15+
<androidx.appcompat.widget.AppCompatImageView
16+
android:id="@+id/iv_image_preview"
17+
android:layout_width="match_parent"
18+
android:layout_height="match_parent"
19+
android:adjustViewBounds="true"
20+
android:scaleType="centerCrop"
21+
tools:src="@tools:sample/avatars" />
22+
23+
</androidx.cardview.widget.CardView>
24+
25+
</FrameLayout>

file-picker/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ afterEvaluate {
5858
from components.release
5959
groupId = 'com.github.majidarabi'
6060
artifactId = 'file-picker'
61-
version = '0.0.4'
61+
version = '0.0.5'
6262
}
6363
}
6464
}

file-picker/src/main/java/com/github/file_picker/FilePicker.kt

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class FilePicker : BottomSheetDialogFragment() {
6464
private var requestPermission =
6565
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
6666
if (isGranted) loadFiles()
67+
else dismissAllowingStateLoss()
6768
}
6869

6970
override fun onCreate(savedInstanceState: Bundle?) {
@@ -150,22 +151,24 @@ class FilePicker : BottomSheetDialogFragment() {
150151
}
151152

152153
private fun setupViews() = binding.apply {
154+
setSubmitButtonDisableColors()
153155
setupRecyclerView(rvFiles)
154156
setFixedSubmitButton()
155157
showSelectedCount()
156-
setButtonEnabled()
157158

158-
btnSubmit.text = submitText
159159
cardLine.setCardBackgroundColor(ColorStateList.valueOf(accentColor))
160160
progress.indeterminateTintList = ColorStateList.valueOf(accentColor)
161161
tvTitle.apply {
162162
text = title
163163
setTextColor(titleTextColor)
164164
}
165165

166-
btnSubmit.setOnClickListener {
167-
submitList()
168-
dismissAllowingStateLoss()
166+
btnSubmit.apply {
167+
text = submitText
168+
setOnClickListener {
169+
submitList()
170+
dismissAllowingStateLoss()
171+
}
169172
}
170173
}
171174

@@ -176,13 +179,13 @@ class FilePicker : BottomSheetDialogFragment() {
176179

177180
private fun setupRecyclerView(rvFiles: RecyclerView) {
178181
itemAdapter = ItemAdapter().apply {
182+
setLimitCount(limitCount)
183+
setAccentColor(accentColor)
179184
setOnItemClickListener { itemPosition ->
180185
itemAdapter?.setSelect(itemPosition)
181186
showSelectedCount()
182187
setButtonEnabled()
183188
}
184-
setLimitCount(limitCount)
185-
setAccentColor(accentColor)
186189
}
187190
rvFiles.apply {
188191
layoutDirection = when (listDirection) {
@@ -200,25 +203,43 @@ class FilePicker : BottomSheetDialogFragment() {
200203
btnSubmit.apply {
201204
isEnabled = hasSelected
202205
if (isEnabled) {
203-
setTextColor(submitTextColor)
204-
setBackgroundColor(accentColor)
206+
setSubmitButtonEnableColors()
205207
} else {
206-
setTextColor(Color.GRAY)
207-
setBackgroundColor(Color.LTGRAY)
208+
setSubmitButtonDisableColors()
208209
}
209210
}
210211
}
211212
}
212213

214+
private fun setSubmitButtonEnableColors() = binding.btnSubmit.apply {
215+
setTextColor(submitTextColor)
216+
setBackgroundColor(accentColor)
217+
}
218+
219+
private fun setSubmitButtonDisableColors() = binding.btnSubmit.apply {
220+
setTextColor(Color.GRAY)
221+
setBackgroundColor(Color.LTGRAY)
222+
}
223+
213224
private fun loadFiles() = CoroutineScope(Dispatchers.IO).launch {
214225
val files = getStorageFiles(fileType = fileType)
215-
.map { Media(it) }
226+
.map { Media(file = it) }
216227
.sortedByDescending { it.file.lastModified() }
228+
229+
selectedFiles.forEach { media ->
230+
files.forEach {
231+
if (it.id == media.id) {
232+
it.isSelected = media.isSelected
233+
}
234+
}
235+
}
236+
217237
requireActivity().runOnUiThread {
218238
itemAdapter?.submitList(files)
219239
binding.progress.isVisible = false
220240
setFixedSubmitButton()
221241
showSelectedCount()
242+
setButtonEnabled()
222243
}
223244
}
224245

file-picker/src/main/res/layout/file_picker.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
android:paddingVertical="8dp"
7777
android:textColor="@android:color/white"
7878
android:textSize="22sp"
79+
android:enabled="false"
7980
app:cornerRadius="16dp"
8081
tools:text="Submit" />
8182

0 commit comments

Comments
 (0)