Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. package com.di7ak.botmaker.editor
  2.  
  3. import android.os.Handler
  4. import android.os.Looper
  5. import com.di7ak.botmaker.bot.*
  6. import de.blox.graphview.Graph
  7. import de.blox.graphview.Node
  8. import java.util.*
  9.  
  10. class EditorController {
  11. private lateinit var bot: Bot
  12. private lateinit var graph: Graph
  13. private var view: EditorFragment? = null
  14. private var nodeMap = mutableMapOf<Int, Node>()
  15.  
  16. fun bindView(view: EditorFragment) {
  17. this.view = view
  18. }
  19.  
  20. fun unbindView() {
  21. view = null
  22. }
  23.  
  24. fun loadBot(path: String) {
  25. if(!BotManager.isExists(path)) {
  26. bot = Bot(nodes = mutableListOf(BotNode(1, "main")))
  27. graph = createGraph(bot)
  28. view?.displayGraph(graph)
  29. view?.displayPropertiesDialog(bot.manifest)
  30. return
  31. }
  32.  
  33. val handler = Handler(Looper.getMainLooper())
  34. view?.displayProgress(true)
  35.  
  36. Thread {
  37. try {
  38. bot = BotManager.open(path)
  39. graph = createGraph(bot)
  40.  
  41. handler.post {
  42. view?.displayProgress(false)
  43. view?.displayGraph(graph)
  44. }
  45. } catch (e: BotLoadException) {
  46. handler.post {
  47. view?.displayProgress(false)
  48. view?.displayError(e.message ?: "load error")
  49. }
  50. }
  51. }.start()
  52. }
  53.  
  54. fun removeNode(node: Node) {
  55. val id = nodeMap.keys.find { nodeMap[it] == node } ?: 0
  56. val botNode = bot.nodes.find { it.id == id }
  57.  
  58. bot.edges = bot.edges.asSequence().filter { ids ->
  59. val contains = ids[0] == id
  60. if(contains) {
  61. nodeMap = nodeMap.filter {
  62. it.key == ids[1]
  63. }.toMutableMap()
  64. bot.nodes = bot.nodes.asSequence().filter {
  65. it.id != ids[0]
  66. }.toMutableList()
  67. }
  68. !contains
  69. }.toMutableList()
  70.  
  71. nodeMap.remove(id)
  72. bot.nodes.remove(botNode)
  73. graph.removeNode(node)
  74. view?.notifyNodeRemoved(node)
  75. }
  76.  
  77. fun addNode(node: Node, name: String) {
  78. val id = nodeMap.keys.find { nodeMap[it] == node } ?: 0
  79. val newId = generateNodeId()
  80. val newNode = Node(name + newId)
  81.  
  82. bot.nodes.add(BotNode(newId, name + newId))
  83. bot.edges.add(listOf(id, newId))
  84.  
  85. nodeMap[newId] = newNode
  86. graph.addEdge(node, newNode)
  87.  
  88. view?.notifyNodeAdded(newNode)
  89. }
  90.  
  91. private fun generateNodeId() : Int {
  92. val random = Random()
  93. var id = 1
  94. while (nodeMap.containsKey(id)) {
  95. id = Math.abs(random.nextInt())
  96. }
  97. return id
  98. }
  99.  
  100. fun saveBot(path: String) {
  101. if(!::bot.isInitialized) return
  102.  
  103. val handler = Handler(Looper.getMainLooper())
  104. view?.displayProgress(true)
  105.  
  106. Thread {
  107. try {
  108. BotManager.save(path, bot)
  109.  
  110. handler.post {
  111. view?.displayProgress(false)
  112. }
  113. } catch (e: BotLoadException) {
  114. handler.post {
  115. view?.displayProgress(false)
  116. view?.displayError(e.message ?: "save error")
  117. }
  118. }
  119. }.start()
  120. }
  121.  
  122. fun setBotManifest(manifest: BotManifest) {
  123. if(!::bot.isInitialized) return
  124.  
  125. bot.manifest = manifest
  126. view?.hidePropertiesDialog()
  127. }
  128.  
  129. private fun createGraph(bot: Bot) : Graph {
  130. val graph = Graph()
  131. nodeMap = mutableMapOf()
  132.  
  133. bot.nodes.forEach {
  134. val node = Node(it.name)
  135. nodeMap[it.id] = node
  136. graph.addNode(node)
  137. }
  138.  
  139. bot.edges.filter {
  140. it.size == 2 && nodeMap.containsKey(it[0]) && nodeMap.containsKey(it[1])
  141. }.forEach {
  142. graph.addEdge(nodeMap[it[0]]!!, nodeMap[it[1]]!!)
  143. }
  144.  
  145. return graph
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement