creek

The AI Image Editor of 2030

android/app/src/main/kotlin/com/example/adobe/MainActivity.kt (4410B)


 1package com.creek.ui
 2
 3import io.flutter.embedding.android.FlutterActivity
 4import io.flutter.embedding.engine.FlutterEngine
 5import io.flutter.plugin.common.MethodChannel
 6import kotlinx.coroutines.CoroutineScope
 7import kotlinx.coroutines.Dispatchers
 8import kotlinx.coroutines.launch
 9import kotlinx.coroutines.withContext
10import android.content.Intent
11
12class MainActivity : FlutterActivity() {
13    private val CHANNEL = "com.creek.ui/methods"
14    private val scope = CoroutineScope(Dispatchers.Default)
15
16    override fun onNewIntent(intent: Intent) {
17        super.onNewIntent(intent)
18        setIntent(intent) // This updates the activity's intent to the new one
19    }
20
21    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
22        super.configureFlutterEngine(flutterEngine)
23
24        // Initialize Python immediately in the background
25        scope.launch(Dispatchers.IO) {
26            ImageAnalyzer.initializePython(context)
27        }
28
29        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
30            // Launch a new coroutine for every method call on the IO dispatcher for parallel execution
31            scope.launch(Dispatchers.IO) {
32                try {
33                    val response: Any? = when (call.method) {
34                        "analyzeLayout" -> {
35                            val path = call.argument<String>("imagePath")!!
36                            ImageAnalyzer.analyzeLayout(path)
37                        }
38                        "analyzeColorStyle" -> {
39                            val path = call.argument<String>("imagePath")!!
40                            ImageAnalyzer.analyzeColorStyle(path)
41                        }
42                        "downloadInstagramImage" -> {
43                            val url = call.argument<String>("url")!!
44                            val outputDir = call.argument<String>("outputDir")!!
45                            ImageAnalyzer.downloadInstagramImage(url, outputDir)
46                        }
47                        "generateStylesheet" -> {
48                            val jsonList = call.argument<List<String>>("jsonList")!!
49                            ImageAnalyzer.generateStylesheet(jsonList)
50                        }
51                        "generateMagicPrompt" -> {
52                            val stylesheetJson = call.argument<String>("stylesheetJson") ?: "{}"
53                            val caption = call.argument<String>("caption") ?: ""
54                            val userPrompt = call.argument<String>("userPrompt") ?: ""
55                            ImageAnalyzer.generateMagicPrompt(stylesheetJson, caption, userPrompt)
56                        }
57                        "getShareSource" -> {
58                            val componentName = intent.component?.className
59                            when {
60                                componentName?.contains("ShareToFiles") == true -> "files"
61                                componentName?.contains("ShareToMoodboards") == true -> "moodboards"
62                                else -> "moodboards"
63                            }
64                        }
65                        else -> null
66                    }
67
68                    // Switch back to the Main Thread to send the result to Flutter
69                    withContext(Dispatchers.Main) {
70                        if (response != null) {
71                            result.success(response)
72                        } else {
73                            if (call.method == "downloadInstagramImage") {
74                                // Specific error handling for downloader if needed
75                                result.error("ERROR", "Download returned null", null)
76                            } else if (response == null) {
77                                // Python returned null (error inside script)
78                                result.error("PYTHON_ERROR", "Python returned null for ${call.method}", null)
79                            } else {
80                                result.notImplemented()
81                            }
82                        }
83                    }
84                } catch (e: Exception) {
85                    withContext(Dispatchers.Main) {
86                        result.error("EXCEPTION", e.message, e.stackTraceToString())
87                    }
88                }
89            }
90        }
91    }
92}