Skip to content

Commit

Permalink
Merge pull request #3 from geckour/develop
Browse files Browse the repository at this point in the history
v1.0.1
  • Loading branch information
geckour authored Feb 26, 2019
2 parents 5de454b + f216ff3 commit 3e310eb
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 75 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId = "com.geckour.lopicmaker"
minSdkVersion(Deps.GradlePlugin.minSdkVersion)
targetSdkVersion(Deps.GradlePlugin.targetSdkVersion)
versionCode = 1
versionName = "1.0.0"
versionCode = 2
versionName = "1.0.1"
testInstrumentationRunner = Deps.Test.instrumentTestRunner

dataBinding.isEnabled = true
Expand Down
1 change: 0 additions & 1 deletion app/release/output.json

This file was deleted.

30 changes: 17 additions & 13 deletions app/src/main/java/com/geckour/lopicmaker/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ class MainActivity :
setImageResource(
when (mode) {
MainViewModel.FabLeftMode.UNDO -> R.drawable.ic_undo
MainViewModel.FabLeftMode.REDO -> R.drawable.ic_redo
MainViewModel.FabLeftMode.FILL -> R.drawable.ic_fill
MainViewModel.FabLeftMode.MARK_NOT_FILL -> R.drawable.ic_close
}
)
setOnClickListener {
when (mode) {
MainViewModel.FabLeftMode.UNDO -> {
viewModel.fabLeftClicked.call()
viewModel.fabLeftClicked.postValue(mode)
}
MainViewModel.FabLeftMode.REDO -> {
viewModel.fabLeftClicked.postValue(mode)
}
MainViewModel.FabLeftMode.FILL -> {
viewModel.fabLeftMode.postValue(MainViewModel.FabLeftMode.MARK_NOT_FILL)
Expand Down Expand Up @@ -231,10 +235,10 @@ class MainActivity :

val fragment = SolveFragment.newInstance(it)
if (fragment != null) {
supportFragmentManager?.beginTransaction()
?.replace(R.id.container, fragment)
?.addToBackStack(null)
?.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit()
}
}

Expand All @@ -243,10 +247,10 @@ class MainActivity :

val fragment = EditorFragment.newInstance(it, false)
if (fragment != null) {
supportFragmentManager?.beginTransaction()
?.replace(R.id.container, fragment)
?.addToBackStack(null)
?.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit()
}
}

Expand All @@ -255,10 +259,10 @@ class MainActivity :

val fragment = EditorFragment.newInstance(it, true)
if (fragment != null) {
supportFragmentManager?.beginTransaction()
?.replace(R.id.container, fragment)
?.addToBackStack(null)
?.commit()
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MainViewModel : ViewModel() {

enum class FabLeftMode {
UNDO,
REDO,
FILL,
MARK_NOT_FILL
}
Expand All @@ -26,7 +27,7 @@ class MainViewModel : ViewModel() {
internal val fabRightVisible = SingleLiveEvent<Boolean>()
internal val fabLeftMode = SingleLiveEvent<FabLeftMode>()
internal val fabLeftVisible = SingleLiveEvent<Boolean>()
internal val fabLeftClicked = SingleLiveEvent<Unit>()
internal val fabLeftClicked = SingleLiveEvent<FabLeftMode>()

internal val toolbarTitleResId = SingleLiveEvent<Pair<@androidx.annotation.StringRes Int, List<String>>>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class DraftProblemsFragment : Fragment() {

mainViewModel.fabRightMode.postValue(MainViewModel.FabRightMode.ADD)

binding.textIndicateEmpty.setText(R.string.problem_fragment_message_empty_draft)

binding.recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = viewModel.adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ class EditorFragment : Fragment(), MyAlertDialogFragment.DialogListener {

private fun observeEvents() {
mainViewModel.fabLeftClicked.observe(this) {
viewModel.onUndoClicked(binding)
when (it) {
MainViewModel.FabLeftMode.UNDO ->
viewModel.onUndoClicked(binding)
MainViewModel.FabLeftMode.REDO ->
viewModel.onRedoClicked(binding)
else -> Unit
}
}

viewModel.problem.observe(this) {
Expand Down Expand Up @@ -199,9 +205,17 @@ class EditorFragment : Fragment(), MyAlertDialogFragment.DialogListener {
mainViewModel.fabLeftVisible.postValue(it)
}

viewModel.fabLeftMode.observe(this) {
mainViewModel.fabLeftMode.postValue(it)
}

viewModel.snackbarStringResId.observe(this) {
mainViewModel.snackBarStringResId.postValue(it)
}

viewModel.toolbarTitleResId.observe(this) {
mainViewModel.toolbarTitleResId.postValue(it)
}
}

private fun onPositive(requestCode: MyAlertDialogFragment.RequestCode, result: Any?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.geckour.lopicmaker.data.DB
import com.geckour.lopicmaker.data.dao.upsert
import com.geckour.lopicmaker.data.model.Problem
import com.geckour.lopicmaker.databinding.FragmentEditorBinding
import com.geckour.lopicmaker.ui.main.MainViewModel
import com.geckour.lopicmaker.util.Algorithm
import com.geckour.lopicmaker.util.MyAlertDialogFragment
import com.geckour.lopicmaker.util.SingleLiveEvent
Expand All @@ -40,6 +41,8 @@ class EditorViewModel : ViewModel() {
private var prevAction: Int = MotionEvent.ACTION_CANCEL

internal val fabLeftVisible = SingleLiveEvent<Boolean>()
internal val fabLeftMode = SingleLiveEvent<MainViewModel.FabLeftMode>()
internal val toolbarTitleResId = SingleLiveEvent<Pair<@androidx.annotation.StringRes Int, List<String>>>()

internal fun initCanvas(
context: Context,
Expand All @@ -60,19 +63,14 @@ class EditorViewModel : ViewModel() {

private fun initCanvas(binding: FragmentEditorBinding, problem: Problem) {
val bitmap = problem.let {
nonNullAlgorithm.prevCells.apply {
clear()
addAll(it.cells)
}

nonNullAlgorithm.setCells(it.cells)
}
binding.canvas.setImageBitmap(bitmap)
binding.canvasImage = bitmap
}

private fun initCanvas(binding: FragmentEditorBinding, size: Point) {
algorithm = Algorithm(size)
binding.canvas.setImageBitmap(nonNullAlgorithm.createCanvasImage())
binding.canvasImage = nonNullAlgorithm.createCanvasImage()
}

internal fun saveProblem(context: Context, data: String, draft: Boolean) {
Expand Down Expand Up @@ -159,7 +157,7 @@ class EditorViewModel : ViewModel() {
keysInRow,
keysInColumn,
thumb,
cells = nonNullAlgorithm.cells
cells = nonNullAlgorithm.currentCells
)
}

Expand Down Expand Up @@ -214,10 +212,7 @@ class EditorViewModel : ViewModel() {
MotionEvent.ACTION_UP,
MotionEvent.ACTION_POINTER_UP,
MotionEvent.ACTION_CANCEL -> {
nonNullAlgorithm.prevCells.apply {
clear()
addAll(nonNullAlgorithm.cells.map { it.copy() })
}
nonNullAlgorithm.setCells()
}
}

Expand Down Expand Up @@ -253,11 +248,10 @@ class EditorViewModel : ViewModel() {

val bitmap =
nonNullAlgorithm.onEditCanvasImage(
(binding.canvas.drawable
as BitmapDrawable).bitmap, cell,
binding.canvasImage, cell,
true
)
binding.canvas.setImageBitmap(bitmap)
binding.canvasImage = bitmap
}
pointPrev0.set(pointCurrent)
}
Expand Down Expand Up @@ -354,14 +348,28 @@ class EditorViewModel : ViewModel() {
}

private fun showUndoButtonIfAvailable() {
if (algorithm?.prevCells?.isNotEmpty() == true)
if (algorithm?.undoAvailable == true) {
fabLeftMode.postValue(MainViewModel.FabLeftMode.UNDO)
fabLeftVisible.postValue(true)
}
}

private fun showRedoButtonIfAvailable() {
if (algorithm?.redoAvailable == true) {
fabLeftMode.postValue(MainViewModel.FabLeftMode.REDO)
fabLeftVisible.postValue(true)
}
}

internal fun onUndoClicked(binding: FragmentEditorBinding) {
val bitmap = nonNullAlgorithm.setCells(nonNullAlgorithm.prevCells)
nonNullAlgorithm.prevCells.clear()
binding.canvas.setImageBitmap(bitmap)
fabLeftVisible.postValue(false)
nonNullAlgorithm.undo()
binding.canvasImage = nonNullAlgorithm.getNewBitmap()
showRedoButtonIfAvailable()
}

internal fun onRedoClicked(binding: FragmentEditorBinding) {
nonNullAlgorithm.redo()
binding.canvasImage = nonNullAlgorithm.getNewBitmap()
showUndoButtonIfAvailable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ProblemsFragment : Fragment() {

mainViewModel.fabRightMode.postValue(MainViewModel.FabRightMode.ADD)

binding.textIndicateEmpty.setText(R.string.problem_fragment_message_empty)

binding.recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = viewModel.adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class SolveFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

viewModel.problem.value?.title?.let {
mainViewModel.toolbarTitleResId.postValue(R.string.action_bar_title_solve_with_title to listOf(it))
}

binding.canvas.setOnTouchListener { _, event ->
viewModel.onTouchCanvas(requireContext(), binding, event, mainViewModel.fabLeftMode.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class SolveViewModel : ViewModel() {

internal val pointPrev0 = PointF(-1f, -1f)
internal val pointPrev1 = PointF(-1f, -1f)
private var prevAction: Int = MotionEvent.ACTION_CANCEL

internal fun initCanvas(
context: Context,
Expand Down Expand Up @@ -101,7 +100,7 @@ class SolveViewModel : ViewModel() {
pointPrev0.set(-1f, -1f)
pointPrev1.set(-1f, -1f)

if (nonNullAlgorithm4Ref.checkCellsWithSolution(nonNullAlgorithm.cells)) {
if (nonNullAlgorithm4Ref.checkCellsWithSolution(nonNullAlgorithm.currentCells)) {
viewModelScope.launch {
problem.value?.apply {
if (this.thumb == null) {
Expand All @@ -119,17 +118,6 @@ class SolveViewModel : ViewModel() {
MotionEvent.ACTION_DOWN,
MotionEvent.ACTION_POINTER_DOWN,
MotionEvent.ACTION_MOVE -> {
when (prevAction) {
MotionEvent.ACTION_UP,
MotionEvent.ACTION_POINTER_UP,
MotionEvent.ACTION_CANCEL -> {
nonNullAlgorithm.prevCells.apply {
clear()
addAll(nonNullAlgorithm.cells.map { it.copy() })
}
}
}

val pointCurrent = PointF(event.x, event.y)
val coordCurrent = nonNullAlgorithm.getCoordinateFromTouchPoint(
binding.canvas,
Expand Down Expand Up @@ -175,7 +163,6 @@ class SolveViewModel : ViewModel() {
pointPrev0.set(pointCurrent)
}
}
prevAction = event.action

return true
}
Expand Down
Loading

0 comments on commit 3e310eb

Please sign in to comment.