|
1 | 1 | package com.th3rdwave.safeareacontext |
2 | 2 |
|
3 | 3 | import android.content.Context |
4 | | -import android.util.Log |
5 | 4 | import android.view.View |
6 | 5 | import android.view.ViewTreeObserver |
7 | 6 | import com.facebook.react.bridge.Arguments |
8 | 7 | import com.facebook.react.uimanager.StateWrapper |
9 | 8 | import com.facebook.react.uimanager.UIManagerModule |
10 | 9 | import com.facebook.react.views.view.ReactViewGroup |
11 | | -import java.util.concurrent.locks.ReentrantLock |
12 | | -import kotlin.concurrent.withLock |
13 | | - |
14 | | -private const val MAX_WAIT_TIME_NANO = 500000000L // 500ms |
15 | 10 |
|
16 | 11 | class SafeAreaView(context: Context?) : |
17 | 12 | ReactViewGroup(context), ViewTreeObserver.OnPreDrawListener { |
@@ -50,54 +45,9 @@ class SafeAreaView(context: Context?) : |
50 | 45 | val uiManager = reactContext.getNativeModule(UIManagerModule::class.java) |
51 | 46 | if (uiManager != null) { |
52 | 47 | uiManager.setViewLocalData(id, localData) |
53 | | - // Sadly there doesn't seem to be a way to properly dirty a yoga node from java, so if we |
54 | | - // are in |
55 | | - // the middle of a layout, we need to recompute it. There is also no way to know whether |
56 | | - // we |
57 | | - // are in the middle of a layout so always do it. |
58 | | - reactContext.runOnNativeModulesQueueThread { |
59 | | - uiManager.uiImplementation.dispatchViewUpdates(-1) |
60 | | - } |
61 | | - waitForReactLayout() |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - private fun waitForReactLayout() { |
68 | | - // Block the main thread until the native module thread is finished with |
69 | | - // its current tasks. To do this we use the done boolean as a lock and enqueue |
70 | | - // a task on the native modules thread. When the task runs we can unblock the |
71 | | - // main thread. This should be safe as long as the native modules thread |
72 | | - // does not block waiting on the main thread. |
73 | | - var done = false |
74 | | - val lock = ReentrantLock() |
75 | | - val condition = lock.newCondition() |
76 | | - val startTime = System.nanoTime() |
77 | | - var waitTime = 0L |
78 | | - getReactContext(this).runOnNativeModulesQueueThread { |
79 | | - lock.withLock { |
80 | | - if (!done) { |
81 | | - done = true |
82 | | - condition.signal() |
83 | 48 | } |
84 | 49 | } |
85 | 50 | } |
86 | | - lock.withLock { |
87 | | - while (!done && waitTime < MAX_WAIT_TIME_NANO) { |
88 | | - try { |
89 | | - condition.awaitNanos(MAX_WAIT_TIME_NANO) |
90 | | - } catch (ex: InterruptedException) { |
91 | | - // In case of an interrupt just give up waiting. |
92 | | - done = true |
93 | | - } |
94 | | - waitTime += System.nanoTime() - startTime |
95 | | - } |
96 | | - } |
97 | | - // Timed out waiting. |
98 | | - if (waitTime >= MAX_WAIT_TIME_NANO) { |
99 | | - Log.w("SafeAreaView", "Timed out waiting for layout.") |
100 | | - } |
101 | 51 | } |
102 | 52 |
|
103 | 53 | fun setMode(mode: SafeAreaViewMode) { |
|
0 commit comments