@@ -25,22 +25,26 @@ class MainActivity : AppCompatActivity() {
2525
2626 binding.toolbar.title = getString(R .string.gallery_title)
2727 binding.rvGallery.layoutManager = GridLayoutManager (this , 3 )
28- binding.rvGallery.adapter = GalleryAdapter (emptyList(), emptyList()) { entry ->
29- when (entry) {
30- is GalleryAdapter .FolderEntry -> {
31- pathStack.add(PathEntry (entry.uri, entry.name))
32- loadCurrentLevel()
33- }
34- is GalleryAdapter .ImageEntry -> openFullScreen(entry)
28+
29+ val adapter = GalleryAdapter (
30+ onFolderClick = { folder ->
31+ pathStack.add(PathEntry (folder.uri, folder.name))
32+ loadCurrentLevel()
33+ },
34+ onImageClick = { index ->
35+ openFullScreen(index)
3536 }
36- }
37+ )
38+ binding.rvGallery.adapter = adapter
3739
38- binding.btnUp.setOnClickListener { goUp() }
40+ binding.toolbar.setNavigationOnClickListener { goUp() }
3941
4042 val savedUri = Prefs (this ).galleryRootUri
4143 if (savedUri != null ) {
4244 pathStack.add(PathEntry (savedUri, getString(R .string.gallery_title)))
4345 loadCurrentLevel()
46+ } else {
47+ showEmptyState(true )
4448 }
4549 }
4650
@@ -55,28 +59,35 @@ class MainActivity : AppCompatActivity() {
5559 private fun loadCurrentLevel () {
5660 val entry = pathStack.last()
5761 binding.toolbar.title = entry.title
58- binding.btnUp.visibility = if (pathStack.size > 1 ) View .VISIBLE else View .GONE
62+ binding.toolbar.navigationIcon = if (pathStack.size > 1 ) {
63+ androidx.appcompat.content.res.AppCompatResources .getDrawable(this , androidx.appcompat.R .drawable.abc_ic_ab_back_material)
64+ } else {
65+ null
66+ }
67+
68+ showEmptyState(false )
5969
6070 val cached = FolderCache .get(entry.uri)
6171 if (cached != null ) {
6272 displayEntries(cached)
6373 } else {
64- showLoading( true )
74+ binding.progressBar.visibility = View . VISIBLE
6575 }
6676
6777 Thread {
6878 try {
6979 val children = queryChildren(entry.uri)
7080 FolderCache .put(entry.uri, children)
7181 runOnUiThread {
72- showLoading( false )
82+ binding.progressBar.visibility = View . GONE
7383 displayEntries(children)
84+ if (children.isEmpty()) showEmptyState(true )
7485 }
7586 } catch (e: Exception ) {
7687 runOnUiThread {
77- showLoading( false )
88+ binding.progressBar.visibility = View . GONE
7889 if (cached == null ) {
79- showError(e )
90+ showEmptyState( true )
8091 }
8192 }
8293 }
@@ -92,9 +103,7 @@ class MainActivity : AppCompatActivity() {
92103 images.add(GalleryAdapter .ImageEntry (Uri .parse(it.uri), it.name, imgIndex++ ))
93104 }
94105
95- binding.emptyText.visibility = if (folders.isEmpty() && images.isEmpty()) View .VISIBLE else View .GONE
96-
97- (binding.rvGallery.adapter as GalleryAdapter ).update(folders, images)
106+ (binding.rvGallery.adapter as GalleryAdapter ).submitData(folders, images)
98107 }
99108
100109 private fun queryChildren (parentUri : Uri ): List <FolderCache .CachedEntry > {
@@ -185,12 +194,12 @@ class MainActivity : AppCompatActivity() {
185194 return count
186195 }
187196
188- private fun openFullScreen (entry : GalleryAdapter . ImageEntry ) {
197+ private fun openFullScreen (index : Int ) {
189198 val adapter = binding.rvGallery.adapter as GalleryAdapter
190- val imageUris = adapter.images .map { it.uri.toString() }.toTypedArray()
199+ val imageUris = adapter.getImages() .map { it.uri.toString() }.toTypedArray()
191200 val intent = android.content.Intent (this , FullScreenImageActivity ::class .java).apply {
192201 putExtra(" uris" , imageUris)
193- putExtra(" position" , entry.position )
202+ putExtra(" position" , index )
194203 }
195204 startActivity(intent)
196205 }
@@ -202,12 +211,7 @@ class MainActivity : AppCompatActivity() {
202211 }
203212 }
204213
205- private fun showLoading (show : Boolean ) {
206- binding.progressBar.visibility = if (show) View .VISIBLE else View .GONE
207- }
208-
209- private fun showError (e : Exception ) {
210- binding.emptyText.text = " ${getString(R .string.error_access)} \n ${e.message} "
211- binding.emptyText.visibility = View .VISIBLE
214+ private fun showEmptyState (show : Boolean ) {
215+ binding.emptyState.visibility = if (show) View .VISIBLE else View .GONE
212216 }
213217}
0 commit comments