Skip to content

Commit a571d42

Browse files
committed
Relief
Избавился от getNextBlock(), getPrevBlock(), getBlockStatus(), setBlockStatus(), getBlockType(), setBlockType()
1 parent 94f316f commit a571d42

20 files changed

Lines changed: 121 additions & 158 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,4 @@ lint/tmp/
101101
.cxx
102102
local.properties
103103
.idea/misc.xml
104+

app/src/main/java/com/apochromat/codeblockmobile/BlocksAdapter.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class BlocksAdapter(private val listBlocks:ArrayList<Block>) : RecyclerView.Adap
2121
}
2222

2323
override fun getItemViewType(position: Int): Int {
24-
val viewType = when(listBlocks[position].getBlockType()){
24+
val viewType = when(listBlocks[position].type){
2525
"EntryPoint" -> R.layout.item_entry_point
2626
"UndefinedVariable" -> R.layout.item_undefined_var
2727
"UndefinedArray" -> R.layout.item_undefined_array
@@ -48,8 +48,8 @@ class BlocksAdapter(private val listBlocks:ArrayList<Block>) : RecyclerView.Adap
4848

4949
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
5050
listBlocks[position].holder = holder
51-
if (listBlocks[position].getBlockType() == "EntryPoint" || listBlocks[position].getBlockType() == "Begin" ||
52-
listBlocks[position].getBlockType() == "End" || listBlocks[position].getBlockType() == "Else"){
51+
if (listBlocks[position].type == "EntryPoint" || listBlocks[position].type== "Begin" ||
52+
listBlocks[position].type == "End" || listBlocks[position].type == "Else"){
5353
return
5454
}
5555
holder.editLeft.setOnEditorActionListener { _, actionId, _ ->
@@ -65,8 +65,8 @@ class BlocksAdapter(private val listBlocks:ArrayList<Block>) : RecyclerView.Adap
6565
false
6666
}
6767

68-
holder.textType.text = listBlocks[position].getBlockType()
69-
holder.textStatus.text = listBlocks[position].getBlockStatus()
68+
holder.textType.text = listBlocks[position].type
69+
holder.textStatus.text = listBlocks[position].status
7070

7171
holder.editLeft.setText(listBlocks[position].inputLeftEdit)
7272
holder.editMedium.setText(listBlocks[position].inputMediumEdit)

app/src/main/java/com/apochromat/codeblockmobile/ProjectActivity.kt

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@ class ProjectActivity : AppCompatActivity() {
4040
supportActionBar?.setDisplayHomeAsUpEnabled(true)
4141
supportActionBar?.title = "newProject"
4242
bindingClass.blocksRV.isDrawingCacheEnabled = true
43-
bindingClass.blocksRV.setItemViewCacheSize(100);
43+
bindingClass.blocksRV.setItemViewCacheSize(100)
4444

4545
createConsoleView()
4646
createBlocksView()
4747

4848
init()
4949

5050
mainHandler = Handler(Looper.getMainLooper())
51-
5251
}
5352
fun kicker() {
5453
listBlocks[0].kickRunning()
@@ -312,16 +311,16 @@ class ProjectActivity : AppCompatActivity() {
312311
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
313312
val position = viewHolder.adapterPosition
314313
if (viewHolder.adapterPosition == 0 ||
315-
listBlocks[position].getBlockType() == "Begin" ||
316-
listBlocks[position].getBlockType() == "End" ||
317-
listBlocks[position].getBlockType() == "Else" ){
314+
listBlocks[position].type == "Begin" ||
315+
listBlocks[position].type == "End" ||
316+
listBlocks[position].type == "Else"){
318317
blocksAdapter.notifyItemRemoved(position)
319318
return
320319
}
321-
else if( listBlocks[position].getBlockType() == "ConditionIf" ||
322-
listBlocks[position].getBlockType() == "ConditionIfElse" ||
323-
listBlocks[position].getBlockType() == "CycleWhile"){
324-
val type = listBlocks[position].getBlockType()
320+
else if( listBlocks[position].type == "ConditionIf" ||
321+
listBlocks[position].type == "ConditionIfElse" ||
322+
listBlocks[position].type == "CycleWhile"){
323+
val type = listBlocks[position].type
325324
listBlocks.remove(listBlocks[position])
326325
listBlocks.remove(listBlocks[position])
327326
blocksAdapter.notifyItemRemoved(position)
@@ -348,13 +347,13 @@ class ProjectActivity : AppCompatActivity() {
348347
}
349348
private fun findNearEnd(start : Int) : Int{
350349
for (i in start until listBlocks.size)
351-
if (listBlocks[i].getBlockType() == "End")
350+
if (listBlocks[i].type == "End")
352351
return i
353352
return -1
354353
}
355354
private fun findNearElse(start : Int) : Int{
356355
for (i in start until listBlocks.size)
357-
if (listBlocks[i].getBlockType() == "Else")
356+
if (listBlocks[i].type == "Else")
358357
return i
359358
return -1
360359
}
@@ -386,28 +385,27 @@ class ProjectActivity : AppCompatActivity() {
386385

387386
private fun check(index : Int) : Pair<Int, Int>{
388387
var i = index
389-
while (listBlocks[i+1].getBlockType() != "End"){
388+
while (listBlocks[i+1].type != "End"){
390389
if (i+1 == listBlocks.size) {
391390
consoleAdapter.addMessage("В строке $i ожидался End")
392391
return Pair(0, 0)
393392
}
394-
if (listBlocks[i].getBlockType() == "ConditionIf" ||
395-
listBlocks[i].getBlockType() == "ConditionIfElse" ||
396-
listBlocks[i].getBlockType() == "CycleWhile"){
397-
val temp : Int
398-
if (listBlocks[i].getBlockType() == "ConditionIfElse")
399-
temp = checkIfElse(i)
393+
if (listBlocks[i].type == "ConditionIf" ||
394+
listBlocks[i].type == "ConditionIfElse" ||
395+
listBlocks[i].type == "CycleWhile"){
396+
val temp = if (listBlocks[i].type == "ConditionIfElse")
397+
checkIfElse(i)
400398
else
401-
temp = checkIf(i)
399+
checkIf(i)
402400

403401
if(temp == 0)
404402
return Pair(0, 0)
405403
else {
406-
if (temp < listBlocks.size && listBlocks[temp].getBlockType() != "End"){
404+
if (temp < listBlocks.size && listBlocks[temp].type != "End"){
407405
connectBlocks(listBlocks[i], listBlocks[temp])
408406
i = temp
409407
}
410-
else if(temp < listBlocks.size && listBlocks[temp].getBlockType() == "End"){
408+
else if(temp < listBlocks.size && listBlocks[temp].type == "End"){
411409
return Pair(i, temp)
412410
}
413411
else if(temp >= listBlocks.size){
@@ -428,14 +426,14 @@ class ProjectActivity : AppCompatActivity() {
428426

429427
private fun checkIfElse(index : Int): Int{
430428
var i = index + 1
431-
if (listBlocks[i].getBlockType() == "Begin"){
429+
if (listBlocks[i].type == "Begin"){
432430
i += 1
433-
if (listBlocks[i].getBlockType() != "End"){
431+
if (listBlocks[i].type != "End"){
434432
connectBlocks(listBlocks[index].begin, listBlocks[i])
435433
val (j, temp) = check(i)
436434
if (j == 0)
437435
return 0
438-
else if (listBlocks[temp].getBlockType() == "End") {
436+
else if (listBlocks[temp].type == "End") {
439437
connectBlocks(listBlocks[j], listBlocks[index].end)
440438
i = temp + 1
441439
}
@@ -448,16 +446,16 @@ class ProjectActivity : AppCompatActivity() {
448446
connectBlocks(listBlocks[index].begin, listBlocks[index].end)
449447
i += 1
450448
}
451-
if (listBlocks[i].getBlockType() == "Else"){
449+
if (listBlocks[i].type == "Else"){
452450
i += 1
453-
if (listBlocks[i].getBlockType() == "Begin"){
451+
if (listBlocks[i].type == "Begin"){
454452
i += 1
455-
if (listBlocks[i].getBlockType() != "End"){
453+
if (listBlocks[i].type != "End"){
456454
connectBlocks(listBlocks[index].beginElse, listBlocks[i])
457455
val (i, temp) = check(i)
458456
if (i == 0)
459457
return 0
460-
else if (listBlocks[temp].getBlockType() == "End") {
458+
else if (listBlocks[temp].type == "End") {
461459
connectBlocks(listBlocks[i], listBlocks[index].end)
462460
return temp + 1
463461
}
@@ -486,17 +484,17 @@ class ProjectActivity : AppCompatActivity() {
486484

487485
private fun checkIf(index : Int): Int{
488486
var i = index + 1
489-
if (listBlocks[i].getBlockType() == "Begin"){
487+
if (listBlocks[i].type == "Begin"){
490488
i += 1
491-
if (listBlocks[i].getBlockType() == "End"){
489+
if (listBlocks[i].type == "End"){
492490
connectBlocks(listBlocks[index].begin, listBlocks[index].end)
493491
return i + 1
494492
}
495493
connectBlocks(listBlocks[index].begin, listBlocks[i])
496494
val (i, temp) = check(i)
497495
if (i == 0)
498496
return 0
499-
else if (listBlocks[temp].getBlockType() == "End") {
497+
else if (listBlocks[temp].type == "End") {
500498
connectBlocks(listBlocks[i], listBlocks[index].end)
501499
return temp + 1
502500
}
@@ -512,29 +510,28 @@ class ProjectActivity : AppCompatActivity() {
512510
private fun connectionBlocks() : Boolean{
513511
var i = 0
514512
while (i < listBlocks.size-1) {
515-
if(listBlocks[i].getBlockType() == "Begin"){
513+
if(listBlocks[i].type == "Begin"){
516514
consoleAdapter.addMessage("Обнаружен не к месту Begin в строке $i")
517515
return false
518516
}
519-
else if(listBlocks[i].getBlockType() == "End"){
517+
else if(listBlocks[i].type == "End"){
520518
consoleAdapter.addMessage("Обнаружен не к месту End в строке $i")
521519
return false
522520
}
523-
else if(listBlocks[i].getBlockType() == "Else"){
521+
else if(listBlocks[i].type == "Else"){
524522
consoleAdapter.addMessage("Обнаружен не к месту Else в строке $i")
525523
return false
526524
}
527-
else if (listBlocks[i].getBlockType() == "ConditionIf" ||
528-
listBlocks[i].getBlockType() == "ConditionIfElse" ||
529-
listBlocks[i].getBlockType() == "CycleWhile") {
525+
else if (listBlocks[i].type == "ConditionIf" ||
526+
listBlocks[i].type == "ConditionIfElse" ||
527+
listBlocks[i].type == "CycleWhile") {
530528

531-
val temp : Int
532-
if(listBlocks[i].getBlockType() == "ConditionIfElse")
533-
temp = checkIfElse(i)
529+
val temp = if (listBlocks[i].type == "ConditionIfElse")
530+
checkIfElse(i)
534531
else
535-
temp = checkIf(i)
536-
if(temp == 0)
537-
return false
532+
checkIf(i)
533+
534+
if (temp == 0) return false
538535
else {
539536
if (temp < listBlocks.size){
540537
connectBlocks(listBlocks[i], listBlocks[temp])
@@ -559,40 +556,38 @@ class ProjectActivity : AppCompatActivity() {
559556
for (i in 0 until listBlocks.size){
560557
listBlocks[i].indexListBlocks = i
561558
listBlocks[i].crutch = true
562-
if (listBlocks[i].status != "OK"){
563-
listBlocks[i].status = "OK"
559+
if (listBlocks[i].status != ok()){
560+
listBlocks[i].status = ok()
564561
blocksAdapter.notifyItemChanged(i)
565562
}
566-
if (listBlocks[i].getBlockType() == "ConsoleInput"){
563+
if (listBlocks[i].type == "ConsoleInput"){
567564
listBlocks[i].activity = this
568565
}
569566

570567
}
571568
if (!connectionBlocks()) {
572-
consoleAdapter.addMessage("Program finished with status: Fail")
569+
consoleAdapter.addMessage(programFinish("Fail"))
573570
return
574571
}
575572
//printAllConnections()
576573
Block.isProgramRunning = true
577574
listBlocks[0].run()
578575
//printAllConnections()
579576
//disconnectAllBlocks()
580-
581-
582577
}
583578

584579
private fun printAllConnections(){
585580
for (i in 0 until listBlocks.size){
586-
consoleAdapter.addMessage("$i "+ (listBlocks[i].getPrevBlock()
587-
?.getBlockType() ?: "null") + "-"+ listBlocks[i].getBlockType() + "-" +
588-
(listBlocks[i].getNextBlock()?.getBlockType() ?: "null"))
581+
consoleAdapter.addMessage("$i "+ (listBlocks[i].prevBlock
582+
?.type?: "null") + "-"+ listBlocks[i].type + "-" +
583+
(listBlocks[i].nextBlock?.type?: "null"))
589584

590585
}
591586
}
592587
private fun disconnectAllBlocks(){
593588
for (i in 0 until listBlocks.size){
594-
listBlocks[i].setPrevBlock(null)
595-
listBlocks[i].setNextBlock(null)
589+
listBlocks[i].prevBlock = null
590+
listBlocks[i].nextBlock = null
596591
listBlocks[i].begin = Begin()
597592
listBlocks[i].end = End()
598593
listBlocks[i].beginElse = Begin()

app/src/main/java/com/apochromat/codeblockmobile/logic/Assignment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Assignment : Block() {
1111
private var inputName: String = ""
1212

1313
init {
14-
setBlockType("Assignment")
14+
type = "Assignment"
1515
}
1616

1717
private fun initVar(){
@@ -30,11 +30,11 @@ class Assignment : Block() {
3030
val obj = defineInput(heap, inputName)
3131
name = obj.second
3232
if (obj.first !in listOf(tagArray(), tagVariable())) {
33-
setBlockStatus(obj.first)
33+
status = obj.first
3434
return
3535
}
3636
val calculated = arithmetics(heap, inputValue)
37-
setBlockStatus(calculated.first)
37+
status = calculated.first
3838
if (calculated.first != ok()) return
3939
value = calculated.second
4040
when (obj.first) {

app/src/main/java/com/apochromat/codeblockmobile/logic/Begin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package com.apochromat.codeblockmobile.logic
22

33
class Begin : Block() {
44
init {
5-
setBlockType("Begin")
5+
type = "Begin"
66
}
77
}

app/src/main/java/com/apochromat/codeblockmobile/logic/Block.kt

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,71 +42,37 @@ open class Block {
4242
var crutch = true
4343

4444
// Ссылки на следующий и предыдущий блоки
45-
private var nextBlock: Block? = null
46-
private var prevBlock: Block? = null
45+
var nextBlock: Block? = null
46+
var prevBlock: Block? = null
4747

4848
// Тип, статус и идентификатор блока
49-
private var type: String = ""
49+
var type: String = ""
5050
var status: String = ok()
5151

52-
// Операции с типом, статусом и идентификатором блока
53-
fun setBlockType(input: String) {
54-
type = input
55-
}
56-
57-
fun getBlockType(): String {
58-
return type
59-
}
60-
61-
fun setBlockStatus(input: String) {
62-
status = input
63-
}
64-
65-
fun getBlockStatus(): String {
66-
return status
67-
}
68-
6952
// Получить доступ к хранилищу переменных
7053
fun accessHeap(): Heap {
7154
return heap
7255
}
7356

74-
// Операции с ссылками на предыдущий и следующий блоки
75-
fun setNextBlock(block: Block?) {
76-
nextBlock = block
77-
}
78-
79-
fun getNextBlock(): Block? {
80-
return nextBlock
81-
}
82-
83-
fun setPrevBlock(block: Block?) {
84-
prevBlock = block
85-
}
86-
87-
fun getPrevBlock(): Block? {
88-
return prevBlock
89-
}
90-
9157
open fun executeBlock() {
9258
status = ok()
9359
}
9460
open fun kickRunning() {}
9561

9662
open fun run() {
97-
if (getBlockType() == "ConsoleInput"){
63+
if (type == "ConsoleInput"){
9864
executeBlock()
9965
}
10066
else{
10167
executeBlock()
10268
when {
103-
getNextBlock() == null -> {
69+
nextBlock == null -> {
10470
isProgramRunning = false
10571
adapterConsole.addMessage(programFinish(status))
10672
adapterBlocks.notifyItemChanged(indexListBlocks)
10773
}
108-
getBlockStatus() == ok() -> {
109-
callStack.push(getNextBlock())
74+
status == ok() -> {
75+
callStack.push(nextBlock)
11076
}
11177
else -> {
11278
isProgramRunning = false

0 commit comments

Comments
 (0)