Skip to main content

123 posts tagged with "software engineering"

View All Tags

Integrating and Using MapKit in SwiftUI iOS Apps

Published: · Last updated: · 4 min read
Appxiom Team
Mobile App Performance Experts

In the ever-evolving landscape of mobile app development, creating engaging and interactive experiences for users is essential. One powerful tool for achieving this is MapKit, Apple's framework for embedding maps and location services into your iOS applications.

In this blog post, we'll explore how to integrate and use MapKit in SwiftUI-based iOS apps to create dynamic and location-aware interfaces.

Prerequisites​

Before we dive into the integration process, make sure you have the following set up:

  • Xcode: Ensure you have the latest version of Xcode installed on your Mac.

  • Development Environment: Basic familiarity with SwiftUI and iOS app development concepts is assumed.

Integrating MapKit into SwiftUI​

To get started, follow these steps to integrate MapKit into your SwiftUI app:

Step 1: Create a New SwiftUI Project​

Open Xcode and create a new SwiftUI project. Give it a meaningful name and select appropriate settings for your project.

Step 2: Import MapKit​

In your project navigator, locate the ContentView.swift file and open it. Import the MapKit framework at the top of the file:

import SwiftUI
import MapKit

Step 3: Create Map View​

Replace the existing content of ContentView with a basic MapView that displays a map. Define a new struct called MapView:

struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
MKMapView()
}

func updateUIView(_ uiView: MKMapView, context: Context) {
// Update the view if needed
}
}

​

Step 4: Use the MapView in ContentView​

Replace the Text("Hello, world!") line in ContentView with your new MapView:

struct ContentView: View {
var body: some View {
MapView()
}
}

​

Step 5: Permissions and Privacy​

MapKit requires access to the user's location. Open the Info.plist file and add the following key to request location access:

<key>NSLocationWhenInUseUsageDescription</key><string>We need your location to display nearby points of interest.</string>

​

Step 6: Displaying User Location​

To display the user's location on the map, you'll need to add a few more lines to the MapView struct:

struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.showsUserLocation = true // Display user's locationreturn mapView
}

func updateUIView(_ uiView: MKMapView, context: Context) {
// Update the view if needed
}
}

​

Customizing the MapView​

Now that you have a basic map view set up, you can start customizing it further to enhance the user experience.

Adding Annotations​

Annotations are points of interest you can add to the map. For instance, to add a pin at a specific coordinate, update the makeUIView function in the MapView struct:

func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()

let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
annotation.title = "San Francisco"
mapView.addAnnotation(annotation)

mapView.showsUserLocation = truereturn mapView
}

​

Changing Map Region​

By default, the map shows a specific region. You can customize this to focus on a particular area using the setRegion method:

func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()

let region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194),
span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
)
mapView.setRegion(region, animated: true)

let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
annotation.title = "San Francisco"
mapView.addAnnotation(annotation)

mapView.showsUserLocation = truereturn mapView
}

​

Responding to Annotations​

You can provide interactivity to the annotations by implementing the MKMapViewDelegate methods. For instance, to show a callout when an annotation is tapped:

func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator

// Rest of the code remains the same// ...
}

class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard annotation is MKPointAnnotation else { return nil }

let identifier = "Annotation"var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
} else {
annotationView?.annotation = annotation
}

return annotationView
}

// Other delegate methods can be implemented here
}

Remember to update the MapView struct to use this coordinator:

func makeCoordinator() -> Coordinator {
Coordinator()
}

​

Conclusion​

In this blog post, we explored the process of integrating and using MapKit in SwiftUI-based iOS apps. We covered the basics of creating a map view, displaying user location, adding annotations, customizing the map's appearance, and adding interactivity to annotations. With MapKit, you have the tools to create engaging and location-aware user experiences in your apps.

Feel free to further explore MapKit's capabilities and experiment with more advanced features to take your app to the next level.

End-to-End Testing of Flutter Apps with flutter_driver

Published: · Last updated: · 4 min read
Appxiom Team
Mobile App Performance Experts

End-to-end (E2E) testing is a critical part of the app development process. It helps ensure that your Flutter app functions correctly from the user's perspective, simulating real-world interactions and scenarios. Flutter provides a powerful tool called flutter_driver for performing E2E testing.

In this blog post, we will delve into the world of flutter_driver and learn how to effectively perform E2E testing for your Flutter app.

1. Introduction to flutter_driver​

flutter_driver is a Flutter package that allows you to write and execute E2E tests on your Flutter app. It provides APIs for interacting with the app and querying the widget tree. The tests are written in Dart and can simulate user interactions, such as tapping buttons, entering text, and verifying UI elements' states.

2. Setting up the Test Environment​

To get started with E2E testing using flutter_driver, follow these steps:

Step 1: Add Dependencies​

In your pubspec.yaml file, add the following dependencies:

dev_dependencies:
flutter_driver:
sdk: flutter
test: any

​

Step 2: Create a Test Driver File​

Create a Dart file (e.g., app_test.dart) in your test directory. This file will define your E2E tests.

Step 3: Start the Test App​

Before running E2E tests, you need to start your app in a special mode that's suitable for testing. Run the following command in your terminal:

flutter drive --target=test_driver/app.dart

​

3. Writing E2E Tests​

Let's create a simple E2E test to demonstrate the capabilities of flutter_driver. Our test scenario will involve tapping a button and verifying that a specific text appears.

import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

void main() {
group('App E2E Test', () {
FlutterDriver driver;

// Connect to the Flutter app before running the tests.
setUpAll(() async {
driver = await FlutterDriver.connect();
});

// Close the connection to the Flutter app after tests are done.
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});

test('Verify Button Tap', () async {
// Find the button by its label.
final buttonFinder = find.byValueKey('myButton');

// Tap the button.
await driver.tap(buttonFinder);

// Find the text by its value.
final textFinder = find.text('Button Tapped');

// Verify that the expected text appears.
expect(await driver.getText(textFinder), 'Button Tapped');
});
});
}

In this example, we've defined a simple test that interacts with a button and verifies the appearance of specific text.

4. Running E2E Tests​

Run your E2E tests using the following command:

flutter drive --target=test_driver/app.dart

This will execute the tests defined in your app_test.dart file.

5. Analyzing Test Results​

After the tests have run, the terminal will display the test results. You'll see information about passed and failed tests, along with any error messages or stack traces.

6. Best Practices for E2E Testing​

  • Isolation: E2E tests should be independent of each other and the testing environment. Avoid relying on the state of previous tests.

  • Use Keys: Assign keys to widgets that you want to interact with in E2E tests. This helps maintain stability even when widget positions change.

  • Clear States: Ensure your app is in a known state before each test. This may involve resetting the app's state or navigating to a specific screen.

  • Regular Maintenance: E2E tests can become fragile if not maintained. Update tests when UI changes occur to prevent false positives/negatives.

  • Limit Flakiness: Use await judiciously to ensure the app has stabilized before performing verifications. This can help reduce test flakiness.

E2E testing with flutter_driver is a powerful way to ensure the quality of your Flutter apps. By writing comprehensive tests that mimic user interactions, you can catch bugs and regressions early in the development process, leading to a more robust and reliable app.

In this blog post, we've covered the basics of setting up flutter_driver, writing tests, running them, and best practices to follow. With this knowledge, you can start incorporating E2E testing into your Flutter development workflow.

Happy testing!

Using MonkeyRunner to Test Android Apps

Published: · Last updated: · 3 min read
Appxiom Team
Mobile App Performance Experts

Mobile app testing is an essential part of the development process to ensure that your app functions correctly across various devices and scenarios. However, manual testing can be time-consuming and error-prone. To streamline the testing process, developers often turn to automation tools. One such tool is MonkeyRunner, a script-based testing framework for Android apps.

In this blog post, we'll explore how to use MonkeyRunner to automate the testing of Android apps.

What is MonkeyRunner?​

MonkeyRunner is a part of the Android SDK that provides a way to write scripts to automate tasks and test Android apps on physical devices or emulators. It simulates user interactions, such as tapping, swiping, and pressing hardware buttons, to mimic real-world usage scenarios.

Setting Up the Environment​

Before we dive into the code, make sure you have the following prerequisites:

  • Android SDK: Install the Android SDK and add the tools and platform-tools directories to your system's PATH.

  • Python: MonkeyRunner scripts are written in Python. Ensure you have Python installed on your system.

Writing the MonkeyRunner Script​

Let's write a basic MonkeyRunner script in Python that interacts with an Android app. This script will launch the app, simulate touch events, and capture screenshots.

Step 1: Create the MonkeyRunner Script​

Create a new Python (.py) file in your project directory and name it app_test.py.

Step 2: Import MonkeyRunner Modules​

In your app_test.py file, import the necessary MonkeyRunner modules:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

​

Step 3: Connect to the Device​

Connect to the Android device or emulator using MonkeyRunner:

device = MonkeyRunner.waitForConnection()

​

Step 4: Launch the App​

Launch the target app on the connected device:

package_name = "com.example.myapp"  # Replace with your app's package name
activity_name = "com.example.myapp.MainActivity" # Replace with the main activity's name

device.startActivity(component=package_name + "/" + activity_name)
MonkeyRunner.sleep(5) # Wait for the app to launch (adjust the time as needed)

​

Step 5: Simulate Touch Events​

Simulate touch events on the app screen:

device.touch(500, 1000, MonkeyDevice.DOWN_AND_UP)  # Replace with desired coordinates
MonkeyRunner.sleep(2) # Wait for 2 seconds

​

Step 6: Capture Screenshots​

Capture screenshots of the app:

screenshot = device.takeSnapshot()
screenshot_path = "path/to/save/screenshot.png"
screenshot.writeToFile(screenshot_path, "png")

​

Step 7: Clean Up​

Close the app and disconnect from the device:

device.shell("am force-stop " + package_name)
device.dispose()

​

Running the MonkeyRunner Script​

To run the MonkeyRunner script, execute the following command in your terminal:

monkeyrunner app_test.py

This will execute the script, simulating touch events on the target app and capturing screenshots.

Conclusion​

Automating Android app testing with MonkeyRunner can save you time and effort while ensuring your app's functionality across various scenarios. By integrating MonkeyRunner scripts, you can harness the power of one of the best automation tools to create a seamless testing process for your Android apps. Remember to customize the scripts according to your app's specific features and requirements.

Happy testing!

Introduction to Using TipKit in SwiftUI Apps

Published: · Last updated: · 4 min read
Don Peter
Cofounder and CTO, Appxiom

TipKit is a new beta framework introduced in iOS 17 that helps developers present tips and instructions to users in their apps. With TipKit, you can easily create and manage tips, set rules for when and how they are displayed.

In this blog post, we will walk you through the steps of getting started with TipKit in your SwiftUI apps.

Steps to using TipKit​

1. Creating a Tip​

To use TipKit, you first need to import the TipKit framework into your project, along with the SwiftUI framework. Then, you can create a new Tip object and specify its content, title, and other properties.

import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {

var title: Text {
Text("Add as Favorite book")
}


var message: Text? {
Text("Click on the fav icon to add the book to favourites")
}

}

For a tip to be valid, it is mandatory to set its title.

2. Adding Rules​

Next step is to add rules that must be met in order for the tip to be displayed. The rules property is an array of Predicate objects, each of which specifies a condition that must be met.

import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {

var title: Text {
Text("Add as Favorite book")
}


var message: Text? {
Text("Click on the fav icon to add the book to favourites")
}

var rules: Predicate<RuleInput...> {
#Rule(Self.$isLoggedIn) { $0 == true }
}
}

In this case, the only rule is that the isLoggedIn property must be equal to true. This means that the tip will only be displayed if the user is logged in.

The #Rule syntax is used to create rules. In this case, the #Rule tag is used to create a rule that is based on the Self.$isLoggedIn property. The Self keyword refers to the current view, and the $isLoggedIn property is a property that gets the current user's login status.

3. Adding Tip to SwiftUI view​

import SwiftUI
import TipKit


struct FavoriteBookTip: Tip {


var title: Text {
Text("Add as Favorite book")
}


var message: Text? {
Text("Click on the fav icon to add the book to favourites")
}

var rules: Predicate<RuleInput...> {
#Rule(Self.$isLoggedIn) { $0 == true }
}
}


@main
struct BookTips: App {
var bookTip = FavoriteBookTip()


var body: some Scene {
WindowGroup {
VStack {
TipView(bookTip, arrowEdge: .bottom)

Image(systemName: "fav_icon")
.imageScale(.large)
Spacer()
}
}
}
}

TipView is a user interface element that represents an inline tip that is provided by TipKit. It displays a tip with an arrow that points to the bottom of the screen. The arrowEdge parameter specifies the edge of the screen where the arrow should point.

In this case, the arrowEdge parameter is set to .bottom, so the arrow will point to the bottom of the screen. TipView takes FavoriteBookTip object as its argument and displays the tip with an arrow that points to the bottom of the screen.

​

4. Configuring Tip using TipsCenter​

Once you have created a tip, you can configure TipsCenter.

TipsCenter is a key part of TipKit that provides several essential features. It allows tips and their associated events to persist between app launches, making it easier to test tips. A default shared instance of TipsCenter is provided, which is what I have added here.

TipsCenter.shared.configure(
displayFrequency: .daily
)

The displayFrequency property specifies how often the tip should be displayed. In this case, the tip will be displayed once per day.

Once you have created your tip and configured TipsCenter, you can display it using the following code when testing.

TipsCenter.shared.showTips([bookTip])

Use-cases of TipKit​

Here are a few examples of how you can use TipKit in your SwiftUI or UIKit based apps:

  • Display a tip when a user opens your app for the first time.

  • Show a tip when a user performs a specific action, such as taking a photo or adding a contact.

  • Give users tips on how to use your app's features.

  • Provide instructions on how to troubleshoot common problems.

​

Conclusion​

TipKit is a powerful new framework that can help you improve the user experience of your SwiftUI or UIKit based apps. By using TipKit, you can easily create and manage tips, set rules for when and how they are displayed, and track their effectiveness.

To make sure your tips are effective, keep them short, instructional, and actionable.

Cold Start, Warm Start and Hot Start in Android Apps

Published: · Last updated: · 5 min read
Appxiom Team
Mobile App Performance Experts

In the world of mobile app development, creating a seamless user experience is paramount. One of the critical factors that contribute to this experience is how quickly an app starts up and becomes responsive. This process is known as app start-up, and it can be categorized into three phases: Cold Start, Warm Start, and Hot Start.

In this blog, we will delve into each of these start-up phases, explore their implications on user experience, and provide insights into how to improve them.

Android App start scenarios

When you launch an Android app, there are three possible scenarios:

  • Cold start: The app is starting from scratch. This is the slowest type of launch, as the system has to create the app's process, load its code and resources, and initialize its components.

  • Warm start: The app's process is already running in the background. In this case, the system only needs to bring the app's activity to the foreground. This is faster than a cold start, but it is still slower than a hot start.

  • Hot start: The app's activity is already in the foreground. In this case, the system does not need to do anything, as the app is already running. This is the fastest type of launch.

The following sections will discuss each of these types of launch in more detail, and provide tips on how to improve them.

​

Cold start

A cold start occurs when the app is launched for the first time after installation or after the system has killed the app process. The following are some of the steps involved in a cold start:

  • The system creates the app's process.

  • The system loads the app's code and resources.

  • The system initializes the app's components.

  • The app's main activity is displayed.

The cold start is the slowest type of launch because it involves loading all of the app's code and resources from scratch. This can take a significant amount of time, especially for large apps.

Ideally the app should complete a cold start in 500 milli seconds or less. That could be challenging sometimes, but make sure the app does the cold start in under 5 seconds. There are a number of things you can do to improve the cold start time of your app:

  • Use lazy loading: Lazy loading means loading resources only when they are needed. This can help to reduce the amount of time it takes to load the app.

  • Use a profiler: A profiler can help you to identify the parts of your app that are taking the most time to load. This can help you to focus your optimization efforts on the most critical areas.

  • Use a caching mechanism: A caching mechanism can store frequently used resources in memory, so that they do not have to be loaded from disk each time the app is launched.

  • Use a custom launcher: A custom launcher can preload the app's resources in the background before the app is launched. This can significantly reduce the cold start time.

Warm start​

A warm start occurs when the app's process is already running in the background. In this case, the system only needs to bring the app's activity to the foreground. This is faster than a cold start, but it is still slower than a hot start.

The following are some of the steps involved in a warm start:

  • The system finds the app's process.

  • The system brings the app's activity to the foreground.

The warm start is faster than a cold start because the app's process is already running. However, the system still needs to bring the app's activity to the foreground, which can take some time.

Ideally the app should complete a warm start in 200 milli seconds or less. In any case, try not to breach the 2 seconds window. There are a number of things you can do to improve the warm start time of your app:

  • Use a profiler: A profiler can help you to identify the parts of your app that are taking the most time to bring to the foreground. This can help you to focus your optimization efforts on the most critical areas.

  • Use a caching mechanism: A caching mechanism can store frequently used activities in memory, so that they do not have to be recreated each time the app is launched.

  • Use a custom launcher: A custom launcher can preload the app's activities in the background before the app is launched. This can significantly reduce the warm start time.

Hot start​

A hot start occurs when the app's activity is already in the foreground. In this case, the system does not need to do anything, as the app is already running. This is the fastest type of launch.

There is not much you can do to improve the hot start time of your app, as it is already running. However, you can take steps to prevent the app from being killed by the system, such as using a foreground service or a wake lock. Ideally the app should complete a hot start in 100 milli seconds or less, or in a worst case scenario, under 1.5 seconds.

​

Conclusion

The cold start, warm start, and hot start are the three different types of app launches in Android. The cold start is the slowest type of launch, while the hot start is the fastest.

There are a number of things you can do to improve the launch time of your app, such as using lazy loading, caching, and a custom launcher.

I hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.

Combine: A Declarative API for Asynchronous Data Processing in Swift

Published: · Last updated: · 4 min read
Don Peter
Cofounder and CTO, Appxiom

Combine is a framework for Swift introduced by Apple in 2019 that provides a declarative API. This makes it ideal for working with asynchronous data, such as network requests and user input. Combine is also a powerful tool for building reactive user interfaces.

In this blog post, we will take a look at the basics of Combine, including publishers, subscribers, and operators. We will also see how Combine can be used to build asynchronous applications and reactive user interfaces.

What is Combine?​

Combine is a reactive programming framework that provides a declarative API for processing values over time. This means that you can describe the desired behaviour of your code without having to worry about the details of how it will be implemented.

Combine is based on the following concepts:

  • Publishers: Publishers emit values over time. They can be anything from network requests to user input.

  • Subscribers: Subscribers receive values from publishers. They can do things like map values, filter values, and perform other operations.

  • Operators: Operators are functions that combine publishers and subscribers. They can be used to perform common tasks, such as combining multiple publishers, filtering values, and retrying failed requests.

Using Combine to Build Asynchronous Applications in Swift​

Combine is ideal for building asynchronous applications. This is because it provides a way to handle asynchronous events in a declarative way. For example, you can use Combine to make a network request and then subscribe to the response. The subscriber can then handle the response, such as mapping it to a model or displaying it in a user interface.

Here is an example of how to use Combine to make a network request:

let publisher = URLSession.shared.dataTaskPublisher(for: URL(string: "https://api.myhost.com")!)

publisher.subscribe(on: RunLoop.main) { data, _, error in
if let data = data {
let json = try JSONDecoder().decode(MyJSONModel.self, from: data)
// Do something with the model
} else if let error = error {
// Handle the error
}
}

This code creates a publisher that emits the response data from the network request. The subscriber then handles the response data, either mapping it to a model or displaying it in a user interface.

Using Combine to Build Reactive User Interfaces​

Combine can also be used to build reactive user interfaces. This is because it provides a way to update user interfaces in response to changes in data. For example, you can use Combine to subscribe to a publisher that emits the current user location. The subscriber can then update the user interface to display the user's location.

Here is an example of how to use Combine to update a user interface with the current user location:

let publisher = locationManager.publisher(for: .location)

publisher.subscribe(on: RunLoop.main) { location in
// Update the user interface with the new location
}

This code creates a publisher that emits the current user location. The subscriber then updates the user interface to display the user's location.

Using custom Combine implementation​

Let us take a look at using PassthroughSubject to implement asynchronous declarative API.

A PassthroughSubject is a type of publisher in Combine that emits any value that is sent to it. It does not have an initial value or a buffer of the most recently-published element. This makes it ideal for use in situations where you need to react to changes in data as they happen.

import Combine

let subject = PassthroughSubject<String, Never>()

subject.sink { string in
print(string)
}

subject.send("Hello, world!")
subject.send("This is a second message")

Here, the first line imports the Combine framework. This is needed to use the PassthroughSubject and sink operators.

The second line creates a PassthroughSubject publisher. This publisher will emit any string that is sent to it.

The third line attaches a sink subscriber to the PassthroughSubject publisher. The sink subscriber will print each string that is emitted by the publisher to the console.

The fourth and fifth lines send two strings to the PassthroughSubject publisher. These strings will be printed to the console by the sink subscriber.

Conclusion​

Combine is a framework that provides a declarative API for processing values over time. This makes it ideal for working with asynchronous data and building reactive user interfaces. If you are new to Combine, I encourage you to check out the official documentation and tutorials.

I hope this blog post has given you a basic understanding of Combine. If you have any questions, please feel free to leave a comment below.

A Guide on Flutter Animations

Published: · Last updated: · 11 min read
Appxiom Team
Mobile App Performance Experts

Animations play a vital role in creating engaging and visually appealing user interfaces in mobile applications. Flutter, a popular open-source UI framework by Google, offers a robust set of tools for creating smooth and expressive animations.

In this comprehensive guide, we'll explore the world of Flutter animations, from the basics to more advanced techniques, accompanied by code samples to help you get started.

1. Introduction to Flutter Animations​

Why Animations Matter​

Animations provide a more dynamic and engaging user experience, guiding users through interface changes and interactions. They help convey important information, enhance the overall aesthetic of the app, and make interactions more intuitive.

Types of Animations in Flutter​

Flutter offers several animation types:

  • Implicit Animations: These animations are built into existing widgets and can be triggered using widget properties, like AnimatedContainer or AnimatedOpacity.

  • Tween Animations: These animations interpolate between two values over a specified duration using Tween objects.

  • Physics-Based Animations: These animations simulate real-world physics, like springs or flings, to create natural-looking motion.

  • Custom Animations: For more complex scenarios, you can create your own custom animations using CustomPainter and AnimationController.

In this guide, we'll cover examples from each category to give you a well-rounded understanding of Flutter animations.

2. Basic Animations​

Animated Container​

The AnimatedContainer widget is a straightforward way to animate changes to a container's properties, such as its size, color, and alignment.

class BasicAnimatedContainer extends StatefulWidget {
@override
_BasicAnimatedContainerState createState() => _BasicAnimatedContainerState();
}

class _BasicAnimatedContainerState extends State<BasicAnimatedContainer> {
double _width = 100.0;
double _height = 100.0;
Color _color = Colors.blue;

void _animateContainer() {
setState(() {
_width = 200.0;
_height = 200.0;
_color = Colors.red;
});
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _animateContainer,
child: Center(
child: AnimatedContainer(
duration: Duration(seconds: 1),
width: _width,
height: _height,
color: _color,
),
),
);
}
}

Animated Opacity​

The AnimatedOpacity widget allows you to animate the opacity of a widget, making it appear or disappear smoothly.

class BasicAnimatedOpacity extends StatefulWidget {
@override
_BasicAnimatedOpacityState createState() => _BasicAnimatedOpacityState();
}

class _BasicAnimatedOpacityState extends State<BasicAnimatedOpacity> {
bool _visible = true;

void _toggleVisibility() {
setState(() {
_visible = !_visible;
});
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedOpacity(
duration: Duration(seconds: 1),
opacity: _visible ? 1.0 : 0.0,
child: FlutterLogo(size: 150.0),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleVisibility,
child: Text(_visible ? "Hide Logo" : "Show Logo"),
),
],
);
}
}

3. Tween Animations​

Animating Widgets with Tween​

Tween animations interpolate between two values over a specified duration. Here's an example of animating the position of a widget using a Tween:

class TweenAnimation extends StatefulWidget {
@override
_TweenAnimationState createState() => _TweenAnimationState();
}

class _TweenAnimationState extends State<TweenAnimation> {
double _endValue = 200.0;

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
setState(() {
_endValue = _endValue == 200.0 ? 100.0 : 200.0;
});
},
child: Center(
child: TweenAnimationBuilder(
tween: Tween<double>(begin: 100.0, end: _endValue),
duration: Duration(seconds: 1),
builder: (BuildContext context, double value, Widget? child) {
return Container(
width: value,
height: value,
color: Colors.blue,
);
},
),
),
);
}
}

​

Tween Animation Builder​

The TweenAnimationBuilder widget is a versatile tool for building animations with Tweens. It allows you to define the tween, duration, and a builder function to create the animated widget.

4. Physics-Based Animations​

Using AnimatedBuilder with Curves​

Curves define the rate of change in an animation, affecting its acceleration and deceleration. The CurvedAnimation class allows you to apply curves to your animations. Here's an example of using AnimatedBuilder with a curve:

class CurvedAnimationDemo extends StatefulWidget {
@override
_CurvedAnimationDemoState createState() => _CurvedAnimationDemoState();
}

class _CurvedAnimationDemoState extends State<CurvedAnimationDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);
final Animation curveAnimation = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);
_animation = Tween<double>(begin: 0, end: 200).animate(curveAnimation);
_controller.forward();
}

@override
Widget build(BuildContext context) {
return Center(
child: AnimatedBuilder(
animation: _animation,
builder: (BuildContext context, Widget? child) {
return Container(
width: _animation.value,
height: 100,
color: Colors.blue,
);
},
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

​

Creating a Spring Animation​

Spring animations simulate the behavior of a spring, creating a bounce-like effect. Flutter provides the SpringSimulation class for this purpose. Here's an example of creating a spring animation:

class SpringAnimationDemo extends StatefulWidget {
@override
_SpringAnimationDemoState createState() => _SpringAnimationDemoState();
}

class _SpringAnimationDemoState extends State<SpringAnimationDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<Offset> _animation;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);

final SpringDescription spring = SpringDescription(
mass: 1,
stiffness: 500,
damping: 20,
);

final SpringSimulation springSimulation = SpringSimulation(
spring,
_controller.value,
1, // The target value
0, // The velocity
);

_animation = Tween<Offset>(begin: Offset.zero, end: Offset(2, 0))
.animate(_controller);

_controller.animateWith(springSimulation);
}

@override
Widget build(BuildContext context) {
return Center(
child: SlideTransition(
position: _animation,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

5. Complex Animations​

Staggered Animations​

Staggered animations involve animating multiple widgets with different delays, creating an appealing sequence. The StaggeredAnimation class manages this behavior. Here's an example:

class StaggeredAnimationDemo extends StatefulWidget {
@override
_StaggeredAnimationDemoState createState() => _StaggeredAnimationDemoState();
}

class _StaggeredAnimationDemoState extends State<StaggeredAnimationDemo>
with TickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);

final StaggeredAnimation staggeredAnimation = StaggeredAnimation(
controller: _controller,
itemCount: 3,
);

_animation = Tween<double>(begin: 0, end: 200).animate(staggeredAnimation);

_controller.forward();
}

@override
Widget build(BuildContext context) {
return Center(
child: ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return FadeTransition(
opacity: _animation,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: _animation.value,
height: 100,
color: Colors.blue,
),
),
);
},
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

class StaggeredAnimation extends Animatable<double> {
final AnimationController controller;
final int itemCount;

StaggeredAnimation({
required this.controller,
required this.itemCount,
}) : super();

@override
double transform(double t) {
int itemCount = this.itemCount;
double fraction = 1.0 / itemCount;
return (t * itemCount).clamp(0.0, itemCount - 1).toDouble() * fraction;
}
}

​

Hero Animations​

Hero animations are used to smoothly transition a widget between two screens. They provide a seamless experience as the widget scales and moves from one screen to another. Here's an example:

class HeroAnimationDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Hero Animation'),
),
body: Center(
child: Hero(
tag: 'hero-tag',
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
),
);
},
),
);
},
child: Scaffold(
appBar: AppBar(
title: const Text('Hero Animation'),
),
body: Center(
child: Hero(
tag: 'hero-tag',
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
),
),
);
}
}

​

6. Implicit Animations​

Animated CrossFade​

The AnimatedCrossFade widget smoothly transitions between two children while crossfading between them. It's useful for scenarios like toggling between two pieces of content.

class CrossFadeDemo extends StatefulWidget {
@override
_CrossFadeDemoState createState() => _CrossFadeDemoState();
}

class _CrossFadeDemoState extends State<CrossFadeDemo> {
bool _showFirst = true;

void _toggle() {
setState(() {
_showFirst = !_showFirst;
});
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedCrossFade(
firstChild: FlutterLogo(size: 150),
secondChild: Container(color: Colors.blue, width: 150, height: 150),
crossFadeState:
_showFirst ? CrossFadeState.showFirst : CrossFadeState.showSecond,
duration: Duration(seconds: 1),
),
ElevatedButton(
onPressed: _toggle,
child: Text(_showFirst ? 'Show Second' : 'Show First'),
),
],
);
}
}

​

Animated Switcher​

The AnimatedSwitcher widget allows smooth transitions between different children based on a key. It's commonly used for transitions like swapping widgets.

class SwitcherDemo extends StatefulWidget {
@override
_SwitcherDemoState createState() => _SwitcherDemoState();
}

class _SwitcherDemoState extends State<SwitcherDemo> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedSwitcher(
duration: Duration(seconds: 1),
child: Text(
'$_counter',
key: ValueKey<int>(_counter),
style: TextStyle(fontSize: 48),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _incrementCounter,
child: Text('Increment'),
),
],
);
}
}

7. Custom Animations​

CustomPainter and AnimationController​

The combination of CustomPainter and AnimationController allows you to create complex animations and draw custom shapes. Here's an example of a rotating custom animation using CustomPainter:

class CustomPainterAnimation extends StatefulWidget {
@override
_CustomPainterAnimationState createState() => _CustomPainterAnimationState();
}

class _CustomPainterAnimationState extends State<CustomPainterAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _controller;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
)..repeat();
}

@override
Widget build(BuildContext context) {
return Center(
child: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget? child) {
return CustomPaint(
painter: RotatingPainter(_controller.value),
child: Container(width: 150, height: 150),
);
},
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

class RotatingPainter extends CustomPainter {
final double rotation;

RotatingPainter(this.rotation);

@override
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height / 2);
canvas.rotate(rotation * 2 * pi);
final rect = Rect.fromCenter(
center: Offset(0, 0),
width: size.width * 0.8,
height: size.height * 0.8,
);
final paint = Paint()..color = Colors.blue;
canvas.drawRect(rect, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

​

Creating a Flip Card Animation​

Using a combination of Transform, GestureDetector, and AnimationController, you can create a flip card animation.

class FlipCardDemo extends StatefulWidget {
@override
_FlipCardDemoState createState() => _FlipCardDemoState();
}

class _FlipCardDemoState extends State<FlipCardDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
bool _isFront = true;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(milliseconds: 500),
);
}

void _flipCard() {
if (_isFront) {
_controller.forward();
} else {
_controller.reverse();
}
_isFront = !_isFront;
}

@override
Widget build(BuildContext context) {
return Center(
child: GestureDetector(
onTap: _flipCard,
child: AnimatedBuilder(
animation: _controller,
builder: (BuildContext context, Widget? child) {
final double rotationValue = _controller.value;
final double rotationAngle = _isFront ? rotationValue : (1 - rotationValue);
final frontRotation = Matrix4.identity()
..setEntry(3, 2, 0.001)
..rotateY(pi * rotationAngle);
final backRotation = Matrix4.identity()
..setEntry(3, 2, 0.001)
..rotateY(pi * (rotationAngle - 1));
return Stack(
children: [
_buildCard(frontRotation, 'Front', Colors.blue),
_buildCard(backRotation, 'Back', Colors.red),
],
);
},
),
),
);
}

Widget _buildCard(Matrix4 transform, String text, Color color) {
return Center(
child: Transform(
transform: transform,
alignment: Alignment.center,
child: Container(
width: 200,
height: 300,
color: color,
alignment: Alignment.center,
child: Text(
text,
style: TextStyle(fontSize: 24, color: Colors.white),
),
),
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

​

8. Performance Optimization​

Using the AnimationController's vsync​

When creating an AnimationController, it's essential to provide a vsync parameter. This parameter helps in syncing the animation frame rate with the device's refresh rate, enhancing performance and reducing unnecessary updates.

class VsyncAnimation extends StatefulWidget {
@override
_VsyncAnimationState createState() => _VsyncAnimationState();
}

class _VsyncAnimationState extends State<VsyncAnimation>
with SingleTickerProviderStateMixin {
late AnimationController _controller;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this, // Pass `this` as the vsync parameter
duration: Duration(seconds: 2),
);
}

// ...
}

​

Avoiding Unnecessary Rebuilds​

To avoid unnecessary rebuilds of widgets, you can use AnimatedBuilder or ValueListenableBuilder. These widgets rebuild only when the animation value changes, improving overall performance.

class AvoidRebuildsDemo extends StatefulWidget {
@override
_AvoidRebuildsDemoState createState() => _AvoidRebuildsDemoState();
}

class _AvoidRebuildsDemoState extends State<AvoidRebuildsDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 2),
);

_animation = Tween<double>(begin: 0, end: 1).animate(_controller);
}

@override
Widget build(BuildContext context) {
return Center(
child: ValueListenableBuilder(
valueListenable: _animation,
builder: (BuildContext context, double value, Widget? child) {
return Transform.scale(
scale: value,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
);
},
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

​

9. Chaining and Sequencing Animations​

Using Future.delayed​

You can chain animations by using Future.delayed. This creates a delayed effect, allowing one animation to start after the previous one completes.

class DelayedAnimationDemo extends StatefulWidget {
@override
_DelayedAnimationDemoState createState() => _DelayedAnimationDemoState();
}

class _DelayedAnimationDemoState extends State<DelayedAnimationDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation1;
late Animation<double> _animation2;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
);

_animation1 = Tween<double>(begin: 0, end: 1).animate(_controller);

_animation2 = Tween<double>(begin: 0, end: 1).animate(
CurvedAnimation(
parent: _controller,
curve: Interval(0.5, 1.0), // Starts after the first animation
),
);

_controller.forward();
}

@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ScaleTransition(
scale: _animation1,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
SizedBox(height: 20),
ScaleTransition(
scale: _animation2,
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
),
],
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

​

Using AnimationController's addListener​

The addListener method of AnimationController can be used to sequence animations, triggering the second animation when the first animation completes.

class SequenceAnimationDemo extends StatefulWidget {
@override
_SequenceAnimationDemoState createState() => _SequenceAnimationDemoState();
}

class _SequenceAnimationDemoState extends State<SequenceAnimationDemo>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation1;
late Animation<double> _animation2;

@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: Duration(seconds: 1),
);

_animation1 = Tween<double>(begin: 0, end: 1).animate(_controller)
..addListener(() {
if (_animation1.isCompleted) {
_controller.reset(); // Reset the controller to restart
_controller.forward(); // Start the second animation
}
});

_animation2 = Tween<double>(begin: 0, end: 1).animate(_controller);

_controller.forward();
}

@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ScaleTransition(
scale: _animation1,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
),
SizedBox(height: 20),
ScaleTransition(
scale: _animation2,
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
),
],
),
);
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}
}

​

10. Conclusion and Further Learning​

Flutter's animation capabilities allow you to create stunning, dynamic user interfaces that engage users and enhance their experience. This guide covered a wide range of animation techniques, from basic animations and tween animations to physics-based simulations and complex custom animations.

As you continue your journey with Flutter animations, consider exploring more advanced topics like Flare animations for vector graphics, using Rive for more complex animations, and experimenting with implicit animations for seamless UI changes.

Remember, mastering Flutter animations takes practice and experimentation. With dedication and creativity, you can bring your app's UI to life and create memorable user experiences that leave a lasting impression.

Happy animating! 🚀

Note: The code samples provided in this blog post are simplified for illustrative purposes. Actual implementation may require additional considerations and optimizations.

Introduction to State Management in SwiftUI: @State, @StateObject and @ObservedObject

Published: · Last updated: · 6 min read
Don Peter
Cofounder and CTO, Appxiom

SwiftUI is a powerful framework for building user interfaces for Apple devices. However, one of the challenges of using SwiftUI is managing state. State is the data that changes over time in your app, such as the current user's location or the contents of a shopping cart.

Using @State property wrapper in SwiftUI​

There are a few different ways to manage state in SwiftUI. The simplest way is to use the @State property wrapper. The @State property wrapper allows you to store a value that can be changed within a view. When the value changes, SwiftUI will automatically update the view.

For example, let's say we have a view that displays a counter. We can use the @State property wrapper to store the current value of the counter. When the user taps a button, we can increment the counter value and then update the view.

struct CounterView: View {
@State private var counter = 0

var body: some View {
Button("Increment") {
counter += 1
}
Text("\(counter)")
}
}

The @State property wrapper is a great way to manage simple state in SwiftUI.

However, some of the limitations of using @State are,

  • @State properties can only be used in structs. This means that you can't use @State properties in classes or enums.

  • @State properties can't be used to store complex objects. This means that you can't store objects that contain functions, closures, or other complex types in a @State property.

  • @State properties can't be changed from outside the view. This means that you can't change the value of a @State property from another view or from code that isn't part of the view hierarchy.

Using @StateObject and @ObservedObject​

The code below shows how to use the @StateObject and @ObservedObject property wrappers to manage state in SwiftUI.

The GameProgress class is an ObservableObject class. This means that it conforms to the ObservableObject protocol, which allows it to be observed by other views. The points property in the GameProgress class is marked with the @Published property wrapper.

This means that any changes to the value of the points property will be automatically published to any views that are observing it.

The ButtonView struct is a view that observes the progress property. The progress property is marked with the @ObservedObject property wrapper, which tells SwiftUI that the view should observe the value of the property and update itself whenever the value changes. The ButtonView struct has a button that increments the value of the points property. When the button is tapped, the points property is incremented and the InnerView struct is updated to reflect the change.

The ContentView struct is the main view of the app. It has a progress property that is an instance of the GameProgress class. The progress property is marked with the @StateObject property wrapper, which tells SwiftUI that the property is owned by the ContentView view. The ContentView struct has a VStack that contains two views: a Text view that displays the current points, and an ButtonView view that allows the user to increment the points.

class GameProgress: ObservableObject {
@Published var points = 0
}

struct ButtonView: View {
@ObservedObject var progress: GameProgress

var body: some View {
Button("Increase Points") {
progress.points += 1
}
}
}

struct ContentView: View {
@StateObject var progress = GameProgress()

var body: some View {
VStack {
Text("Your points are \(progress.points)")
ButtonView(progress: progress)
}
}
}

Here are some key takeaways from this code:

  • The @StateObject property wrapper is used to create an object that can be observed by other views.

  • The @Published property wrapper is used to mark a property in an ObservableObject class as being observable.

  • The @ObservedObject property wrapper is used to observe a property in an ObservableObject class from another view.

  • When the value of a property that is marked with the @Published property wrapper changes, the views that are observing the property will be updated automatically.

This is a simple example of how to use the @StateObject and @ObservedObject property wrappers to manage state in SwiftUI. In a more complex app, the GameProgress class would likely be responsible for managing more than just the points. It might also be responsible for fetching data from a server or interacting with other parts of the app.

Using @EnvironmentObject​

final class MyTheme: ObservableObject {
@Published var mainColor: Color = .purple
}

struct ThemeApp: App {
@StateObject var myTheme = MyTheme()

var body: some Scene {
WindowGroup {
ThemesListView()
.environmentObject(myTheme) // Make the theme available through the environment.
}
}
}

And the ThemesListView struct will be,

struct ThemesListView: View {

@EnvironmentObject var myTheme: Theme

Text("Text Title")
.backgroundColor(myTheme.mainColor)

}

The code is for a SwiftUI app that uses an environment object to share a theme between views. The theme object is a MyTheme class that conforms to the ObservableObject protocol. This means that the theme object can be observed by other views.

The ThemeApp struct is the main entry point for the app. It creates a myTheme property that is an instance of the MyTheme class. The myTheme property is marked with the @StateObject property wrapper, which means that it is owned by the ThemeApp struct.

The ThemeApp struct also has a body property that returns a WindowGroup. The WindowGroup contains an ThemesListView view. The ThemesListView view is a view that displays a list of themes.

The ThemesListView view uses the environmentObject modifier to access the myTheme property. This modifier tells SwiftUI to look for the myTheme property in the environment of the ThemesListView view. If the myTheme property is not found in the environment, then a new instance of the MyTheme class will be created.

The ThemesListView view uses the myTheme.mainColor property to set the color of the list items. This means that the color of the list items will be updated automatically whenever the mainColor property of the myTheme object changes.

Using an environment object is a simple and elegant solution. We only have to create the theme object once, and it will be available to all child views automatically. This makes our code easier to read and maintain.

Conclusion​

In this blog post, we have explored three different ways to manage state in SwiftUI. We have seen how to use the @State property wrapper to manage simple state, how to use the @StateObject and @ObservedObject property wrappers to manage complex state, and how to use environment objects to share state between views.

The best approach to use will depend on the specific needs of your app.

Accessibility Guidelines for Android Apps

Published: · Last updated: · 3 min read
Appxiom Team
Mobile App Performance Experts

Accessibility is a crucial aspect of app development as it ensures that all users, including those with disabilities, can fully access and interact with your Android app. Jetpack Compose, the modern UI toolkit for building Android apps, provides powerful tools and features to make your app more accessible and inclusive.

In this blog, we'll explore some accessibility guidelines and demonstrate how to implement them using Jetpack Compose.

1. Provide Content Descriptions for Images​

For users who rely on screen readers, providing content descriptions for images is essential. It allows them to understand the context of the image. In Jetpack Compose, you can use the Image composable and include a contentDescription parameter.

import androidx.compose.foundation.Image
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.painterResource

@Composable
fun AccessibleImage() {
Image(
painter = painterResource(id = R.drawable.my_image),
contentDescription = "A beautiful sunset at the beach"
)
}

2. Add Accessibility Labels to Interactive Elements​

For interactive elements like buttons and clickable components, adding accessibility labels is crucial. These labels are read aloud by screen readers to inform users about the purpose of the element. You can use the contentDescription parameter for buttons and other interactive components as well.

import androidx.compose.material.Button
import androidx.compose.runtime.Composable

@Composable
fun AccessibleButton() {
Button(
onClick = { /* Handle button click */ },
contentDescription = "Click to submit the form"
) {
// Button content
}
}

​

3. Ensure Sufficient Contrast​

Maintaining sufficient color contrast is essential for users with low vision or color blindness. Jetpack Compose Color object has luminance funcction to check the contrast ratio between text and background colors.

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance

fun isContrastRatioSufficient(textColor: Color, backgroundColor: Color): Boolean {
val luminanceText = textColor.luminance()
val luminanceBackground = backgroundColor.luminance()
val contrastRatio = (luminanceText + 0.05) / (luminanceBackground + 0.05)
return contrastRatio >= 4.5
}

This function demonstrates how to validate the contrast ratio and adjust colors accordingly to meet the accessibility standards.

4. Manage Focus and Navigation​

Properly managing focus and navigation is essential for users who rely on keyboards or other input methods. In Jetpack Compose, you can use the clickable modifier and the semantics modifier to manage focus and navigation.

import androidx.compose.foundation.clickable
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun AccessibleClickableItem() {
Box(
modifier = Modifier
.clickable { /* Handle click */ }
.semantics { /* Provide accessibility information */ }
) {
// Item content
}
}

​

5. Provide Text Scale and Font Size Options​

Some users may require larger text or different font sizes to read the content comfortably. Jetpack Compose makes it easy to implement text scaling and provide font size options.

import androidx.compose.material.LocalTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp

@Composable
fun ScalableText(
text: String,
textSize: TextUnit = 16.sp
) {
val density = LocalDensity.current.density
val scaledTextSize = with(density) { textSize.toDp() }
LocalTextStyle.current = TextStyle(fontSize = scaledTextSize)

// Render the text
}

​

6. Test Android App with Accessibility Services​

Testing your app's accessibility features is crucial to ensure they work as intended. You can use built-in Android accessibility tools like TalkBack to test your app's compatibility. Turn on TalkBack or other accessibility services on your device and navigate through your app to see how it interacts with these services.

Conclusion​

By following these accessibility guidelines and using Jetpack Compose's built-in accessibility features, you can create Android apps that are more inclusive and provide a better user experience for all users, regardless of their abilities.

Remember, this blog provides only an overview of accessibility guidelines for Android apps using Jetpack Compose. For more detailed guidelines and specifications, refer to the official Android Accessibility documentation.

Ensuring accessibility in your app not only improves user satisfaction but also demonstrates your commitment to creating an inclusive digital environment. So, let's make our apps accessible and embrace the diversity of our users!

Happy coding!

Advantages of Structs in Swift and How to Use Them Effectively

Published: · Last updated: · 4 min read
Appxiom Team
Mobile App Performance Experts

In Swift, structs are an essential feature of the language that allows developers to create custom data types to encapsulate related pieces of data and functionality. Unlike classes, structs are value types, meaning they are copied when passed around, which has numerous advantages.

In this blog, we'll explore the benefits of using structs in Swift and provide insights into how to use them effectively in your code.

Advantages of Using Structs​

1. Value Semantics​

One of the most significant advantages of using structs is their value semantics. When you create an instance of a struct and assign it to another variable or pass it as a parameter to a function, a complete copy of the struct is made. This behavior eliminates issues related to shared mutable state, making code more predictable and less prone to bugs.

struct Point {
var x: Int
var y: Int
}

var point1 = Point(x: 10, y: 20)
var point2 = point1 // Creates a copy of the struct
point2.x = 100 // Only modifies point2, leaving point1 unchanged

2. Performance and Memory Efficiency​

Since structs are copied by value, they are stored directly where they are used, usually on the stack. This allocation strategy results in better memory management and performance compared to reference types (classes) that use heap storage. Structs are particularly useful for small, lightweight data types, which are prevalent in many applications.

3. Thread Safety​

Due to their immutability and value semantics, structs are inherently thread-safe. Since they cannot be mutated once created, they eliminate the need for synchronization mechanisms like locks or serial dispatch queues in concurrent programming scenarios.

4. Swift Standard Library Foundation​

Many essential Swift types, such as Int, Double, Bool, String, Array, and Dictionary, are implemented as structs in the Swift Standard Library. Leveraging structs enables you to build on top of these foundational types effectively.

5. Copy-on-Write Optimization​

Swift's copy-on-write optimization further enhances the performance of structs. When a copy of a struct is made, the actual data is not duplicated immediately. Instead, both copies share the same data. The data is only duplicated when one of the copies is modified, ensuring efficient memory management.

Effective Usage of Structs​

​

1. Model Data​

Structs are ideal for modeling data, especially when dealing with simple objects with no need for inheritance or identity. For example, consider using structs to represent geometric shapes, user profiles, or configuration settings.

struct Circle {
var radius: Double
var center: Point
}

struct UserProfile {
var username: String
var email: String
var age: Int
}

​

2. Immutability​

Consider making structs immutable whenever possible. Immutable structs prevent accidental modifications, leading to more robust and predictable code.

struct ImmutablePoint {
let x: Int
let y: Int
}

​

3. Small-sized Data Structures​

As mentioned earlier, structs are great for small-sized data structures. For larger and more complex data structures, classes might be a more appropriate choice.

4. Use Extensions for Additional Functionality​

To keep the primary purpose of a struct focused and maintain separation of concerns, use extensions to add extra functionality.

struct Point {
var x: Int
var y: Int
}

extension Point {
func distance(to otherPoint: Point) -> Double {
let xDist = Double(x - otherPoint.x)
let yDist = Double(y - otherPoint.y)
return (xDist * xDist + yDist * yDist).squareRoot()
}
}

​

5. Use Mutating Methods Sparingly​

If you need to modify a struct, you must declare the method as mutating. However, try to limit the number of mutating methods and prefer immutability whenever possible.

Conclusion​

Swift structs offer numerous advantages, including value semantics, performance, thread safety, and easy integration with the Swift Standard Library. By using structs effectively, you can write more robust, predictable, and efficient code. Remember to choose structs when modeling small-sized data and prefer immutability for improved code safety. Swift's powerful language features, combined with the advantages of structs, make it a great choice for developing applications across various domains.

Remember to practice and experiment with structs in your code to gain a deeper understanding of their advantages and to leverage their capabilities effectively.

Happy coding!

Accessibility Guidelines for Flutter Mobile Apps

Published: · Last updated: · 3 min read
Appxiom Team
Mobile App Performance Experts

In today's digital age, mobile apps play a significant role in our lives. However, many app developers often overlook the importance of accessibility. Building mobile apps with accessibility in mind ensures that everyone, including individuals with disabilities, can access and enjoy your app without barriers. Flutter, a popular cross-platform framework, offers several features and tools to create accessible mobile apps.

In this blog, we will explore some essential accessibility guidelines for developing mobile apps with Flutter and provide example code to demonstrate each guideline.

1. Provide Meaningful Semantics​

To make your app more accessible, it's crucial to use proper semantics for widgets and elements. Semantics help screen readers understand the purpose and function of each UI component.

Example: Suppose you have a custom button in your app. Use the Semantics widget to provide meaningful semantics.

Semantics(
label: 'Submit Button',
child: ElevatedButton(
onPressed: () {
// Button click logic
},
child: Text('Submit'),
),
)

2. Use Descriptive Alt Text for Images​

Images are a vital part of mobile apps, but they must be accessible to users who cannot see them. Providing descriptive alternative text (alt text) for images is essential for screen readers to convey the image's content.

Example: When using an image in your app, add an Image widget with the semanticLabel parameter:

Image(
image: AssetImage('assets/image.png'),
semanticLabel: 'A beautiful sunset at the beach',
)

3. Ensure Sufficient Contrast​

Maintaining proper contrast between text and background is crucial for users with visual impairments. Flutter provides a ThemeData class that allows you to define consistent colors throughout your app and adhere to accessibility standards.

Example: Define a custom theme with sufficient contrast:

ThemeData(
brightness: Brightness.light,
primaryColor: Colors.blue,
accentColor: Colors.orange,
textTheme: TextTheme(
bodyText1: TextStyle(color: Colors.black87),
bodyText2: TextStyle(color: Colors.black54),
),
)

4. Enable built-in Screen Reader Support in Flutter​

Flutter has built-in support for screen readers like TalkBack (Android) and VoiceOver (iOS). To enable screen reader support, ensure that your UI components are accessible and convey the relevant information to the users.

Example: For adding accessibility support to a text widget:

Text(
'Hello, World!',
semanticsLabel: 'Greeting',
)

5. Manage Focus and Navigation​

Proper focus management is crucial for users who rely on keyboard navigation or screen readers. Ensure that focus is visible and logical when navigating through your app's elements.

Example: Implement a FocusNode and Focus widget to manage focus:

class FocusDemo extends StatefulWidget {
@override
_FocusDemoState createState() => _FocusDemoState();
}

class _FocusDemoState extends State<FocusDemo> {
final FocusNode _focusNode = FocusNode();

@override
Widget build(BuildContext context) {
return Focus(
focusNode: _focusNode,
child: ElevatedButton(
onPressed: () {
// Button click logic
},
child: Text('Click Me'),
),
);
}
}

6. Handle Dynamic Text Sizes​

Some users may rely on larger text sizes for better readability. Flutter supports dynamic text sizes that adapt to the user's accessibility settings.

Example: Use the MediaQuery to access the user's text scale factor:

dartCopy code
Text(
'Dynamic Text',
style: TextStyle(fontSize: MediaQuery.of(context).textScaleFactor * 20),
)

Conclusion​

Building accessible mobile apps with Flutter is not only a legal and ethical obligation but also a step towards creating a more inclusive digital environment. By following the guidelines mentioned in this blog, you can ensure that your app is accessible to a broader audience, including individuals with disabilities.

Remember that accessibility is an ongoing process, and continuous user feedback and testing are essential to refine your app's accessibility. Let's strive to make technology more inclusive and accessible for everyone!

Quick Start Guide on Hilt and Dependency Injection in Kotlin Android Apps

Published: · Last updated: · 5 min read
Appxiom Team
Mobile App Performance Experts

Dependency injection is an essential architectural pattern in Android app development that allows us to manage and provide dependencies to classes or components in a flexible and scalable way. Traditionally, setting up dependency injection in Android apps involved writing a significant amount of boilerplate code. However, with the introduction of Hilt, a dependency injection library from Google built on top of Dagger, this process has become much more streamlined and intuitive.

In this blog, we will explore the step-by-step process of integrating Hilt into a Kotlin Android app and leverage its power to manage dependencies effortlessly.

What is Hilt?​

Hilt is a dependency injection library for Android, developed by Google. It is designed to simplify the implementation of dependency injection in Android apps by reducing boilerplate code and providing a set of predefined components and annotations.

Hilt is built on top of Dagger, which is a popular dependency injection framework for Java and Android. By using Hilt, developers can focus more on writing clean and modular code, and Hilt takes care of generating the necessary Dagger code under the hood.

Prerequisites​

Before we proceed, make sure you have the following set up in your development environment:

  • Android Studio with the latest Kotlin plugin.

  • A Kotlin-based Android project.

Integrating Hilt with Kotlin Android app​

Step 1: Add Hilt Dependencies​

The first step is to include the necessary Hilt dependencies in your project.

Open your app's build.gradle file and add the following lines:

dependencies {
implementation "com.google.dagger:hilt-android:2.41"
kapt "com.google.dagger:hilt-android-compiler:2.41"
}

Hilt requires two dependencies - hilt-android for the runtime library and hilt-android-compiler for annotation processing during build time.

Step 2: Enable Hilt in the Application Class​

Next, we need to enable Hilt in the Application class of our app. If you don't already have an Application class, create one by extending the Application class. Then, annotate the Application class with @HiltAndroidApp, which informs Hilt that this class will be the entry point for dependency injection in our app:

@HiltAndroidApp
class MyApp : Application() {
// ...
}

The @HiltAndroidApp annotation generates the necessary Dagger components and modules under the hood, and it also initializes Hilt in the Application class.

Step 3: Setting up Hilt Modules​

Hilt uses modules to provide dependencies. A module is a class annotated with @Module, and it contains methods annotated with @Provides. These methods define how to create and provide instances of different classes. Let's create an example module that provides a singleton instance of a network service:

@Module
@InstallIn(ApplicationComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideNetworkService(): NetworkService {
return NetworkService()
}
}

In this example, we define a method provideNetworkService() annotated with @Provides that returns a NetworkService instance. The @Singleton annotation ensures that the same instance of NetworkService is reused whenever it is requested.

Step 4: Injecting Dependencies​

After setting up the module, we can now use the @Inject annotation to request dependencies in our Android components, such as activities, fragments, or view models. For example, to inject the NetworkService into a ViewModel, annotate the View Model with @HiltViewModel.

@HiltViewModel
class MyViewModel @Inject constructor(
private val networkService: NetworkService
) : ViewModel() {
// ...
}

In this example, the MyViewModel class requests the NetworkService dependency via constructor injection. Hilt will automatically provide the required NetworkService instance when creating MyViewModel.

Step 5: AndroidEntryPoint Annotation​

To enable dependency injection in activities and fragments, annotate them with @AndroidEntryPoint:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
@Inject
lateinit var networkService: NetworkService

// ...
}

By using the @AndroidEntryPoint annotation, we tell Hilt to inject dependencies into this activity. Here, we inject the NetworkService instance into the networkService variable using field injection. After injecting, the networkService variable will be ready to use within the MainActivity.

Step 6: Gradle Plugin Configuration​

To ensure smooth integration and prevent certain issues, we need to configure the Gradle plugin. Add the following configurations to your app's build.gradle file:

android {
// ...
defaultConfig {
// ...
javaCompileOptions {
annotationProcessorOptions {
arguments["dagger.hilt.android.internal.disableAndroidSuperclassValidation"] = "true"
}
}
}
// ...
}

With this configuration, we disable certain superclass validation checks that can interfere with Hilt's code generation and avoid potential runtime issues.

Usage and Benefits of Hilt​

  • Simplified Dependency Injection: Hilt significantly reduces the boilerplate code required for dependency injection. The use of annotations allows developers to declare dependencies clearly and concisely.

  • Scoping and Caching: Hilt provides built-in support for scoping annotations like @Singleton, @ActivityScoped, @FragmentScoped, etc., ensuring that singleton instances are cached and reused when requested. This saves memory and processing time.

  • Easy Testing: Hilt simplifies testing by allowing you to swap out dependencies easily using different modules for testing, providing clear separation between production and test code.

  • Seamless Integration with Android Components: Hilt seamlessly integrates with Android activities, fragments, services, and view models, making it convenient to inject dependencies into these components. It allows for smooth development without worrying about manual instantiation or passing dependencies around.

Conclusion​

In this blog, we explored the step-by-step process of integrating Hilt into a Kotlin Android app. We started with a brief introduction to Hilt and its benefits. Then, we walked through the integration process, including adding dependencies, enabling Hilt in the Application class, setting up Hilt modules, injecting dependencies into Android components, and configuring the Gradle plugin. Hilt significantly simplifies the dependency injection process, resulting in a cleaner and more maintainable codebase.

By leveraging Hilt's power, developers can enhance the modularity and testability of their Android apps, leading to a smoother development process and a better user experience.

Happy coding!