File tree 4 files changed +51
-19
lines changed
4 files changed +51
-19
lines changed Original file line number Diff line number Diff line change 21
21
#include < rxcpp/operators/rx-observe_on.hpp>
22
22
#include < rxcpp/schedulers/rx-runloop.hpp>
23
23
24
+ #if defined(__ANDROID__)
25
+ #include < android/looper.h>
26
+ #endif
27
+
28
+ #include " Utility/Log.hpp"
29
+
24
30
namespace VGG
25
31
{
26
32
@@ -33,7 +39,15 @@ class RunLoop
33
39
34
40
rxcpp::observe_on_one_worker thread ()
35
41
{
42
+ #if defined(__ANDROID__)
43
+ ALooper* looper = ALooper_forThread ();
44
+ if (looper == nullptr ) {
45
+ looper = ALooper_prepare (ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
46
+ }
47
+ return rxcpp::observe_on_new_thread ();
48
+ #else
36
49
return rxcpp::observe_on_run_loop (m_runLoop);
50
+ #endif
37
51
}
38
52
39
53
bool empty () const
Original file line number Diff line number Diff line change @@ -27,12 +27,15 @@ class Timer
27
27
using TCallback = std::function<void ()>;
28
28
29
29
private:
30
+ double m_interval;
30
31
TCallback m_callback;
32
+ bool m_repeats;
31
33
rxcpp::composite_subscription m_timer;
32
34
33
35
public:
34
36
Timer (double interval, TCallback callback, bool repeats = false );
37
+ bool setup ();
35
38
void invalidate ();
36
39
};
37
40
38
- } // namespace VGG
41
+ } // namespace VGG
Original file line number Diff line number Diff line change @@ -316,14 +316,19 @@ void UIScrollView::setScrollAnimation(std::shared_ptr<UIScrollViewAnimation> ani
316
316
317
317
if (!m_scrollTimer)
318
318
{
319
- m_scrollTimer. reset ( new Timer (
319
+ auto t = new Timer (
320
320
1 . / 60 ,
321
321
[this ]()
322
322
{
323
323
VERBOSE (" UIScrollView: call updateScrollAnimation by timer" );
324
324
this ->updateScrollAnimation ();
325
325
},
326
- true ));
326
+ true );
327
+ m_scrollTimer.reset (t);
328
+ if (!m_scrollTimer->setup ())
329
+ {
330
+ WARN (" Failed to setup scroll timer" );
331
+ }
327
332
}
328
333
}
329
334
Original file line number Diff line number Diff line change @@ -37,35 +37,45 @@ namespace VGG
37
37
{
38
38
39
39
Timer::Timer (double interval, TCallback callback, bool repeats)
40
- : m_callback(callback)
40
+ : m_interval(interval)
41
+ , m_callback(callback)
42
+ , m_repeats(repeats)
41
43
{
42
- auto period = std::chrono::milliseconds (static_cast <int64_t >(interval * 1000 ));
44
+ }
45
+
46
+ bool Timer::setup ()
47
+ {
48
+ auto period = std::chrono::milliseconds (static_cast <int64_t >(m_interval * 1000 ));
43
49
44
- if (repeats )
50
+ if (m_repeats )
45
51
{
46
52
auto values = rxcpp::observable<>::interval (period);
47
- m_timer = values.observe_on (RunLoop::sharedInstance ()->thread ())
48
- .subscribe ([this ](int v) { this ->m_callback (); }, []() {});
53
+ auto observeTarget = RunLoop::sharedInstance ()->thread ();
54
+ auto s = values
55
+ #if defined(__ANDROID__)
56
+ .subscribe_on (rxcpp::observe_on_new_thread ())
57
+ #endif
58
+ .observe_on (observeTarget);
59
+ m_timer = s.subscribe ([this ](int v) { if (this ->m_callback ) { this ->m_callback (); } }, []() {});
49
60
}
50
61
else
51
62
{
52
63
auto values = rxcpp::observable<>::timer (period);
53
- m_timer = values.observe_on (RunLoop::sharedInstance ()->thread ())
54
- .subscribe (
55
- [this ](int v)
56
- {
57
- if (this ->m_callback )
58
- {
59
- this ->m_callback ();
60
- }
61
- },
62
- []() {});
64
+ auto observeTarget = RunLoop::sharedInstance ()->thread ();
65
+ auto s = values
66
+ #if defined(__ANDROID__)
67
+ .subscribe_on (rxcpp::observe_on_new_thread ())
68
+ #endif
69
+ .observe_on (observeTarget);
70
+ m_timer = s.subscribe ([this ](int v) { if (this ->m_callback ) { this ->m_callback (); } }, []() {});
63
71
}
72
+
73
+ return true ;
64
74
}
65
75
66
76
void Timer::invalidate ()
67
77
{
68
78
m_timer.unsubscribe ();
69
79
}
70
80
71
- } // namespace VGG
81
+ } // namespace VGG
You can’t perform that action at this time.
0 commit comments