Skip to content

Commit f029769

Browse files
committed
Version 1.0.7
1 parent 6faf351 commit f029769

File tree

7 files changed

+165
-9
lines changed

7 files changed

+165
-9
lines changed

.github/workflows/iOS.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: iOS CI
2+
on: [push, pull_request]
3+
jobs:
4+
build-test:
5+
runs-on: macos-14
6+
steps:
7+
- uses: actions/checkout@v4
8+
- name: Select Xcode
9+
run: sudo xcode-select -s /Applications/Xcode_16.0.app
10+
- name: Build & Test (Xcodeproj)
11+
run: |
12+
cd iOS
13+
xcodebuild -project AppDimens.xcodeproj \
14+
-scheme AppDimens \
15+
-sdk iphonesimulator \
16+
-destination 'platform=iOS Simulator,name=iPhone 15,OS=18.0' \
17+
clean build test | xcpretty

Android/appdimens_dynamic/src/main/java/com/appdimens/dynamic/code/AppDimens.kt

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,43 @@ object AppDimens {
6161
}
6262
}
6363

64+
/**
65+
* [EN] Registry of all AppDimensDynamic instances for global cache management.
66+
* [PT] Registro de todas as instâncias AppDimensDynamic para gerenciamento global de cache.
67+
*/
68+
private val dynamicInstances = mutableSetOf<AppDimensDynamic>()
69+
70+
/**
71+
* [EN] Registers an AppDimensDynamic instance for global cache management.
72+
* [PT] Registra uma instância AppDimensDynamic para gerenciamento global de cache.
73+
*/
74+
@JvmStatic
75+
internal fun registerDynamicInstance(instance: AppDimensDynamic) {
76+
dynamicInstances.add(instance)
77+
}
78+
79+
/**
80+
* [EN] Unregisters an AppDimensDynamic instance from global cache management.
81+
* [PT] Remove o registro de uma instância AppDimensDynamic do gerenciamento global de cache.
82+
*/
83+
@JvmStatic
84+
internal fun unregisterDynamicInstance(instance: AppDimensDynamic) {
85+
dynamicInstances.remove(instance)
86+
}
87+
6488
/**
6589
* [EN] Clears all caches from all instances.
6690
* [PT] Limpa todos os caches de todas as instâncias.
6791
*/
6892
@JvmStatic
6993
fun clearAllCaches() {
70-
// This would need to be implemented with a registry of instances
71-
// For now, individual instances will clear their own caches
94+
// Clear the global auto cache
95+
AppDimensAutoCache.clearAll()
96+
97+
// Clear individual instance caches
98+
dynamicInstances.forEach { instance ->
99+
instance.clearInstanceCache()
100+
}
72101
}
73102

74103
/**
@@ -120,6 +149,29 @@ object AppDimens {
120149
* @return True se os ajustes multi-view são ignorados globalmente.
121150
*/
122151
fun isGlobalIgnoreMultiViewAdjustment(): Boolean = globalIgnoreMultiViewAdjustment
152+
153+
/**
154+
* [EN] Sets the global cache control setting.
155+
* @param enabled If true, enables global cache; if false, disables and clears all caches.
156+
* @return The AppDimens instance for chaining.
157+
* [PT] Define a configuração global de controle de cache.
158+
* @param enabled Se verdadeiro, ativa o cache global; se falso, desativa e limpa todos os caches.
159+
* @return A instância AppDimens para encadeamento.
160+
*/
161+
@JvmStatic
162+
fun setGlobalCache(enabled: Boolean): AppDimens {
163+
globalCacheEnabled = enabled
164+
return this
165+
}
166+
167+
/**
168+
* [EN] Gets the current global cache setting.
169+
* @return True if global cache is enabled.
170+
* [PT] Obtém a configuração global atual de cache.
171+
* @return True se o cache global está ativado.
172+
*/
173+
@JvmStatic
174+
fun isGlobalCacheEnabled(): Boolean = globalCacheEnabled
123175

124176
/**
125177
* [EN] Gateway for `AppDimensFixed`.
@@ -186,7 +238,9 @@ object AppDimens {
186238
@JvmStatic
187239
fun dynamic(initialValueDp: Float, ignoreMultiViewAdjustment: Boolean? = null): AppDimensDynamic {
188240
val finalIgnoreMultiView = ignoreMultiViewAdjustment ?: globalIgnoreMultiViewAdjustment
189-
return AppDimensDynamic(initialValueDp, finalIgnoreMultiView)
241+
val instance = AppDimensDynamic(initialValueDp, finalIgnoreMultiView)
242+
registerDynamicInstance(instance)
243+
return instance
190244
}
191245

192246
/**
@@ -201,7 +255,9 @@ object AppDimens {
201255
@JvmStatic
202256
fun dynamic(initialValueInt: Int, ignoreMultiViewAdjustment: Boolean? = null): AppDimensDynamic {
203257
val finalIgnoreMultiView = ignoreMultiViewAdjustment ?: globalIgnoreMultiViewAdjustment
204-
return AppDimensDynamic(initialValueInt.toFloat(), finalIgnoreMultiView)
258+
val instance = AppDimensDynamic(initialValueInt.toFloat(), finalIgnoreMultiView)
259+
registerDynamicInstance(instance)
260+
return instance
205261
}
206262

207263
/**

Android/appdimens_dynamic/src/main/java/com/appdimens/dynamic/code/AppDimensAutoCache.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ object AppDimensAutoCache {
187187
dependencyTracker.clear()
188188
}
189189

190+
/**
191+
* [EN] Clears cache entries that match a specific pattern.
192+
* Useful for clearing instance-specific cache entries.
193+
* [PT] Limpa entradas do cache que correspondem a um padrão específico.
194+
* Útil para limpar entradas de cache específicas da instância.
195+
*/
196+
fun clearByPattern(pattern: String) {
197+
val keysToRemove = cache.keys.filter { it.contains(pattern) }
198+
keysToRemove.forEach { key ->
199+
cache.remove(key)
200+
dependencyTracker.remove(key)
201+
}
202+
}
203+
190204
/**
191205
* [EN] Gets cache statistics for debugging.
192206
* [PT] Obtém estatísticas do cache para debug.

Android/appdimens_dynamic/src/main/java/com/appdimens/dynamic/code/AppDimensDynamic.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class AppDimensDynamic(
243243
fun cache(enable: Boolean = true): AppDimensDynamic {
244244
enableCache = enable
245245
if (!enable) {
246-
autoCache.clearAll()
246+
clearInstanceCache()
247247
}
248248
return this
249249
}
@@ -378,6 +378,19 @@ class AppDimensDynamic(
378378
cachedScreenDimensions = null
379379
lastConfigurationHash = 0
380380
}
381+
382+
/**
383+
* [EN] Clears the cache for this specific instance.
384+
* Called by the global cache management system.
385+
* [PT] Limpa o cache para esta instância específica.
386+
* Chamado pelo sistema de gerenciamento global de cache.
387+
*/
388+
internal fun clearInstanceCache() {
389+
invalidateCachedData()
390+
// Clear any instance-specific cache entries from the global auto cache
391+
val instanceHash = this.hashCode().toString()
392+
autoCache.clearByPattern(instanceHash)
393+
}
381394

382395
/**
383396
* [EN] Auxiliary version of `resolveQualifierDp` that works with Float instead of Dp.

iOS/Sources/AppDimens/AppDimens.swift

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ public class AppDimens {
9292
*/
9393
public func dynamic(_ initialValue: CGFloat, ignoreMultiWindowAdjustment: Bool? = nil) -> AppDimensDynamic {
9494
let ignore = ignoreMultiWindowAdjustment ?? false
95-
return AppDimensDynamic(initialValue, ignoreMultiWindowAdjustment: ignore)
95+
let instance = AppDimensDynamic(initialValue, ignoreMultiWindowAdjustment: ignore)
96+
AppDimensGlobal.registerDynamicInstance(instance)
97+
return instance
9698
}
9799

98100
/**
@@ -131,13 +133,40 @@ public class AppDimensGlobal {
131133
}
132134
}
133135

136+
/// [EN] Registry of all AppDimensDynamic instances for global cache management.
137+
/// [PT] Registro de todas as instâncias AppDimensDynamic para gerenciamento global de cache.
138+
private static var dynamicInstances = NSHashTable<AppDimensDynamic>.weakObjects()
139+
private static let registryQueue = DispatchQueue(label: "com.appdimens.registry", attributes: .concurrent)
140+
141+
/// [EN] Registers an AppDimensDynamic instance for global cache management.
142+
/// [PT] Registra uma instância AppDimensDynamic para gerenciamento global de cache.
143+
public static func registerDynamicInstance(_ instance: AppDimensDynamic) {
144+
registryQueue.async(flags: .barrier) {
145+
dynamicInstances.add(instance)
146+
}
147+
}
148+
149+
/// [EN] Unregisters an AppDimensDynamic instance from global cache management.
150+
/// [PT] Remove o registro de uma instância AppDimensDynamic do gerenciamento global de cache.
151+
public static func unregisterDynamicInstance(_ instance: AppDimensDynamic) {
152+
registryQueue.async(flags: .barrier) {
153+
dynamicInstances.remove(instance)
154+
}
155+
}
156+
134157
/**
135158
* [EN] Clears all caches from all instances.
136159
* [PT] Limpa todos os caches de todas as instâncias.
137160
*/
138161
public static func clearAllCaches() {
139-
// This would need to be implemented with a registry of instances
140-
// For now, individual instances will clear their own caches
162+
// Clear the global auto cache
163+
AppDimensAutoCache.shared.clearAll()
164+
// Clear individual instance caches
165+
registryQueue.sync {
166+
dynamicInstances.allObjects.forEach { instance in
167+
instance.clearInstanceCache()
168+
}
169+
}
141170
}
142171
}
143172

iOS/Sources/AppDimens/AppDimensAutoCache.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ public class AppDimensAutoCache {
217217
}
218218
}
219219

220+
/**
221+
* [EN] Clears cache entries that match a specific pattern.
222+
* Useful for clearing instance-specific cache entries.
223+
* [PT] Limpa entradas do cache que correspondem a um padrão específico.
224+
* Útil para limpar entradas de cache específicas da instância.
225+
*/
226+
public func clearByPattern(_ pattern: String) {
227+
cacheQueue.async(flags: .barrier) {
228+
let keys = self.cache.allKeys.compactMap { $0 as? String }
229+
let keysToRemove = keys.filter { $0.contains(pattern) }
230+
keysToRemove.forEach { key in
231+
self.cache.removeObject(forKey: key as NSString)
232+
self.dependencyTracker.removeObject(forKey: key as NSString)
233+
}
234+
}
235+
}
236+
220237
/**
221238
* [EN] Gets cache statistics for debugging.
222239
* [PT] Obtém estatísticas do cache para debug.

iOS/Sources/AppDimens/AppDimensDynamic.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public class AppDimensDynamic {
236236
public func cache(enable: Bool = true) -> AppDimensDynamic {
237237
enableCache = enable
238238
if !enable {
239-
autoCache.clearAll()
239+
clearInstanceCache()
240240
}
241241
return self
242242
}
@@ -381,6 +381,16 @@ public class AppDimensDynamic {
381381
lastConfigurationHash = 0
382382
}
383383

384+
/// [EN] Clears the cache for this specific instance.
385+
/// Called by the global cache management system.
386+
/// [PT] Limpa o cache para esta instância específica.
387+
/// Chamado pelo sistema de gerenciamento global de cache.
388+
internal func clearInstanceCache() {
389+
invalidateCachedData()
390+
// Clear any instance-specific cache entries from the global auto cache
391+
AppDimensAutoCache.shared.clearByPattern("_\(self.hashValue)")
392+
}
393+
384394
/**
385395
* [EN] Performs the final dynamic dimension calculation.
386396
* @return The dynamically adjusted value as a CGFloat (not converted to pixels).

0 commit comments

Comments
 (0)