Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
objc_library(
name = "DeviceProximity",
enable_modules = True,
module_name = "DeviceProximity",
srcs = glob([
"Sources/*.m",
"Sources/*.h",
]),
hdrs = glob([
"PublicHeaders/**/*.h",
]),
includes = [
"PublicHeaders",
],
sdk_frameworks = [
"Foundation",
"UIKit",
],
visibility = [
"//visibility:public",
],
)
@@ -0,0 +1,5 @@
#import <UIKit/UIKit.h>
#import <DeviceProximity/DeviceProximityManager.h>
@@ -0,0 +1,16 @@
#import <Foundation/Foundation.h>
@interface DeviceProximityManager : NSObject
@property (nonatomic, copy) void(^ _Nullable proximityChanged)(bool);
+ (DeviceProximityManager * _Nonnull)shared;
- (bool)currentValue;
- (void)setGloballyEnabled:(bool)value;
- (NSInteger)add:(void (^ _Nonnull)(bool))f;
- (void)remove:(NSInteger)index;
@end
@@ -0,0 +1,15 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DeviceProximityBag : NSObject
- (NSInteger)addItem:(id)item;
- (void)enumerateItems:(void (^)(id))block;
- (void)removeItem:(NSInteger)key;
- (bool)isEmpty;
- (NSArray *)copyItems;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,63 @@
#import "DeviceProximityBag.h"
@interface DeviceProximityBag () {
NSInteger _nextKey;
NSMutableArray *_items;
NSMutableArray *_itemKeys;
}
@end
@implementation DeviceProximityBag
- (instancetype)init {
self = [super init];
if (self != nil) {
_items = [[NSMutableArray alloc] init];
_itemKeys = [[NSMutableArray alloc] init];
}
return self;
}
- (NSInteger)addItem:(id)item {
if (item == nil) {
return -1;
}
NSInteger key = _nextKey;
[_items addObject:item];
[_itemKeys addObject:@(key)];
_nextKey++;
return key;
}
- (void)enumerateItems:(void (^)(id))block {
if (block) {
for (id item in _items) {
block(item);
}
}
}
- (void)removeItem:(NSInteger)key {
NSUInteger index = 0;
for (NSNumber *itemKey in _itemKeys) {
if ([itemKey integerValue] == key) {
[_items removeObjectAtIndex:index];
[_itemKeys removeObjectAtIndex:index];
break;
}
index++;
}
}
- (bool)isEmpty {
return _items.count == 0;
}
- (NSArray *)copyItems {
return [[NSArray alloc] initWithArray:_items];
}
@end
@@ -0,0 +1,115 @@
#import <DeviceProximity/DeviceProximityManager.h>
#import <UIKit/UIKit.h>
#import "DeviceProximityBag.h"
@interface DeviceProximityManager () {
DeviceProximityBag *_subscribers;
bool _proximityState;
bool _globallyEnabled;
}
@end
@implementation DeviceProximityManager
+ (DeviceProximityManager * _Nonnull)shared {
static DeviceProximityManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[DeviceProximityManager alloc] init];
});
return instance;
}
- (bool)currentValue {
return _proximityState;
}
- (instancetype)init {
self = [super init];
if (self != nil) {
_subscribers = [[DeviceProximityBag alloc] init];
__weak DeviceProximityManager *weakSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:UIDeviceProximityStateDidChangeNotification object:[UIDevice currentDevice] queue:[NSOperationQueue mainQueue] usingBlock:^(__unused NSNotification *notification)
{
__strong DeviceProximityManager *strongSelf = weakSelf;
if (strongSelf != nil) {
bool proximityState = [UIDevice currentDevice].proximityState;
if (strongSelf->_proximityState != proximityState) {
strongSelf->_proximityState = proximityState;
if (!strongSelf->_proximityState && [strongSelf->_subscribers isEmpty]) {
[UIDevice currentDevice].proximityMonitoringEnabled = false;
}
for (void (^f)(bool) in [strongSelf->_subscribers copyItems]) {
f(proximityState);
}
if (strongSelf.proximityChanged != nil) {
strongSelf.proximityChanged(proximityState);
}
} else if (!strongSelf->_proximityState && [strongSelf->_subscribers isEmpty]) {
[UIDevice currentDevice].proximityMonitoringEnabled = false;
}
}
}];
}
return self;
}
- (void)setGloballyEnabled:(bool)value {
if (_globallyEnabled != value) {
_globallyEnabled = value;
[self updateState:![_subscribers isEmpty] globallyEnabled:_globallyEnabled];
}
}
- (NSInteger)add:(void (^)(bool))f {
bool wasEmpty = [_subscribers isEmpty];
NSInteger index = [_subscribers addItem:[f copy]];
f(_proximityState);
if (wasEmpty) {
[self updateState:true globallyEnabled:_globallyEnabled];
}
return index;
}
- (void)remove:(NSInteger)index {
bool wasEmpty = [_subscribers isEmpty];
[_subscribers removeItem:index];
if ([_subscribers isEmpty] && !wasEmpty) {
[self updateState:false globallyEnabled:_globallyEnabled];
}
}
- (void)updateState:(bool)hasSubscribers globallyEnabled:(bool)globallyEnabled {
if (hasSubscribers && globallyEnabled) {
[UIDevice currentDevice].proximityMonitoringEnabled = true;
bool deviceProximityState = [UIDevice currentDevice].proximityState;
if (deviceProximityState != _proximityState) {
_proximityState = deviceProximityState;
for (void (^f)(bool) in [_subscribers copyItems]) {
f(_proximityState);
}
if (self.proximityChanged != nil) {
self.proximityChanged(_proximityState);
}
}
} else {
if (_proximityState) {
_proximityState = false;
for (void (^f)(bool) in [_subscribers copyItems]) {
f(_proximityState);
}
if (self.proximityChanged != nil) {
self.proximityChanged(_proximityState);
}
} else {
[UIDevice currentDevice].proximityMonitoringEnabled = false;
}
}
}
@end