Skip to content

Commit

Permalink
- 记录最后选择的相册
Browse files Browse the repository at this point in the history
  • Loading branch information
limuyang committed May 2, 2020
1 parent 5dcde47 commit de399ea
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 148 deletions.
19 changes: 2 additions & 17 deletions app/src/main/java/top/limuyang2/pohotopicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,13 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {
//验证权限
if (EasyPermissions.hasPermissions(this, WRITE_EXTERNAL_STORAGE)) {

val intent = LPhotoPickerActivity.IntentBuilder(this)
.maxChooseCount(multiMumTv.text.toString().toInt())
.columnsNumber(columnsNumberMumTv.text.toString().toInt())
.imageType(LPPImageType.ofAll())
.pauseOnScroll(pauseOnScroll_cb.isChecked)
.isSingleChoose(singleChoose_cb.isChecked)
.imageEngine(LGlideEngine())
.theme(theme)
// .selectedPhotos(ArrayList<String>().apply {
// add("/storage/emulated/0/DCIM/Screenshots/Screenshot_2018-07-09-11-43-08-936_com.wxm.android.png")
// add("/storage/emulated/0/pictures/li_mumuの二维码.jpg")
// })
.build()

// startActivityForResult(intent, CHOOSE_PHOTO_REQUEST)

LPhotoHelper.Builder()
.maxChooseCount(multiMumTv.text.toString().toInt())
.columnsNumber(columnsNumberMumTv.text.toString().toInt())
.imageType(LPPImageType.ofAll())
.pauseOnScroll(pauseOnScroll_cb.isChecked)
.isSingleChoose(singleChoose_cb.isChecked)
.isOpenLastAlbum(true)
.theme(theme)
.build()
.start(this, CHOOSE_PHOTO_REQUEST)
Expand All @@ -127,7 +112,7 @@ class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {
if (resultCode == RESULT_OK) {
when (requestCode) {
CHOOSE_PHOTO_REQUEST -> {
val selectedPhotos = LPhotoPickerActivity.getSelectedPhotos(data)
val selectedPhotos = LPhotoHelper.getSelectedPhotos(data)

if (singleChoose_cb.isChecked) { //单选模式
if (use_uCrop_cb.isChecked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ import top.limuyang2.photolibrary.util.ImageEngineUtils
*/
class LPhotoHelper internal constructor(private val intent: Intent) {
companion object {
const val EXTRA_LAST_OPENED_ALBUM = "EXTRA_LAST_OPENED_ALBUM"
const val EXTRA_SELECTED_PHOTOS = "EXTRA_SELECTED_PHOTOS"
const val EXTRA_MAX_CHOOSE_COUNT = "EXTRA_MAX_CHOOSE_COUNT"
const val EXTRA_PAUSE_ON_SCROLL = "EXTRA_PAUSE_ON_SCROLL"
const val EXTRA_COLUMNS_NUMBER = "EXTRA_COLUMNS_NUMBER"
const val EXTRA_IS_SINGLE_CHOOSE = "EXTRA_IS_SINGLE_CHOOSE"
const val EXTRA_TYPE = "EXTRA_TYPE"
const val EXTRA_THEME = "EXTRA_THEME"

/**
* 获取已选择的图片集合
*
* @param intent
* @return
*/
@JvmStatic
fun getSelectedPhotos(intent: Intent?): ArrayList<String> {
return intent?.getStringArrayListExtra(EXTRA_SELECTED_PHOTOS) ?: ArrayList()
}
}


Expand All @@ -39,13 +51,13 @@ class LPhotoHelper internal constructor(private val intent: Intent) {
class Builder {
private val mIntent: Intent = Intent()

// /**
// * 拍照后图片保存的目录。如果传 null 表示没有拍照功能,如果不为 null 则具有拍照功能,
// */
// fun cameraFileDir(cameraFileDir: File?): IntentBuilder {
// mIntent.putExtra(EXTRA_CAMERA_FILE_DIR, cameraFileDir)
// return this
// }

fun isOpenLastAlbum(isOpen: Boolean): Builder {
mIntent.putExtra(EXTRA_LAST_OPENED_ALBUM, isOpen)
return this
}



/**
* 需要显示哪种类型的图片(JPG\PNG\GIF\WEBP),默认全部加载
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package top.limuyang2.photolibrary.activity

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.TypedValue
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import top.limuyang2.photolibrary.LPhotoHelper.Companion.EXTRA_LAST_OPENED_ALBUM
import top.limuyang2.photolibrary.LPhotoHelper.Companion.EXTRA_TYPE
import top.limuyang2.photolibrary.R
import top.limuyang2.photolibrary.adapter.LFolderAdapter
import top.limuyang2.photolibrary.databinding.LPpActivityFolderBinding
import top.limuyang2.photolibrary.model.LFolderModel
import top.limuyang2.photolibrary.util.dip
import top.limuyang2.photolibrary.util.findFolder
import top.limuyang2.photolibrary.util.setStatusBarColor
Expand All @@ -25,24 +31,30 @@ class LPhotoFolderActivity : LBaseActivity<LPpActivityFolderBinding>() {

private val mFolderAdapter: LFolderAdapter by lazy(LazyThreadSafetyMode.NONE) { LFolderAdapter() }

private val showTypeArray by lazy { intent.getStringArrayExtra(EXTRA_TYPE) }
private val showTypeArray by lazy(LazyThreadSafetyMode.NONE) { intent.getStringArrayExtra(EXTRA_TYPE) }

private val isOpenLastAlbum by lazy(LazyThreadSafetyMode.NONE) { intent.getBooleanExtra(EXTRA_LAST_OPENED_ALBUM, false) }

override fun initBinding(): LPpActivityFolderBinding {
return LPpActivityFolderBinding.inflate(layoutInflater)
}

override fun initView(savedInstanceState: Bundle?) {
// 判断是否需要打开最后记录的相册
if (isOpenLastAlbum) {
val lastModel = lastOpenAlbum
if (lastModel.bucketId != 0L) {
openAlbum(lastModel)
}
}

initAttr()
viewBinding.apply {
recyclerView.adapter = mFolderAdapter

mFolderAdapter.setOnItemClick { _, _, model ->
println(model)
intent.component = ComponentName(this@LPhotoFolderActivity, LPhotoPickerActivity::class.java)
intent.putExtra("bucketId", model.bucketId)
intent.putExtra("bucketName", model.bucketName)
startActivity(intent)
openAlbum(model)
lastOpenAlbum = model
}
}
}
Expand All @@ -58,6 +70,10 @@ class LPhotoFolderActivity : LBaseActivity<LPpActivityFolderBinding>() {


viewBinding.apply {
// 背景色
val activityBg = typedArray.getColor(R.styleable.LPPAttr_l_pp_picker_activity_bg, Color.parseColor("#F9F9F9"))
window.setBackgroundDrawable(ColorDrawable(activityBg))

val toolBarHeight = typedArray.getDimensionPixelSize(R.styleable.LPPAttr_l_pp_toolBar_height, dip(56).toInt())
val l = toolBar.layoutParams
l.height = toolBarHeight
Expand Down Expand Up @@ -97,5 +113,47 @@ class LPhotoFolderActivity : LBaseActivity<LPpActivityFolderBinding>() {
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
PICK_CODE -> {
if (resultCode == Activity.RESULT_OK) {
// 透传数据
setResult(Activity.RESULT_OK, data)
finish()
} else {
// 如果取消了选择,则清除最后打开的记录
lastOpenAlbum = LFolderModel()
}
}
}
}

private fun openAlbum(model: LFolderModel) {
intent.component = ComponentName(this@LPhotoFolderActivity, LPhotoPickerActivity::class.java)
intent.putExtra("bucketId", model.bucketId)
intent.putExtra("bucketName", model.bucketName)
startActivityForResult(intent, PICK_CODE)
}


private var lastOpenAlbum: LFolderModel
set(value) {
val sp = getSharedPreferences("l_pp_sp", Context.MODE_PRIVATE)
sp.edit().putLong(SP_LAST_BUCKET_ID, value.bucketId)
.putString(SP_LAST_BUCKET_NAME, value.bucketName)
.apply()
}
get() {
val sp = getSharedPreferences("l_pp_sp", Context.MODE_PRIVATE)
val id = sp.getLong(SP_LAST_BUCKET_ID, 0)
val name = sp.getString(SP_LAST_BUCKET_NAME, "") ?: ""
return LFolderModel(name, id, "", -1)
}

companion object {
private const val PICK_CODE = 8
private const val SP_LAST_BUCKET_ID = "bucketId"
private const val SP_LAST_BUCKET_NAME = "bucketName"
}
}
Loading

0 comments on commit de399ea

Please sign in to comment.