Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.di7ak.botmaker.editor
- import android.os.Handler
- import android.os.Looper
- import com.di7ak.botmaker.bot.*
- import de.blox.graphview.Graph
- import de.blox.graphview.Node
- import java.util.*
- class EditorController {
- private lateinit var bot: Bot
- private lateinit var graph: Graph
- private var view: EditorFragment? = null
- private var nodeMap = mutableMapOf<Int, Node>()
- fun bindView(view: EditorFragment) {
- this.view = view
- }
- fun unbindView() {
- view = null
- }
- fun loadBot(path: String) {
- if(!BotManager.isExists(path)) {
- bot = Bot(nodes = mutableListOf(BotNode(1, "main")))
- graph = createGraph(bot)
- view?.displayGraph(graph)
- view?.displayPropertiesDialog(bot.manifest)
- return
- }
- val handler = Handler(Looper.getMainLooper())
- view?.displayProgress(true)
- Thread {
- try {
- bot = BotManager.open(path)
- graph = createGraph(bot)
- handler.post {
- view?.displayProgress(false)
- view?.displayGraph(graph)
- }
- } catch (e: BotLoadException) {
- handler.post {
- view?.displayProgress(false)
- view?.displayError(e.message ?: "load error")
- }
- }
- }.start()
- }
- fun removeNode(node: Node) {
- val id = nodeMap.keys.find { nodeMap[it] == node } ?: 0
- val botNode = bot.nodes.find { it.id == id }
- bot.edges = bot.edges.asSequence().filter { ids ->
- val contains = ids[0] == id
- if(contains) {
- nodeMap = nodeMap.filter {
- it.key == ids[1]
- }.toMutableMap()
- bot.nodes = bot.nodes.asSequence().filter {
- it.id != ids[0]
- }.toMutableList()
- }
- !contains
- }.toMutableList()
- nodeMap.remove(id)
- bot.nodes.remove(botNode)
- graph.removeNode(node)
- view?.notifyNodeRemoved(node)
- }
- fun addNode(node: Node, name: String) {
- val id = nodeMap.keys.find { nodeMap[it] == node } ?: 0
- val newId = generateNodeId()
- val newNode = Node(name + newId)
- bot.nodes.add(BotNode(newId, name + newId))
- bot.edges.add(listOf(id, newId))
- nodeMap[newId] = newNode
- graph.addEdge(node, newNode)
- view?.notifyNodeAdded(newNode)
- }
- private fun generateNodeId() : Int {
- val random = Random()
- var id = 1
- while (nodeMap.containsKey(id)) {
- id = Math.abs(random.nextInt())
- }
- return id
- }
- fun saveBot(path: String) {
- if(!::bot.isInitialized) return
- val handler = Handler(Looper.getMainLooper())
- view?.displayProgress(true)
- Thread {
- try {
- BotManager.save(path, bot)
- handler.post {
- view?.displayProgress(false)
- }
- } catch (e: BotLoadException) {
- handler.post {
- view?.displayProgress(false)
- view?.displayError(e.message ?: "save error")
- }
- }
- }.start()
- }
- fun setBotManifest(manifest: BotManifest) {
- if(!::bot.isInitialized) return
- bot.manifest = manifest
- view?.hidePropertiesDialog()
- }
- private fun createGraph(bot: Bot) : Graph {
- val graph = Graph()
- nodeMap = mutableMapOf()
- bot.nodes.forEach {
- val node = Node(it.name)
- nodeMap[it.id] = node
- graph.addNode(node)
- }
- bot.edges.filter {
- it.size == 2 && nodeMap.containsKey(it[0]) && nodeMap.containsKey(it[1])
- }.forEach {
- graph.addEdge(nodeMap[it[0]]!!, nodeMap[it[1]]!!)
- }
- return graph
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement