11package com.smarttoolfactory.cropper.settings
22
3+ import androidx.compose.runtime.mutableStateListOf
34import androidx.compose.ui.graphics.ImageBitmap
45import com.smarttoolfactory.cropper.model.*
6+ import com.smarttoolfactory.cropper.util.createPolygonShape
57
68class CropFrameFactory (private val defaultImage : ImageBitmap ) {
79
8- private val cropFrameMap = linkedMapOf<OutlineType , CropFrame >()
9- private var items = listOf<CropFrame >()
10+ private val cropFrames = mutableStateListOf<CropFrame >()
1011
1112 fun getCropFrames (): List <CropFrame > {
12- if (items .isEmpty()) {
13+ if (cropFrames .isEmpty()) {
1314 val temp = mutableListOf<CropFrame >()
1415 OutlineType .values().forEach {
1516 temp.add(getCropFrame(it))
1617 }
17- items = temp
18+ cropFrames.addAll( temp)
1819 }
19- return items.toList()
20+ return cropFrames
2021 }
2122
2223 fun getCropFrame (outlineType : OutlineType ): CropFrame {
23- return cropFrameMap[outlineType] ? : createDefaultFrame(outlineType)
24+ return cropFrames
25+ .firstOrNull { it.outlineType == outlineType } ? : createDefaultFrame(outlineType)
2426 }
2527
2628 private fun createDefaultFrame (outlineType : OutlineType ): CropFrame {
@@ -114,15 +116,38 @@ class CropFrameFactory(private val defaultImage: ImageBitmap) {
114116
115117 OutlineType .Polygon -> {
116118 PolygonOutlineContainer (
117- outlines = listOf (PolygonCropShape (id = 0 , title = " Polygon" ))
119+ outlines = listOf (
120+ PolygonCropShape (
121+ id = 0 ,
122+ title = " Polygon"
123+ ),
124+ PolygonCropShape (
125+ id = 1 ,
126+ title = " Pentagon" ,
127+ polygonProperties = PolygonProperties (sides = 5 , 0f ),
128+ shape = createPolygonShape(5 , 0f )
129+ ),
130+ PolygonCropShape (
131+ id = 2 ,
132+ title = " Heptagon" ,
133+ polygonProperties = PolygonProperties (sides = 7 , 0f ),
134+ shape = createPolygonShape(7 , 0f )
135+ ),
136+ PolygonCropShape (
137+ id = 3 ,
138+ title = " Octagon" ,
139+ polygonProperties = PolygonProperties (sides = 8 , 0f ),
140+ shape = createPolygonShape(8 , 0f )
141+ )
142+ )
118143 )
119144 }
120145
121146 OutlineType .Custom -> {
122147 CustomOutlineContainer (
123148 outlines = listOf (
124149 CustomPathOutline (id = 0 , title = " Custom" , path = Paths .Favorite ),
125- CustomPathOutline (id = 0 , title = " Star" , path = Paths .Star ),
150+ CustomPathOutline (id = 1 , title = " Star" , path = Paths .Star )
126151 )
127152 )
128153 }
@@ -138,6 +163,7 @@ class CropFrameFactory(private val defaultImage: ImageBitmap) {
138163 }
139164
140165 fun editCropFrame (cropFrame : CropFrame ) {
141- cropFrameMap[cropFrame.outlineType] = cropFrame
166+ val indexOf = cropFrames.indexOfFirst { it.outlineType == cropFrame.outlineType }
167+ cropFrames[indexOf] = cropFrame
142168 }
143169}
0 commit comments