From 3e398ebfbb6a2a3d167123d98af5ef61eba4d6ea Mon Sep 17 00:00:00 2001 From: "DESKTOP-6JRHMBT\\yazan" Date: Thu, 26 Nov 2020 03:00:22 -0800 Subject: [PATCH 1/8] Some Enhancments --- app/build.gradle | 6 ++-- app/src/main/AndroidManifest.xml | 2 +- .../supernova/gligarexample/MainActivity.kt | 29 +++++++---------- gligar/build.gradle | 10 +++--- .../opensooq/supernova/gligar/GligarPicker.kt | 7 ++++- .../supernova/gligar/ui/GligarScreenResult.kt | 31 +++++++++++++++++++ .../gligar/ui/ImagePickerActivity.kt | 2 ++ .../supernova/gligar/ui/PickerViewModel.kt | 30 +++++++++++++++--- .../opensooq/supernova/gligar/utils/Consts.kt | 2 +- 9 files changed, 88 insertions(+), 31 deletions(-) create mode 100644 gligar/src/main/java/com/opensooq/supernova/gligar/ui/GligarScreenResult.kt diff --git a/app/build.gradle b/app/build.gradle index 8bc9144..9e8a8d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -33,8 +33,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' + implementation "androidx.activity:activity-ktx:1.2.0-beta01" + implementation "androidx.fragment:fragment-ktx:1.3.0-beta01" + implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation project(':gligar') + implementation 'androidx.appcompat:appcompat:1.2.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6fe7740..ab17077 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt index 04d4233..c5e393b 100644 --- a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt @@ -5,34 +5,29 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.opensooq.supernova.gligar.GligarPicker +import com.opensooq.supernova.gligar.ui.GligarScreenResult import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { - val PICKER_REQUEST_CODE = 30 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - GligarPicker().limit(10).disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) - .withActivity(this).show() - } - - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - if (resultCode != Activity.RESULT_OK) { - return - } - when (requestCode) { - PICKER_REQUEST_CODE -> { - val imagesList = data?.extras?.getStringArray(GligarPicker.IMAGES_RESULT) - if (!imagesList.isNullOrEmpty()) { - imagesCount.text = "Number of selected Images: ${imagesList.size}" - } + val startForResult = registerForActivityResult(object : GligarScreenResult() { + override fun getGligarIntentLauncher(): Intent { + return GligarPicker().limit(10).disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) + .withActivity(this@MainActivity).show() + } + }) { + if (!it.isNullOrEmpty()) { + imagesCount.text = "Number of selected Images: ${it.size}" } } + + startForResult.launch(0) } + } diff --git a/gligar/build.gradle b/gligar/build.gradle index d2df5ad..b54e4bb 100644 --- a/gligar/build.gradle +++ b/gligar/build.gradle @@ -61,8 +61,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation "androidx.activity:activity-ktx:1.2.0-beta01" + implementation "androidx.fragment:fragment-ktx:1.3.0-beta01" + implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04' implementation "androidx.lifecycle:lifecycle-runtime:2.1.0-alpha04" @@ -73,8 +75,8 @@ dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0' implementation "android.arch.paging:runtime:1.0.1" - implementation 'com.github.bumptech.glide:glide:4.9.0' - annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' + implementation 'com.github.bumptech.glide:glide:4.11.0' + annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' implementation 'com.google.android.material:material:1.2.0-alpha02' implementation 'com.android.support.constraint:constraint-layout:1.1.3' diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/GligarPicker.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/GligarPicker.kt index 038d2a7..b759c5c 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/GligarPicker.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/GligarPicker.kt @@ -6,6 +6,7 @@ import androidx.fragment.app.Fragment import com.opensooq.supernova.gligar.ui.ImagePickerActivity.Companion.EXTRA_CAMERA_DIRECT import com.opensooq.supernova.gligar.ui.ImagePickerActivity.Companion.EXTRA_DISABLE_CAMERA import com.opensooq.supernova.gligar.ui.ImagePickerActivity.Companion.EXTRA_LIMIT +import com.opensooq.supernova.gligar.ui.ImagePickerActivity.Companion.EXTRA_SINGLE_SELECTION import com.opensooq.supernova.gligar.ui.ImagePickerActivity.Companion.startActivityForResult import java.lang.IllegalStateException @@ -26,17 +27,19 @@ class GligarPicker { private var limit: Int = 10 private var disableCamera: Boolean = false private var cameraDirect: Boolean = false + private var isSingleSelection: Boolean = false fun requestCode(requestCode: Int) = apply { this.requestCode = requestCode } fun limit(limit: Int) = apply { this.limit = limit } fun disableCamera(disableCamera: Boolean) = apply { this.disableCamera = disableCamera } + fun singleSelection(isSingleSelection: Boolean) = apply { this.isSingleSelection = isSingleSelection } fun cameraDirect(cameraDirect: Boolean) = apply { this.cameraDirect = cameraDirect } fun withActivity(activity: Activity) = apply { this.withActivity = activity } fun withFragment(fragment: Fragment) = apply { this.withFragment = fragment } - fun show() { + fun show(): Intent { if(withActivity == null && withFragment ==null){ throw IllegalStateException("Activity or fragment should be passed, use withActivity(activity) or withFragment(fragment) to set any.") } @@ -44,6 +47,7 @@ class GligarPicker { val intent = Intent() intent.putExtra(EXTRA_LIMIT, limit) intent.putExtra(EXTRA_CAMERA_DIRECT, cameraDirect) + intent.putExtra(EXTRA_SINGLE_SELECTION, isSingleSelection) if (!cameraDirect) { intent.putExtra(EXTRA_DISABLE_CAMERA, disableCamera) } @@ -54,5 +58,6 @@ class GligarPicker { startActivityForResult(withFragment!!,requestCode,intent) } + return intent } } \ No newline at end of file diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/GligarScreenResult.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/GligarScreenResult.kt new file mode 100644 index 0000000..fb9bfa0 --- /dev/null +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/GligarScreenResult.kt @@ -0,0 +1,31 @@ +package com.opensooq.supernova.gligar.ui + +import android.app.Activity +import android.content.Context +import android.content.Intent +import androidx.activity.result.contract.ActivityResultContract +import androidx.fragment.app.FragmentActivity +import com.opensooq.supernova.gligar.GligarPicker + +abstract class GligarScreenResult : ActivityResultContract?>() { + + companion object { + const val ACTION = "com.opensooq.supernova.gligar.action.GLIGAR_SCREEN_ACTION" + val PICKER_REQUEST_CODE = 30 + + } + + override fun createIntent(context: Context, input: Any?): Intent { + return getGligarIntentLauncher() + } + + override fun parseResult(resultCode: Int, intent: Intent?): Array? { + return when (resultCode) { + Activity.RESULT_OK -> intent?.extras?.getStringArray(GligarPicker.IMAGES_RESULT) + else -> null + } + } + + abstract fun getGligarIntentLauncher(): Intent + +} diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt index a18f5a8..65acc7a 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt @@ -55,6 +55,7 @@ internal class ImagePickerActivity : AppCompatActivity(), LoadMoreListener.OnLoa companion object { const val EXTRA_LIMIT = "limit" const val EXTRA_CAMERA_DIRECT = "camera_direct" + const val EXTRA_SINGLE_SELECTION = "single_selection" const val EXTRA_DISABLE_CAMERA = "disable_camera" const val STORAGE_PERMISSION_REQUEST_CODE = 100 const val CAMERA_PERMISSION_REQUEST_CODE = 101 @@ -86,6 +87,7 @@ internal class ImagePickerActivity : AppCompatActivity(), LoadMoreListener.OnLoa private var isPermissionGranted = false private var isSaveState = false private var forceCamera = false + private var isSingleSelection = false private lateinit var icDone: ImageView private lateinit var alertBtn: MaterialButton diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt index 4b4d0bf..155b7e5 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt @@ -15,6 +15,7 @@ import com.opensooq.supernova.gligar.utils.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java.lang.Exception /** * Created by Hani AlMomani on 24,September,2019 @@ -188,7 +189,7 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : savedStateHandle.set(SELECTED_ALBUM, mSelectedAlbum) savedStateHandle.set(SELECTED_IMAGES, mSelectedList) savedStateHandle.set(CURRENT_SELECTION, mCurrentSelection) - savedStateHandle.set(LIMIT, mLimit) + savedStateHandle.set(LIMIT_NUMBER, mLimit) savedStateHandle.set(DISABLE_CAMERA, mCameraCisabled) } @@ -197,13 +198,32 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : saveStateImages = savedStateHandle.get(IMAGES) ?: arrayListOf() mAlbums.value = savedStateHandle.get(ALBUMS) ?: arrayListOf() mCurrentPhotoPath = savedStateHandle.get(PHOTO_PATH) - mCurrentSelectedAlbum = savedStateHandle.get(ALBUM_POS) ?: 0 - mPage = savedStateHandle.get(PAGE) ?: 0 mSelectedAlbum = savedStateHandle.get(SELECTED_ALBUM) mSelectedList = savedStateHandle.get(SELECTED_IMAGES) ?: hashMapOf() - mCurrentSelection = savedStateHandle.get(CURRENT_SELECTION) ?: 0 - mLimit = savedStateHandle.get(LIMIT) ?: 0 mCameraCisabled = savedStateHandle.get(DISABLE_CAMERA) ?: false + mPage = try { + savedStateHandle.get(PAGE) ?: 0 + } catch (ex: Exception) { + 0 + } + + mCurrentSelection = try { + savedStateHandle.get(CURRENT_SELECTION) ?: 0 + } catch (ex: Exception) { + 0 + } + + mLimit = try { + savedStateHandle.get(LIMIT_NUMBER) ?: 0 + } catch (ex: Exception) { + 0 + } + + mCurrentSelectedAlbum = try { + savedStateHandle.get(ALBUM_POS) ?: 0 + } catch (ex: Exception) { + 0 + } } } \ No newline at end of file diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/utils/Consts.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/utils/Consts.kt index 6987cd0..f60f8b7 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/utils/Consts.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/utils/Consts.kt @@ -22,5 +22,5 @@ internal const val PAGE = "page" internal const val SELECTED_ALBUM = "selected_album" internal const val SELECTED_IMAGES = "selected_images" internal const val CURRENT_SELECTION = "curren_selection" -internal const val LIMIT = "limit" +internal const val LIMIT_NUMBER = "limit" internal const val DISABLE_CAMERA = "limit" From 9dd869906f34fcc2056bfef789abc7233123430d Mon Sep 17 00:00:00 2001 From: Yazan Tarifi Date: Thu, 26 Nov 2020 14:48:56 +0200 Subject: [PATCH 2/8] Add Single Selection Mode --- .../supernova/gligarexample/MainActivity.kt | 4 +- .../gligar/dataSource/ImagesDataSource.kt | 2 +- .../gligar/ui/ImagePickerActivity.kt | 1 - .../supernova/gligar/ui/PickerViewModel.kt | 42 ++++++++++++++++--- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt index c5e393b..803e575 100644 --- a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt @@ -17,7 +17,9 @@ class MainActivity : AppCompatActivity() { val startForResult = registerForActivityResult(object : GligarScreenResult() { override fun getGligarIntentLauncher(): Intent { - return GligarPicker().limit(10).disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) + return GligarPicker() + .singleSelection(true) + .disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) .withActivity(this@MainActivity).show() } }) { diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt index 1e2334a..e43c0a8 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/dataSource/ImagesDataSource.kt @@ -84,7 +84,7 @@ internal class ImagesDataSource(private val contentResolver: ContentResolver){ ) } photoCursor?.isAfterLast ?: return list - photoCursor.doWhile { + while(photoCursor.moveToNext()) { val image = photoCursor.getString((photoCursor.getColumnIndex(PATH_COLUMN))) list.add(ImageItem(image, ImageSource.GALLERY, 0)) } diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt index 65acc7a..19e6c88 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/ImagePickerActivity.kt @@ -87,7 +87,6 @@ internal class ImagePickerActivity : AppCompatActivity(), LoadMoreListener.OnLoa private var isPermissionGranted = false private var isSaveState = false private var forceCamera = false - private var isSingleSelection = false private lateinit var icDone: ImageView private lateinit var alertBtn: MaterialButton diff --git a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt index 155b7e5..ebe270a 100644 --- a/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt +++ b/gligar/src/main/java/com/opensooq/supernova/gligar/ui/PickerViewModel.kt @@ -15,7 +15,6 @@ import com.opensooq.supernova.gligar.utils.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import java.lang.Exception /** * Created by Hani AlMomani on 24,September,2019 @@ -47,6 +46,7 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : private var mSelectedList = hashMapOf() private var mCurrentSelection: Int = 0 private var mLimit = 0 + private var isSingleSelectionEnabled = false private var mCameraCisabled: Boolean = true private lateinit var mImageDataSource: ImagesDataSource @@ -62,6 +62,7 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : mLimit = extras.getInt(EXTRA_LIMIT, 0) mCameraCisabled = extras.getBoolean(ImagePickerActivity.EXTRA_DISABLE_CAMERA, false) mDirectCamera.value = extras.getBoolean(ImagePickerActivity.EXTRA_CAMERA_DIRECT, false) + isSingleSelectionEnabled = extras.getBoolean(ImagePickerActivity.EXTRA_SINGLE_SELECTION, false) } @@ -109,8 +110,15 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : if (mCurrentPhotoPath.isNullOrEmpty()) { return } - val imageItem = - ImageItem(mCurrentPhotoPath!!, ImageSource.GALLERY, getCurrentSelectionCountForCamera()) + val imageItem = ImageItem(mCurrentPhotoPath!!, ImageSource.GALLERY, getCurrentSelectionCountForCamera()) + if (isSingleSelectionEnabled) { + if (mSelectedList.size > 0) { + imageItem.selected = 0 + } else { + imageItem.selected = 1 + } + } + mSelectedList[imageItem.imagePath] = imageItem adapterItems?.add(1, imageItem) mNotifyInsert.value = 1 @@ -118,6 +126,10 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : internal fun setImageSelection(position: Int, adapterImageItem: ArrayList?) { + if (isSingleSelectionEnabled) { + mSelectedList.clear() + } + if (adapterImageItem.isNullOrEmpty()) { return } @@ -132,7 +144,11 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : showOverLimit.value = true return } - mCurrentSelection++ + if (isSingleSelectionEnabled) { + mCurrentSelection = 1 + } else { + mCurrentSelection++ + } imageItem.selected = mCurrentSelection mSelectedList[imageItem.imagePath] = imageItem } else { @@ -148,11 +164,25 @@ internal class PickerViewModel(private val savedStateHandle: SavedStateHandle) : } mNotifyPosition.value = position mDoneEnabled.value = getCurrentSelection() > 0 - } + if (isSingleSelectionEnabled) { + for ((i, mItem) in adapterImageItem.withIndex()) { + if (!mItem.imagePath.equals(imageItem.imagePath)) { + if (mItem.selected > 0) { + mItem.selected = 0 + mNotifyPosition.value = i + } + } + } + } + } private fun getCurrentSelectionCountForCamera(): Int { - mCurrentSelection++ + if (isSingleSelectionEnabled) { + mCurrentSelection = 1 + } else { + mCurrentSelection++ + } return mCurrentSelection } From 7f9128276f174b865b6073c681067e7c7bb35f7a Mon Sep 17 00:00:00 2001 From: Yazan Tarifi Date: Sat, 28 Nov 2020 11:32:41 +0200 Subject: [PATCH 3/8] Support Image Type --- app/src/main/AndroidManifest.xml | 5 ++- ...nActivity.kt => ActivityResultLauncher.kt} | 5 +-- .../gligarexample/MainScreenLauncher.kt | 26 ++++++++++++ .../gligarexample/NormalActivityStarter.kt | 38 +++++++++++++++++ .../gligarexample/SupportedFilesLauncher.kt | 32 +++++++++++++++ app/src/main/res/layout/activity_main.xml | 2 +- .../res/layout/activity_main_launcher.xml | 41 +++++++++++++++++++ .../opensooq/supernova/gligar/GligarPicker.kt | 17 ++++++++ .../gligar/dataSource/ImagesDataSource.kt | 12 +++++- .../supernova/gligar/ui/GligarScreenResult.kt | 2 - .../gligar/ui/ImagePickerActivity.kt | 1 + .../supernova/gligar/ui/PickerViewModel.kt | 10 ++++- .../opensooq/supernova/gligar/utils/Consts.kt | 1 + 13 files changed, 181 insertions(+), 11 deletions(-) rename app/src/main/java/com/opensooq/supernova/gligarexample/{MainActivity.kt => ActivityResultLauncher.kt} (88%) create mode 100644 app/src/main/java/com/opensooq/supernova/gligarexample/MainScreenLauncher.kt create mode 100644 app/src/main/java/com/opensooq/supernova/gligarexample/NormalActivityStarter.kt create mode 100644 app/src/main/java/com/opensooq/supernova/gligarexample/SupportedFilesLauncher.kt create mode 100644 app/src/main/res/layout/activity_main_launcher.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ab17077..bd2b7c1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,13 +10,16 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> - + + + + \ No newline at end of file diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/ActivityResultLauncher.kt similarity index 88% rename from app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt rename to app/src/main/java/com/opensooq/supernova/gligarexample/ActivityResultLauncher.kt index 803e575..3044ff9 100644 --- a/app/src/main/java/com/opensooq/supernova/gligarexample/MainActivity.kt +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/ActivityResultLauncher.kt @@ -1,6 +1,5 @@ package com.opensooq.supernova.gligarexample -import android.app.Activity import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity @@ -8,7 +7,7 @@ import com.opensooq.supernova.gligar.GligarPicker import com.opensooq.supernova.gligar.ui.GligarScreenResult import kotlinx.android.synthetic.main.activity_main.* -class MainActivity : AppCompatActivity() { +class ActivityResultLauncher : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -20,7 +19,7 @@ class MainActivity : AppCompatActivity() { return GligarPicker() .singleSelection(true) .disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) - .withActivity(this@MainActivity).show() + .withActivity(this@ActivityResultLauncher).show() } }) { if (!it.isNullOrEmpty()) { diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/MainScreenLauncher.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/MainScreenLauncher.kt new file mode 100644 index 0000000..bc5bc16 --- /dev/null +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/MainScreenLauncher.kt @@ -0,0 +1,26 @@ +package com.opensooq.supernova.gligarexample + +import android.content.Intent +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import kotlinx.android.synthetic.main.activity_main_launcher.* + +class MainScreenLauncher : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main_launcher) + + normal_launcher_button?.setOnClickListener { + startActivity(Intent(this@MainScreenLauncher, NormalActivityStarter::class.java)) + } + + start_launcher_button?.setOnClickListener { + startActivity(Intent(this@MainScreenLauncher, ActivityResultLauncher::class.java)) + } + + supported_files_button?.setOnClickListener { + startActivity(Intent(this@MainScreenLauncher, SupportedFilesLauncher::class.java)) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/NormalActivityStarter.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/NormalActivityStarter.kt new file mode 100644 index 0000000..ee0bfbe --- /dev/null +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/NormalActivityStarter.kt @@ -0,0 +1,38 @@ +package com.opensooq.supernova.gligarexample + +import android.app.Activity +import android.content.Intent +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.opensooq.supernova.gligar.GligarPicker +import kotlinx.android.synthetic.main.activity_main.* + +class NormalActivityStarter : AppCompatActivity() { + + val PICKER_REQUEST_CODE = 30 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + GligarPicker().limit(10).disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) + .withActivity(this).show() + } + + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + if (resultCode != Activity.RESULT_OK) { + return + } + + when (requestCode) { + PICKER_REQUEST_CODE -> { + val imagesList = data?.extras?.getStringArray(GligarPicker.IMAGES_RESULT) + if (!imagesList.isNullOrEmpty()) { + imagesCount.text = "Number of selected Images: ${imagesList.size}" + } + } + } + } + +} diff --git a/app/src/main/java/com/opensooq/supernova/gligarexample/SupportedFilesLauncher.kt b/app/src/main/java/com/opensooq/supernova/gligarexample/SupportedFilesLauncher.kt new file mode 100644 index 0000000..f298314 --- /dev/null +++ b/app/src/main/java/com/opensooq/supernova/gligarexample/SupportedFilesLauncher.kt @@ -0,0 +1,32 @@ +package com.opensooq.supernova.gligarexample + +import android.content.Intent +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.opensooq.supernova.gligar.GligarPicker +import com.opensooq.supernova.gligar.ui.GligarScreenResult +import kotlinx.android.synthetic.main.activity_main.* + +class SupportedFilesLauncher : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + val startForResult = registerForActivityResult(object : GligarScreenResult() { + override fun getGligarIntentLauncher(): Intent { + return GligarPicker() + .supportExtensions(arrayListOf("png")) + .singleSelection(true) + .disableCamera(false).cameraDirect(false).requestCode(PICKER_REQUEST_CODE) + .withActivity(this@SupportedFilesLauncher).show() + } + }) { + if (!it.isNullOrEmpty()) { + imagesCount.text = "Number of selected Images: ${it.size}" + } + } + + startForResult.launch(0) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 68363d7..da97492 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".ActivityResultLauncher"> + + + + +