Skip to content

Commit

Permalink
Merge branch 'release/1.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mshabiola committed Feb 20, 2023
2 parents c54058f + 9b1333f commit c0b810c
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
defaultConfig.targetSdk = 33
// compileSdkPreview = "UpsideDownCake"
defaultConfig.minSdk = 24
defaultConfig.versionName = "1.1.2"
defaultConfig.versionCode = 10
defaultConfig.versionName = "1.1.3"
defaultConfig.versionCode = 11

defaultConfig.testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
defaultConfig.vectorDrawables {
Expand Down
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ versionCatalogUpdate {
// keep all plugins that aren't used in the project
keepUnusedPlugins.set(true)
}
}
}

//./gradlew assembleDebug -PenableComposeCompilerReports=true
// ./gradlew assembleDebug -PenableComposeCompilerMetrics=true
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface PathDao {
@Query("SELECT * FROM path_table WHERE imageId=:imageID")
@Query("SELECT * FROM path_table WHERE imageId=:imageID ORDER BY pathId")
fun getPaths(imageID: Long): Flow<List<DrawPathEntity>>

@Query("DELETE FROM PATH_TABLE WHERE imageId=:imageID")
Expand Down
4 changes: 3 additions & 1 deletion distribution/whatsnew/whatsnew-en-US
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
* fix bugs
- Add new UI
- Fix Alarm bug
- Increase Performance
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun Board(
drawingController.setPathData(offset.x, offset.y, mode)
}

val p2 = remember(drawingController.listOfPathData.value) {
val p2 = remember(drawingController.unCompletePathData.value) {
drawingController.getPathAndData()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fun DrawingBar(
mutableStateOf(4)
}

LaunchedEffect(key1 = controller.listOfPathData.value, block = {
LaunchedEffect(key1 = controller.unCompletePathData.value, block = {
if (isUp) {
isUp = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.drawscope.DrawScope
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.toImmutableMap
typealias ImmutablePath = ImmutableMap<PathData, List<Offset>>

@SuppressLint("MutableCollectionMutableState")
class DrawingController {
Expand All @@ -38,6 +40,7 @@ class DrawingController {
Color(0xFFFF3D00),

)

val lineCaps = arrayOf(StrokeCap.Round, StrokeCap.Butt, StrokeCap.Round)
val lineJoins = arrayOf(StrokeJoin.Round, StrokeJoin.Bevel, StrokeJoin.Miter)

Expand All @@ -48,8 +51,11 @@ class DrawingController {
var draw_mode = DRAW_MODE.PEN
var colorAlpha = 1f

private var _listOfPathData = mutableStateOf(ListOfPathData())
val listOfPathData: State<ListOfPathData> = _listOfPathData
private var _unCompletePathData = mutableStateOf(emptyMap<PathData, List<Offset>>().toImmutableMap())
val unCompletePathData: State<ImmutablePath> = _unCompletePathData

private var _completePathData = mutableStateOf(emptyMap<PathData, List<Offset>>().toImmutableMap())
val completePathData: State<ImmutablePath> = _completePathData

private val redoPaths = HashMap<PathData, List<Offset>>()
private val _canUndo = mutableStateOf(false)
Expand All @@ -66,27 +72,61 @@ class DrawingController {
fun setPathData(x: Float, y: Float, mode: MODE) {
when (draw_mode) {
DRAW_MODE.ERASE -> {
if (mode == MODE.DOWN) {
xx = x
yy = y
}
if (mode == MODE.MOVE) {
val rect = RectF(minOf(xx, x), minOf(y, yy), maxOf(xx, x), maxOf(y, yy))
val paths = _listOfPathData.value.paths2.toMutableMap()
val path =
paths.filter { entry -> entry.value.any { rect.contains(it.x, it.y) } }
path.forEach { p ->
paths.remove(p.key)
redoPaths[p.key] = p.value
when(mode){
MODE.DOWN->{
xx = x
yy = y
}
MODE.MOVE->{
val rect = RectF(minOf(xx, x), minOf(y, yy), maxOf(xx, x), maxOf(y, yy))
val paths = _unCompletePathData.value.toMutableMap()
val path =
paths.filter { entry -> entry.value.any { rect.contains(it.x, it.y) } }
if (path.isNotEmpty()){
path.forEach { p ->
paths.remove(p.key)
redoPaths[p.key] = p.value
}
//rearrange id
val newPaths= HashMap<PathData,List<Offset>>()
paths
.toList()
.forEachIndexed { index, pair ->
val newdata=pair.first.copy(id=index)
newPaths[newdata] = pair.second
}


_unCompletePathData.value=newPaths.toImmutableMap()
}

}
MODE.UP->{
setCompleteList()
}
setListData(_listOfPathData.value.copy(paths2 = paths.toImmutableMap()))
}
// if (mode == MODE.DOWN) {
// xx = x
// yy = y
// }
// if (mode == MODE.MOVE) {
// val rect = RectF(minOf(xx, x), minOf(y, yy), maxOf(xx, x), maxOf(y, yy))
// val paths = _listOfPathData.value.toMutableMap()
// val path =
// paths.filter { entry -> entry.value.any { rect.contains(it.x, it.y) } }
// path.forEach { p ->
// paths.remove(p.key)
// redoPaths[p.key] = p.value
// }
// _listOfPathData.value=paths.toImmutableMap()
// }
//finish move, delete data
}

else -> {
when (mode) {
MODE.DOWN -> {
val id = _listOfPathData.value.paths2.keys.size
val id = _unCompletePathData.value.keys.size
pathData = PathData(
id = id,
color = color,
Expand All @@ -96,63 +136,69 @@ class DrawingController {
colorAlpha = colorAlpha,
)
// id++
val paths2 = _listOfPathData.value.paths2.toMutableMap()
val paths2 = _unCompletePathData.value.toMutableMap()
val list = emptyList<Offset>().toMutableList()

list.add(Offset(x, y))
paths2[pathData] = list
setListData(_listOfPathData.value.copy(paths2 = paths2.toImmutableMap()))
_unCompletePathData.value= paths2.toImmutableMap()
}

MODE.MOVE -> {
val paths2 = _listOfPathData.value.paths2.toMutableMap()
val paths2 = _unCompletePathData.value.toMutableMap()
val list = paths2[pathData]!!.toMutableList()

list.add(Offset(x, y))
paths2[pathData] = list
setListData(_listOfPathData.value.copy(paths2 = paths2.toImmutableMap()))
_unCompletePathData.value = paths2.toImmutableMap()
}

MODE.UP -> {
//save data
setCompleteList()
}
}
}
}
setDoUnDo()
}

fun setListData(listOfPathDa: ListOfPathData) {
_listOfPathData.value = listOfPathDa
}
// fun setListData(listOfPathDa: ListOfPathData) {
// _listOfPathData.value = listOfPathDa
// }

fun setPathData(pathDatas: Map<PathData, List<Offset>>) {
val paths = _listOfPathData.value.paths2.toMutableMap()
val paths = _unCompletePathData.value.toMutableMap()
paths.putAll(pathDatas)
// id = pathDatas.size
setListData(_listOfPathData.value.copy(paths2 = paths.toImmutableMap()))
_unCompletePathData.value= paths.toImmutableMap()
_completePathData.value=paths.toImmutableMap()
}

fun undo() {
if (canUndo.value) {
val paths = _listOfPathData.value.paths2.toMutableMap()
val paths = _unCompletePathData.value.toMutableMap()
val lastKey = paths.keys.last()
redoPaths[lastKey] = paths.remove(lastKey)!!
setListData(_listOfPathData.value.copy(paths2 = paths.toImmutableMap()))
_unCompletePathData.value= paths.toImmutableMap()
setDoUnDo()
}
}

private fun setDoUnDo() {
_canUndo.value = _listOfPathData.value.paths2.isNotEmpty()
_canUndo.value = _unCompletePathData.value.isNotEmpty()
_canRedo.value = redoPaths.isNotEmpty()

//save data
setCompleteList()
}

fun redo() {
if (canRedo.value) {
val paths = _listOfPathData.value.paths2.toMutableMap()
val paths = _unCompletePathData.value.toMutableMap()
val lastKey = redoPaths.keys.last()
paths[lastKey] = redoPaths.remove(lastKey)!!
setListData(_listOfPathData.value.copy(paths2 = paths.toImmutableMap()))
_unCompletePathData.value= paths.toImmutableMap()

setDoUnDo()
// listOfPathData.value.add(redoPaths.removeLast())
Expand All @@ -169,9 +215,8 @@ class DrawingController {
fun getPathAndData(): List<Pair<Path, PathData>> {
var prevOff = Offset.Zero

val p = _listOfPathData
val p = _unCompletePathData
.value
.paths2
.map {
val yPath = Path()
it.value.forEachIndexed { index, offset ->
Expand All @@ -194,11 +239,17 @@ class DrawingController {
}

fun clearPath() {
val paths = _listOfPathData.value.paths2.toMutableMap()
val paths = _unCompletePathData.value.toMutableMap()
paths.clear()
redoPaths.clear()
setListData(_listOfPathData.value.copy(paths2 = paths.toImmutableMap()))
_unCompletePathData.value = paths.toImmutableMap()
setDoUnDo()
//save data
}

private fun setCompleteList(){
_completePathData.value=unCompletePathData.value

}

fun getBitMap(width: Int, heigth: Int, density: Float): Bitmap {
Expand Down Expand Up @@ -231,7 +282,6 @@ class DrawingController {
@Composable
fun rememberDrawingController(): DrawingController {
return remember {
DrawingController().apply {
}
DrawingController()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.mshdabiola.firebase.FirebaseScreenLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File

@Composable
Expand All @@ -46,30 +48,23 @@ fun DrawingScreen(
onBack: () -> Unit,
) {
FirebaseScreenLog(screen = "drawing_screen")
val lifecycleOwner = LocalLifecycleOwner.current.lifecycle
val context = LocalContext.current
val lifecycleObserver = object : DefaultLifecycleObserver {
override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
viewModel.onPause(context)
}
}

DisposableEffect(key1 = Unit, effect = {
lifecycleOwner.addObserver(lifecycleObserver)
onDispose {
lifecycleOwner.removeObserver(lifecycleObserver)
LaunchedEffect(key1=viewModel.controller.completePathData.value, block = {
withContext(Dispatchers.IO){
viewModel.saveDrawing(viewModel.controller.completePathData.value)
}
})
val res = context.resources.displayMetrics
LaunchedEffect(key1 = viewModel.controller.listOfPathData.value.paths2, block = {
viewModel.saveImage(
viewModel.controller.getBitMap(
res.widthPixels,
res.heightPixels,
res.density,
),
)
LaunchedEffect(key1 = viewModel.controller.completePathData.value, block = {
withContext(Dispatchers.IO){
viewModel.saveImage(
viewModel.controller.getBitMap(
res.widthPixels,
res.heightPixels,
res.density,
),
)
}
})
DrawingScreen(
onBackk = onBack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class DrawingViewModel @Inject constructor(
),
)
val controller = DrawingController()
val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
val coroutineScope = CoroutineScope(dispatcher + SupervisorJob())

init {
viewModelScope.launch {
Expand All @@ -65,13 +63,6 @@ class DrawingViewModel @Inject constructor(
contentManager.saveBitmap(path, bitmap)
}

fun onPause(context: Context) {
coroutineScope.launch {
val res = context.resources.displayMetrics
val map = controller.listOfPathData.value.paths2
saveDrawing(map)
}
}

fun deleteImage() {
viewModelScope.launch(Dispatchers.IO) {
Expand All @@ -82,7 +73,7 @@ class DrawingViewModel @Inject constructor(
}

// private var job: Job? = null
private suspend fun saveDrawing(map: Map<PathData, List<Offset>>) {
suspend fun saveDrawing(map: Map<PathData, List<Offset>>) {
val data = changeToDrawPath(map)
if (map.isEmpty()) {
drawingPathRepository.delete(imageID)
Expand Down
Loading

0 comments on commit c0b810c

Please sign in to comment.