@@ -27,10 +27,18 @@ class PhotoGroupView : RecyclerView {
2727 private var spacing: Int = 0
2828
2929 constructor (context: Context ) : this (context, null )
30- constructor (context: Context , attrs: AttributeSet ? ) : this (context, attrs, androidx.recyclerview.R .attr.recyclerViewStyle)
30+ constructor (context: Context , attrs: AttributeSet ? ) : this (
31+ context,
32+ attrs,
33+ androidx.recyclerview.R .attr.recyclerViewStyle
34+ )
3135
3236 @SuppressLint(" Recycle" )
33- constructor (context: Context , attrs: AttributeSet ? , defStyle: Int ) : super (context, attrs, defStyle) {
37+ constructor (context: Context , attrs: AttributeSet ? , defStyle: Int ) : super (
38+ context,
39+ attrs,
40+ defStyle
41+ ) {
3442 isVerticalScrollBarEnabled = false
3543 isHorizontalScrollBarEnabled = false
3644 overScrollMode = OVER_SCROLL_NEVER
@@ -41,19 +49,22 @@ class PhotoGroupView : RecyclerView {
4149 }
4250
4351 private val choiceList: List <String > = listOf (
44- context.getString(R .string.pick_chose_camera),
45- context.getString(R .string.pick_chose_album)
52+ context.getString(R .string.pick_chose_camera),
53+ context.getString(R .string.pick_chose_album)
4654 )
4755
48- fun setup (special : Special ,
49- exists : List <String >? = null,
50- readOnly : Boolean ,
51- appendText : String = "",
52- changed : (PhotoGroupAdapter , Int ) -> Unit = { _, _ -> }) {
56+ fun setup (
57+ special : Special ,
58+ exists : List <String >? = null,
59+ readOnly : Boolean ,
60+ appendText : String = "",
61+ changed : (PhotoGroupAdapter , Int ) -> Unit = { _, _ -> }
62+ ) {
5363 post {
5464 val vWidth = measuredWidth - paddingStart - paddingEnd
5565 val minWidth = context.resources.getDimensionPixelOffset(R .dimen.pick_photo_min_size)
56- val lp: ViewGroup .LayoutParams = ViewGroup .LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT )
66+ val lp: ViewGroup .LayoutParams =
67+ ViewGroup .LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT )
5768 do {
5869 lp.width = (vWidth - (special.column - 1 ) * spacing) / special.column
5970 if (lp.width >= minWidth || special.column <= 1 ) break
@@ -78,13 +89,23 @@ class PhotoGroupView : RecyclerView {
7889 if (el.path.isEmpty()) {
7990 if (pAdapter.isReadOnly()) return @setTapBlock
8091 // 选择图片
81- val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else emptyList()
92+ val selects: List <String > =
93+ if (pAdapter.isAppend) pAdapter.selectPaths() else emptyList()
8294 PickChoseActivity .show(special.activity, choiceList) { key ->
83- showPickPhoto(special.activity, key, selects, special.limit, pAdapter, index, special.crop)
95+ showPickPhoto(
96+ special.activity,
97+ key,
98+ selects,
99+ special.limit,
100+ pAdapter,
101+ index,
102+ special.crop
103+ )
84104 }
85105 } else {
86106 // 预览
87- val selects: List <String > = if (pAdapter.isAppend) pAdapter.selectPaths() else arrayListOf (el.path)
107+ val selects: List <String > =
108+ if (pAdapter.isAppend) pAdapter.selectPaths() else arrayListOf (el.path)
88109 showPreview(special.activity, selects, if (pAdapter.isAppend) index else 0 )
89110 }
90111 }
@@ -99,20 +120,22 @@ class PhotoGroupView : RecyclerView {
99120 // 跳转至预览
100121 private fun showPreview (activity : Activity , selects : List <String >, index : Int ) {
101122 PickControl .obtain(true )
102- .action(PickControl .ACTION_PREVIEW )
103- .selects(selects)
104- .index(index)
105- .done(activity)
123+ .action(PickControl .ACTION_PREVIEW )
124+ .selects(selects)
125+ .index(index)
126+ .done(activity)
106127 }
107128
108129 // 跳转至选择图片
109- private fun showPickPhoto (activity : Activity ,
110- key : String ,
111- selects : List <String >,
112- limit : Int ,
113- adapter : PhotoGroupAdapter ,
114- index : Int ,
115- crop : CropParams ? ) {
130+ private fun showPickPhoto (
131+ activity : Activity ,
132+ key : String ,
133+ selects : List <String >,
134+ limit : Int ,
135+ adapter : PhotoGroupAdapter ,
136+ index : Int ,
137+ crop : CropParams ?
138+ ) {
116139 val action: Int = when (key) {
117140 choiceList[0 ] -> {
118141 PickControl .ACTION_CAMERA
@@ -123,46 +146,50 @@ class PhotoGroupView : RecyclerView {
123146 else -> - 1
124147 }
125148 if (action < 0 ) return
126- val block: (Int , List <Uri >) -> Unit = { code, list ->
127- if (code == PickControl .ACTION_CAMERA ) {
128- if (adapter.isAppend) {
129- val allOf: MutableList <String > = ArrayList (adapter.selectCount() + 1 )
130- allOf.addAll(adapter.selectPaths())
131- allOf.addAll(list.map(Uri ::toString))
132- adapter.update(allOf, limit, index)
133- } else {
134- adapter.update(list.map(Uri ::toString), limit, index)
135- }
136- } else {
137- if (adapter.isAppend) {
138- val remotes: List <String > = selects.filter { null == PickUtils .path2Uri(it) }
139- val allOf: MutableList <String > = ArrayList (limit)
140- allOf.addAll(remotes)
141- allOf.addAll(list.map(Uri ::toString))
142- adapter.update(allOf, limit, index)
149+ val block: PickControl .PickCallback = object : PickControl .PickCallback () {
150+ override fun onSuccess (action : Int , uris : List <Uri >) {
151+ if (action == PickControl .ACTION_CAMERA ) {
152+ if (adapter.isAppend) {
153+ val allOf: MutableList <String > = ArrayList (adapter.selectCount() + 1 )
154+ allOf.addAll(adapter.selectPaths())
155+ allOf.addAll(uris.map(Uri ::toString))
156+ adapter.update(allOf, limit, index)
157+ } else {
158+ adapter.update(uris.map(Uri ::toString), limit, index)
159+ }
143160 } else {
144- adapter.update(list.map(Uri ::toString), limit, index)
161+ if (adapter.isAppend) {
162+ val remotes: List <String > =
163+ selects.filter { null == PickUtils .path2Uri(it) }
164+ val allOf: MutableList <String > = ArrayList (limit)
165+ allOf.addAll(remotes)
166+ allOf.addAll(uris.map(Uri ::toString))
167+ adapter.update(allOf, limit, index)
168+ } else {
169+ adapter.update(uris.map(Uri ::toString), limit, index)
170+ }
145171 }
146172 }
147173 }
148174 PickControl .obtain(clean = true ).action(action)
149- .selects(selects)
150- .limit(limit)
151- .crop(crop)
152- .callback(block)
153- .done(activity)
175+ .selects(selects)
176+ .limit(limit)
177+ .crop(crop)
178+ .callback(block)
179+ .done(activity)
154180 }
155181
156182 // 指定参数
157183 class Special (
158- var activity : Activity ,
159- var limit : Int = 1 ,
160- var column : Int = 4 ,
161- var fixed : MutableList <PhotoItem >? = null ,
162- var crop : CropParams ? = null
184+ var activity : Activity ,
185+ var limit : Int = 1 ,
186+ var column : Int = 4 ,
187+ var fixed : MutableList <PhotoItem >? = null ,
188+ var crop : CropParams ? = null
163189 )
164190
165- internal class GridSpacingItemDecoration (private val spanCount : Int , private val spacing : Int ) : ItemDecoration() {
191+ internal class GridSpacingItemDecoration (private val spanCount : Int , private val spacing : Int ) :
192+ ItemDecoration () {
166193
167194 override fun getItemOffsets (outRect : Rect , view : View , parent : RecyclerView , state : State ) {
168195 val position = parent.getChildAdapterPosition(view)
0 commit comments