forked from bumptech/glide
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Gallery sample to Kotlin using a ViewModel and Flow.
- Loading branch information
Showing
16 changed files
with
423 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/GalleryModule.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/GalleryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.bumptech.glide.samples.gallery | ||
|
||
import com.bumptech.glide.annotation.GlideModule | ||
import com.bumptech.glide.module.AppGlideModule | ||
|
||
/** Ensures that Glide's generated API is created for the Gallery sample. */ | ||
@GlideModule | ||
class GalleryModule : AppGlideModule() |
25 changes: 25 additions & 0 deletions
25
samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/GalleryViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.bumptech.glide.samples.gallery | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.launch | ||
|
||
class GalleryViewModel(application: Application) : AndroidViewModel(application) { | ||
private val mediaStoreDataSource = MediaStoreDataSource(application) | ||
|
||
private val _uiState: MutableStateFlow<List<MediaStoreData>> = MutableStateFlow(emptyList()) | ||
val mediaStoreData: StateFlow<List<MediaStoreData>> = _uiState | ||
|
||
init { | ||
viewModelScope.launch { | ||
mediaStoreDataSource.loadMediaStoreData().flowOn(Dispatchers.IO).collect { | ||
_uiState.value = it | ||
} | ||
} | ||
} | ||
} |
61 changes: 0 additions & 61 deletions
61
...s/gallery/src/main/java/com/bumptech/glide/samples/gallery/HorizontalGalleryFragment.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
...les/gallery/src/main/java/com/bumptech/glide/samples/gallery/HorizontalGalleryFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.bumptech.glide.samples.gallery | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.recyclerview.widget.GridLayoutManager | ||
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader | ||
import kotlinx.coroutines.launch | ||
|
||
/** Displays media store data in a recycler view. */ | ||
class HorizontalGalleryFragment : Fragment() { | ||
private lateinit var adapter: RecyclerAdapter | ||
private lateinit var recyclerView: RecyclerView | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val galleryViewModel: GalleryViewModel by viewModels() | ||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
galleryViewModel.mediaStoreData.collect { data -> | ||
adapter.setData(data) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?, | ||
): View? { | ||
val result = inflater.inflate(R.layout.recycler_view, container, false) | ||
recyclerView = result.findViewById<View>(R.id.recycler_view) as RecyclerView | ||
val layoutManager = GridLayoutManager(activity, 1) | ||
layoutManager.orientation = RecyclerView.HORIZONTAL | ||
recyclerView.layoutManager = layoutManager | ||
recyclerView.setHasFixedSize(true) | ||
|
||
val glideRequests = GlideApp.with(this) | ||
adapter = RecyclerAdapter(requireContext(), glideRequests) | ||
val preloader = RecyclerViewPreloader(glideRequests, adapter, adapter, 3) | ||
recyclerView.addOnScrollListener(preloader) | ||
recyclerView.adapter = adapter | ||
return result | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/MainActivity.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
samples/gallery/src/main/java/com/bumptech/glide/samples/gallery/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.bumptech.glide.samples.gallery | ||
|
||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentActivity | ||
import com.bumptech.glide.MemoryCategory | ||
|
||
/** Displays a [HorizontalGalleryFragment]. */ | ||
class MainActivity : FragmentActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.main_activity) | ||
GlideApp.get(this).setMemoryCategory(MemoryCategory.HIGH) | ||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) | ||
!= PackageManager.PERMISSION_GRANTED | ||
) { | ||
requestStoragePermission() | ||
} else { | ||
replaceFragment() | ||
} | ||
} | ||
|
||
private fun requestStoragePermission() { | ||
ActivityCompat.requestPermissions( | ||
this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_READ_STORAGE) | ||
} | ||
|
||
private fun replaceFragment() { | ||
val fragment: Fragment = HorizontalGalleryFragment() | ||
supportFragmentManager | ||
.beginTransaction() | ||
.replace(R.id.fragment_container, fragment) | ||
.commit() | ||
} | ||
|
||
override fun onRequestPermissionsResult( | ||
requestCode: Int, permissions: Array<String>, grantResults: IntArray, | ||
) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
when (requestCode) { | ||
REQUEST_READ_STORAGE -> { | ||
// If request is cancelled, the result arrays are empty. | ||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
replaceFragment() | ||
} else { | ||
Toast.makeText(this, "Storage permission is required", Toast.LENGTH_LONG).show() | ||
requestStoragePermission() | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val REQUEST_READ_STORAGE = 0 | ||
} | ||
} |
Oops, something went wrong.