sparkling-debug-tool 2.1.0-rc.11 → 2.1.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,19 +3,96 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  package com.tiktok.sparkling.debugtool
5
5
 
6
+ import android.app.Activity
7
+ import android.app.AlertDialog
6
8
  import android.app.Application
9
+ import android.content.Context
10
+ import android.os.Looper
11
+ import android.text.InputType
12
+ import android.widget.EditText
13
+ import android.widget.Toast
7
14
  import com.lynx.devtool.LynxDevtoolEnv
8
15
  import com.lynx.service.devtool.LynxDevToolService
9
16
  import com.lynx.tasm.LynxEnv
10
17
  import com.lynx.tasm.service.LynxServiceCenter
11
18
 
12
19
  object SparklingDebugTool {
20
+ private const val PREFS_NAME = "sparkling_debug_tool"
21
+ private const val KEY_DEV_URL = "dev_url"
22
+
13
23
  @JvmStatic
14
24
  fun init(application: Application) {
25
+ // Preset values must be set BEFORE LynxEnv flags so the DevTool
26
+ // service picks them up during initialization.
27
+ LynxDevToolService.INSTANCE.setLynxDebugPresetValue(true)
28
+ LynxDevToolService.INSTANCE.setLogBoxPresetValue(true)
29
+ LynxDevToolService.INSTANCE.setLoadJsBridge(true)
30
+
15
31
  LynxServiceCenter.inst().registerService(LynxDevToolService.INSTANCE)
16
32
  LynxEnv.inst().enableLynxDebug(true)
17
33
  LynxEnv.inst().enableDevtool(true)
18
34
  LynxEnv.inst().enableLogBox(true)
19
35
  LynxDevtoolEnv.inst().enableLongPressMenu(true)
20
36
  }
37
+
38
+ @JvmStatic
39
+ fun getDevUrl(context: Context, fallback: String): String {
40
+ val stored = context.applicationContext
41
+ .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
42
+ .getString(KEY_DEV_URL, null)
43
+ ?.trim()
44
+ return if (stored.isNullOrEmpty()) fallback else stored
45
+ }
46
+
47
+ @JvmStatic
48
+ fun setDevUrl(context: Context, url: String) {
49
+ context.applicationContext
50
+ .getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
51
+ .edit()
52
+ .putString(KEY_DEV_URL, url.trim())
53
+ .apply()
54
+ }
55
+
56
+ @JvmStatic
57
+ fun showDevUrlDialog(
58
+ activity: Activity,
59
+ initialUrl: String? = null,
60
+ onSaved: ((String) -> Unit)? = null,
61
+ ) {
62
+ if (Looper.myLooper() != Looper.getMainLooper()) {
63
+ activity.runOnUiThread { showDevUrlDialog(activity, initialUrl, onSaved) }
64
+ return
65
+ }
66
+
67
+ val input = EditText(activity).apply {
68
+ setText(initialUrl ?: "")
69
+ hint = "http://10.0.2.2:5969/main.lynx.bundle"
70
+ inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI
71
+ setSelection(text.length)
72
+ }
73
+
74
+ val dialog = AlertDialog.Builder(activity)
75
+ .setTitle("Set Sparkling Dev URL")
76
+ .setMessage("Update the main Lynx bundle URL for debug mode.")
77
+ .setView(input)
78
+ .setNegativeButton("Cancel", null)
79
+ .setPositiveButton("Save", null)
80
+ .create()
81
+
82
+ dialog.setOnShowListener {
83
+ val saveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
84
+ saveButton.setOnClickListener {
85
+ val value = input.text?.toString()?.trim().orEmpty()
86
+ if (value.isEmpty()) {
87
+ Toast.makeText(activity, "Dev URL cannot be empty", Toast.LENGTH_SHORT).show()
88
+ return@setOnClickListener
89
+ }
90
+ setDevUrl(activity, value)
91
+ onSaved?.invoke(value)
92
+ dialog.dismiss()
93
+ }
94
+ }
95
+
96
+ dialog.show()
97
+ }
21
98
  }
@@ -4,12 +4,79 @@
4
4
 
5
5
  import Foundation
6
6
  import Lynx
7
+ import UIKit
7
8
 
8
9
  @objcMembers
9
10
  public class SparklingDebugTool: NSObject {
11
+ private static let devURLKey = "sparkling.debug.dev_url"
12
+
10
13
  public static func setup() {
11
- LynxEnv.sharedInstance().lynxDebugEnabled = true
12
- LynxEnv.sharedInstance().devtoolEnabled = true
13
- LynxEnv.sharedInstance().logBoxEnabled = true
14
+ // Preset values must be set BEFORE LynxEnv flags so the DevTool
15
+ // service picks them up during initialization.
16
+ if let devtool = LynxServices.getInstanceWith(
17
+ NSProtocolFromString("LynxServiceDevToolProtocol")!
18
+ ) as? LynxServiceDevToolProtocol {
19
+ devtool.logBoxPresetValue = true
20
+ devtool.lynxDebugPresetValue = true
21
+ }
22
+
23
+ let env = LynxEnv.sharedInstance()
24
+ env.lynxDebugEnabled = true
25
+ env.devtoolEnabled = true
26
+ env.logBoxEnabled = true
27
+
28
+ // Enable DebugRouter so the DevTool desktop app can discover and
29
+ // connect to this app (via USB or network).
30
+ if let routerClass = NSClassFromString("DebugRouter"),
31
+ let instance = routerClass.perform(NSSelectorFromString("instance"))?.takeUnretainedValue(),
32
+ instance.responds(to: NSSelectorFromString("enableAllSessions")) {
33
+ instance.perform(NSSelectorFromString("enableAllSessions"))
34
+ }
35
+ }
36
+
37
+ public static func devURL(fallback: String) -> String {
38
+ let stored = UserDefaults.standard.string(forKey: devURLKey)?.trimmingCharacters(in: .whitespacesAndNewlines)
39
+ if let stored, !stored.isEmpty {
40
+ return stored
41
+ }
42
+ return fallback
43
+ }
44
+
45
+ public static func setDevURL(_ url: String) {
46
+ let normalized = url.trimmingCharacters(in: .whitespacesAndNewlines)
47
+ UserDefaults.standard.set(normalized, forKey: devURLKey)
48
+ }
49
+
50
+ public static func showDevURLDialog(
51
+ from viewController: UIViewController,
52
+ initialURL: String?,
53
+ onSaved: ((String) -> Void)? = nil
54
+ ) {
55
+ DispatchQueue.main.async {
56
+ let alert = UIAlertController(
57
+ title: "Set Sparkling Dev URL",
58
+ message: "Update the main Lynx bundle URL for debug mode.",
59
+ preferredStyle: .alert
60
+ )
61
+
62
+ alert.addTextField { textField in
63
+ textField.placeholder = "http://localhost:5969/main.lynx.bundle"
64
+ textField.keyboardType = .URL
65
+ textField.text = initialURL
66
+ textField.clearButtonMode = .whileEditing
67
+ }
68
+
69
+ alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
70
+ alert.addAction(UIAlertAction(title: "Save", style: .default, handler: { _ in
71
+ let value = alert.textFields?.first?.text?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
72
+ guard !value.isEmpty else {
73
+ return
74
+ }
75
+ setDevURL(value)
76
+ onSaved?(value)
77
+ }))
78
+
79
+ viewController.present(alert, animated: true)
80
+ }
14
81
  }
15
82
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sparkling-debug-tool",
3
- "version": "2.1.0-rc.11",
3
+ "version": "2.1.0-rc.14",
4
4
  "description": "Sparkling debug tool SDK",
5
5
  "homepage": "https://tiktok.github.io/sparkling/",
6
6
  "repository": {