Skip to content

Commit

Permalink
Merge pull request #4822 from sjudd:gallery_kotlin
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 457864906
  • Loading branch information
glide-copybara-robot committed Jun 29, 2022
2 parents 9560ead + dae0f9a commit 1f0b049
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 581 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ buildscript {
classpath 'com.guardsquare:proguard-gradle:7.1.0'
classpath "se.bjurr.violations:violations-gradle-plugin:${VIOLATIONS_PLUGIN_VERSION}"
classpath "androidx.benchmark:benchmark-gradle-plugin:${ANDROID_X_BENCHMARK_VERSION}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${JETBRAINS_KOTLIN_VERSION}"
}
}

Expand Down
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ ANDROID_X_TEST_RULES_VERSION=1.1.0
ANDROID_X_TEST_RUNNER_VERSION=1.1.0
ANDROID_X_TRACING_VERSION=1.0.0
ANDROID_X_VECTOR_DRAWABLE_ANIMATED_VERSION=1.0.0
ANDROID_X_CORE_KTX_VERSION=1.8.0
ANDROID_X_LIFECYCLE_KTX_VERSION=2.4.1

# org.jetbrains versions
JETBRAINS_KOTLINX_COROUTINES_VERSION=1.6.3
JETBRAINS_KOTLIN_VERSION=1.7.0

## Other dependency versions
ANDROID_GRADLE_VERSION=7.2.1
Expand Down
33 changes: 25 additions & 8 deletions samples/gallery/build.gradle
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: "org.jetbrains.kotlin.plugin.parcelize"

dependencies {
implementation project(':library')
implementation(project(':integration:recyclerview')) {
transitive = false
}
implementation "androidx.recyclerview:recyclerview:${ANDROID_X_RECYCLERVIEW_VERSION}"
implementation "androidx.fragment:fragment:${ANDROID_X_FRAGMENT_VERSION}"
annotationProcessor project(':annotation:compiler')

implementation "androidx.recyclerview:recyclerview:$ANDROID_X_RECYCLERVIEW_VERSION"
implementation "androidx.fragment:fragment-ktx:$ANDROID_X_FRAGMENT_VERSION"
implementation "androidx.core:core-ktx:$ANDROID_X_CORE_KTX_VERSION"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$ANDROID_X_LIFECYCLE_KTX_VERSION"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$ANDROID_X_LIFECYCLE_KTX_VERSION"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$JETBRAINS_KOTLINX_COROUTINES_VERSION"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$JETBRAINS_KOTLINX_COROUTINES_VERSION"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$JETBRAINS_KOTLIN_VERSION"

kapt project(':annotation:compiler')
}

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

android {
compileSdkVersion COMPILE_SDK_VERSION as int
compileSdkVersion 32

defaultConfig {
applicationId 'com.bumptech.glide.samples.gallery'
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
minSdkVersion 29
targetSdkVersion 32
versionCode 1
versionName '1.0'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

Expand Down
6 changes: 6 additions & 0 deletions samples/gallery/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bumptech.glide.samples.gallery">

<uses-sdk
android:minSdkVersion="29"
android:targetSdkVersion="32"
android:compileSdkVersion="32" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:label="@string/app_name"
android:icon="@android:drawable/sym_def_app_icon"
android:allowBackup="false">
<activity android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down

This file was deleted.

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()
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
}
}
}
}

This file was deleted.

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
}
}

This file was deleted.

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
}
}
Loading

0 comments on commit 1f0b049

Please sign in to comment.