Ta android + ios

This commit is contained in:
LYP
2024-08-07 20:22:28 +08:00
parent 69fee08c61
commit 76458664d8
722 changed files with 41709 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
//
// TATrackTimer.h
// ThinkingSDK
//
// Created by Yangxiongon 2022/6/1.
// Copyright © 2022 thinking. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TATrackTimer : NSObject
- (void)trackEvent:(NSString *)eventName withSystemUptime:(NSTimeInterval)systemUptime;
- (void)enterForegroundWithSystemUptime:(NSTimeInterval)systemUptime;
- (void)enterBackgroundWithSystemUptime:(NSTimeInterval)systemUptime;
- (NSTimeInterval)foregroundDurationOfEvent:(NSString * _Nonnull)eventName isActive:(BOOL)isActive systemUptime:(NSTimeInterval)systemUptime;
- (NSTimeInterval)backgroundDurationOfEvent:(NSString * _Nonnull)eventName isActive:(BOOL)isActive systemUptime:(NSTimeInterval)systemUptime;
- (void)removeEvent:(NSString * _Nonnull)eventName;
- (BOOL)isExistEvent:(NSString * _Nonnull)eventName;
- (void)clear;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: bc2b13ed52c2fe74289ee877867b72e5
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,125 @@
//
// TATrackTimer.m
// ThinkingSDK
//
// Created by Yangxiongon 2022/6/1.
// Copyright © 2022 thinking. All rights reserved.
//
#import "TATrackTimer.h"
#import "TATrackTimerItem.h"
#import "TAThreadSafeDictionary.h"
#import "TDDeviceInfo.h"
@interface TATrackTimer ()
@property (nonatomic, strong) TAThreadSafeDictionary *events;
@end
@implementation TATrackTimer
- (instancetype)init
{
self = [super init];
if (self) {
self.events = [TAThreadSafeDictionary dictionary];
}
return self;
}
- (void)trackEvent:(NSString *)eventName withSystemUptime:(NSTimeInterval)systemUptime {
if (!eventName.length) {
return;
}
TATrackTimerItem *item = [[TATrackTimerItem alloc] init];
item.beginTime = systemUptime ?: [TDDeviceInfo uptime];
self.events[eventName] = item;
}
- (void)enterForegroundWithSystemUptime:(NSTimeInterval)systemUptime {
NSArray *keys = [self.events allKeys];
for (NSString *key in keys) {
TATrackTimerItem *item = self.events[key];
item.beginTime = systemUptime;
if (item.enterBackgroundTime == 0) {
item.backgroundDuration = 0;
} else {
item.backgroundDuration = systemUptime - item.enterBackgroundTime + item.backgroundDuration;
}
}
}
- (void)enterBackgroundWithSystemUptime:(NSTimeInterval)systemUptime {
NSArray *keys = [self.events allKeys];
for (NSString *key in keys) {
TATrackTimerItem *item = self.events[key];
item.enterBackgroundTime = systemUptime;
item.foregroundDuration = systemUptime - item.beginTime + item.foregroundDuration;
}
}
- (NSTimeInterval)foregroundDurationOfEvent:(NSString *)eventName isActive:(BOOL)isActive systemUptime:(NSTimeInterval)systemUptime {
if (!eventName.length) {
return 0;
}
TATrackTimerItem *item = self.events[eventName];
if (!item) {
return 0;
}
if (isActive) {
NSTimeInterval duration = systemUptime - item.beginTime + item.foregroundDuration;
return [self validateDuration:duration eventName:eventName];
} else {
return [self validateDuration:item.foregroundDuration eventName:eventName];
}
}
- (NSTimeInterval)backgroundDurationOfEvent:(NSString *)eventName isActive:(BOOL)isActive systemUptime:(NSTimeInterval)systemUptime {
if (!eventName.length) {
return 0;
}
TATrackTimerItem *item = self.events[eventName];
if (!item) {
return 0;
}
if (isActive) {
return [self validateDuration:item.backgroundDuration eventName:eventName];
} else {
NSTimeInterval duration = 0;
if (item.enterBackgroundTime == 0) {
duration = 0;
} else {
duration = systemUptime - item.enterBackgroundTime + item.backgroundDuration;
}
return [self validateDuration:duration eventName:eventName];
}
}
- (void)removeEvent:(NSString *)eventName {
[self.events removeObjectForKey:eventName];
}
- (BOOL)isExistEvent:(NSString *)eventName {
return self.events[eventName] != nil;
}
- (void)clear {
[self.events removeAllObjects];
}
//MARK: - Private Methods
- (NSTimeInterval)validateDuration:(NSTimeInterval)duration eventName:(NSString *)eventName {
NSInteger max = 3600 * 24;
if (duration >= max) {
return max;
}
return duration;
}
@end

View File

@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 10e8d03ff07d67240bf27d1394d7c4d2
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,25 @@
//
// TATrackTimerItem.h
// ThinkingSDK
//
// Created by Yangxiongon 2022/6/1.
// Copyright © 2022 thinking. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TATrackTimerItem : NSObject
/// The moment when the event starts to be recorded (the total time the device has been running)
@property (nonatomic, assign) NSTimeInterval beginTime;
/// Accumulated time in the foreground
@property (nonatomic, assign) NSTimeInterval foregroundDuration;
/// The time the event entered the background (total time the device has been running)
@property (nonatomic, assign) NSTimeInterval enterBackgroundTime;
/// accumulated time in the background
@property (nonatomic, assign) NSTimeInterval backgroundDuration;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: e202e328f1f39c943803f79bdbd42e8d
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
//
// TATrackTimerItem.m
// ThinkingSDK
//
// Created by Yangxiongon 2022/6/1.
// Copyright © 2022 thinking. All rights reserved.
//
#import "TATrackTimerItem.h"
@implementation TATrackTimerItem
-(NSString *)description {
return [NSString stringWithFormat:@"beginTime: %lf, foregroundDuration: %lf, enterBackgroundTime: %lf, backgroundDuration: %lf", _beginTime, _foregroundDuration, _enterBackgroundTime, _backgroundDuration];;
}
@end

View File

@@ -0,0 +1,33 @@
fileFormatVersion: 2
guid: 327aca07f69704e4a88cbb6e68727baf
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
userData:
assetBundleName:
assetBundleVariant: