Skip to content

Commit

Permalink
Merge pull request #302 from green-code-initiative/fix/mobile-rules-s…
Browse files Browse the repository at this point in the history
…orting
  • Loading branch information
zippy1978 authored May 20, 2024
2 parents 97e6631 + a43a44a commit bddb4f7
Show file tree
Hide file tree
Showing 24 changed files with 52 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"environment",
"ecocode",
"android",
"ios",
"eco-design"
],
"ecoScore": "0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"environment",
"ecocode",
"android",
"ios",
"eco-design"
],
"ecoScore": "0.4",
Expand Down
3 changes: 2 additions & 1 deletion ecocode-rules-specifications/src/main/rules/EC519/EC519.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"environment",
"ecocode",
"android",
"eco-design"
"eco-design",
"ios"
],
"ecoScore": "1.0",
"defaultSeverity": "Info"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"environment",
"ecocode",
"android",
"ios",
"eco-design"
],
"ecoScore": "1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
Location awareness is one of the most popular features used by apps.
To avoid draining the battery, an iOS device adapt its brightness to the environment .

By default, standard location updates run with the best accuracy level, but unless your app really needs to know the user’s position within a few meters, this level of accuracy isn't needed.
Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. If so, developers typically force the Brightness to 1 with `UIScreen.main.brightness = a float`.

Requesting higher accuracy than you need causes the system to power up additional hardware and waste power for unnecessary precision. You can specify a degree of accuracy by setting the `CLLocationManager#desiredAccuracy` property.

Also, setting the `CLLocationManager#activityType` property will let the system knows what type of location activity your app is performing and helps it determine the most appropriate time to perform location updates. Finally, if your app just needs a quick fix on the user’s location, it’s best to call the `CLLocationManager#requestLocation` method, that will deliver a single location update.

## Compliant Code Example
## Noncompliant Code Example

```swift
let manager = CLLocationManager()
manager.desiredAccuracy = 2
UIScreen.main.brightness = CGFloat(0.3)
```

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
Turning on the torch mode programmatically must absolutely be avoided because the flashlight is one of the most energy-intensive component.
Location awareness is one of the most popular features used by apps.

## Noncompliant Code Example
By default, standard location updates run with the best accuracy level, but unless your app really needs to know the user’s position within a few meters, this level of accuracy isn't needed.

```swift
AVCaptureTorchMode.on
```

or
Requesting higher accuracy than you need causes the system to power up additional hardware and waste power for unnecessary precision. You can specify a degree of accuracy by setting the `CLLocationManager#desiredAccuracy` property.

```swift
setTorchModeOn(level: Float)
```
Also, setting the `CLLocationManager#activityType` property will let the system knows what type of location activity your app is performing and helps it determine the most appropriate time to perform location updates. Finally, if your app just needs a quick fix on the user’s location, it’s best to call the `CLLocationManager#requestLocation` method, that will deliver a single location update.

or
## Compliant Code Example

```swift
torchMode = .on
let manager = CLLocationManager()
manager.desiredAccuracy = 2
```

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"environment",
"ecocode",
"android",
"ios",
"eco-design"
],
"ecoScore": "0.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"title": "Idleness: Keep Screen On",
"title": "Location updates pause disabled",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "20min"
"constantCost": "5min"
},
"tags": [
"idleness",
"environment",
"ecocode",
"android",
"eco-design"
"environment",
"sobriety",
"eco-design",
"ios"
],
"ecoScore": "0.4",
"defaultSeverity": "Major"
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"title": "Idleness: Keep CPU On",
"title": "Motion Sensor Update Rate",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "1h"
"func": "Constant/Issue",
"constantCost": "5min"
},
"tags": [
"idleness",
"sobriety",
"environment",
"ecocode",
"android",
"ios",
"eco-design"
],
"defaultSeverity": "Major"
}
}
17 changes: 0 additions & 17 deletions ecocode-rules-specifications/src/main/rules/EC545/EC545.json

This file was deleted.

17 changes: 0 additions & 17 deletions ecocode-rules-specifications/src/main/rules/EC546/EC546.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Animation Methods in UIKit and SwiftUI",
"title": "Animation Free",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
It's always good that an app has different behavior when device is connected/disconnected to a power station, or has different battery levels. Your app can query the `UIDevice#batteryLevel` and `UIDevice#batteryState` properties at any time to determine the level of charge and the state of the battery.
Animations play a crucial role in creating a dynamic and engaging user interface in iOS applications. Both UIKit and SwiftUI provide powerful tools to create smooth and appealing animations. Developers need to be familiar with various methods and properties to implement animations effectively.

Your app can also register to receive notifications when the battery level or state changes, using `batteryLevelDidChangeNotification` and `batteryStateDidChangeNotification`.
To ensure animations are used effectively without draining the device's battery, it's important to check for unnecessary animations and optimize them. This can be done by reviewing the use of functions like `withAnimation(::)`, the `animation(_:value:)` view modifier, and the binding’s `animation(_:)` method in SwiftUI, as well as checking the use of UIKit's animation methods.

## Compliant Code Example
## Noncompliant Code Example UIKit

```swift
let level = UIDevice.current.batteryLevel
UIView.animate(withDuration:animations:)
UIView.animate(withDuration:animations:completion:)
UIView.animate(withDuration:delay:options:animations:completion:)
UIView.animateKeyframes(withDuration:delay:options:animations:completion:)
UIView.transition(with:duration:options:animations:completion:)
CABasicAnimation
CAKeyframeAnimation
CATransition
```

or
## Noncompliant Code Example SwiftUI

```swift
let state = UIDevice.current.batteryState
```

or

```swift
NotificationCenter.default.addObserver(forName: UIDevice.batteryLevelDidChangeNotification, object: nil, queue: nil) { _ in }
```

or

```swift
NotificationCenter.default.addObserver(forName: UIDevice.batteryStateDidChangeNotification, object: nil, queue: nil) { _ in }
withAnimation
Animation
AnyTransition
.animation(Animation?)
.transition(AnyTransition)
.onAppear(perform:)
.onDisappear(perform:)
```

0 comments on commit bddb4f7

Please sign in to comment.