Skip to main content

117 posts tagged with "software engineering"

View All Tags

WHATS NEW IN STORE FOR DEVELOPERS WITH SWIFT 6.

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

Swift 6 introduces new and creative functionalities aimed at improving your coding experience and enabling you to develop more sturdy and effective applications.

Exploring the key features of Swift 6, from advancements in concurrency to fine-tuning functions, this blog post uncovers the top 4 highlights. Discover how these enhancements can enhance your development process and open up new opportunities for your projects.

1. Swift Concurrency Isolation

Swift Concurrency is designed to ensure data safety by implementing a system that isolates code execution with actors. Actors in Swift are a concurrency feature introduced in Swift 5.5. They are designed to protect their state from data races and ensure that only one piece of code can access an actor's data at a time.

By confining code execution within these isolated units, Swift Concurrency minimizes the risk of conflicts arising from simultaneous access to shared data. In order to send data between these units, developers has to use Sendable types. You can read in detail about sendable types here https://developer.apple.com/documentation/swift/sendable

However, while this mechanism enhances data safety, it also introduces certain constraints on programming practices. Specifically, some commonly used patterns involve non-Sendable data, like classes or mutable structs, which refers to data that is not inherently safe to share across different contexts. This is because using non-Sendable types concurrently can lead to data races and compiler warns developers against it.

This limitation can impact how developers write their programs, as they must carefully consider the implications of sharing non-Sendable data within the context of Swift Concurrency.

// Not Sendable
class Client {
init(name: String, initialBalance: Double) { ... }
}

actor ClientStore {
var clients: [Client] = []

static let shared = ClientStore()

func addClient(_ c: Client) {
clients.append(c)
}
}

func openNewAccount(name: String, initialBalance: Double) async {
let client = Client(name: name, initialBalance: initialBalance)
await ClientStore.shared.addClient(client) // Error! 'Client' is non-`Sendable`!
}

1.1 Introducing Isolation Regions

Swift 6 introduces a new feature known as isolation regions, which revolutionizes the way the compiler comprehends data usage and ensures security during the transmission of non-Sendable values across isolation boundaries like actors. Isolation regions essentially equip the compiler with the ability to analyze how data is utilized, thereby enabling it to ascertain whether two data entities have the potential to influence each other and cause data race situations.

There is nothing specific for the developer to do for this capability to be activated, except upgrading to Swift 6.

2. Count and Filter

Swift now includes a nifty feature called count(where:). This method lets you efficiently count elements in a collection that meet a specific condition. It combines the functionality of filter() (which creates a new array with matching elements) and count() (which calculates the number of elements) into a single step.

let testScores = [70, 85, 90, 68, 95]
let passingCount = testScores.count(where: { $0 >= 85 })

print("Number of tests with scores 85 or higher:", passingCount)

This not only saves you from creating unnecessary temporary arrays, but also provides a cleaner and more readable way to achieve this common task.

The beauty of count(where:) is that it's not limited to just arrays. It works with any collection type that conforms to the Sequence protocol, including sets and dictionaries. This gives you a powerful and versatile tool for working with various data structures in Swift.

3. Error Handling with Typed Throws

Swift introduces a much-awaited feature: "typed throws." This eliminates a common frustration with error handling - the need for a general catch clause even when you've caught all specific errors.

enum RegistrationError: Error {
case notAlphaNumbericChars
}
  • Specificity: You can now declare precisely what types of errors a function can throw using throws(OneSpecificErrorType). This signals that only that specific error type can be thrown by the function.

  • Cleaner Code: Since Swift knows the exact error type, you can write more concise code. For example, if your function throws only RegistrationError, you can write throw .notAlphaNumbericChars instead of a generic error message.

do {
register()
} catch RegistrationError.notAlphaNumbericChars {
print("Please make sure password filed contains alpha numberic Characters")
}
  • Automatic Type Inference: In a do block that throws only one type of error, the error value in a general catch block automatically becomes the specific error type instead of a generic Error.

  • Improved Safety: Swift throws a compile-time error if you attempt to throw an error not listed in the throws clause.

  • Expressive Rethrows: You can write rethrows more clearly in many cases. For example, throws(any Error) is equivalent to just throws, and throws(Never) signifies a non-throwing function.

4. Internal Imports within Modules

Imagine a large e-commerce application with a modular architecture:

  • Core Functionality: This core module handles essential functionalities like product management, shopping cart handling, and user authentication.

  • Payment Processing: This separate module deals with secure payment processing and integrates with various payment gateways.

  • Analytics & Logging: This module is responsible for tracking user interactions, logging events, and potentially utilizing third-party analytics services.

4.1 Challenge: Dependency Management

The core application depends on both Payment Processing and Analytics & Logging modules. However, ideally, the core functionality shouldn't expose these internal dependencies to other parts of the codebase.

internal import <ModuleName>

4.2 Access Control Modifiers to the Rescue

Swift 6.0's access control modifiers on import statements come in handy here:

  • Private Imports: The core module can privately import the Payment Processing and Analytics & Logging modules. This ensures that these dependencies are not accidentally exposed or used outside the core module.

  • Encapsulation and Security: By keeping payment processing and analytics private, the core module promotes better encapsulation and potentially strengthens security by limiting access to sensitive functionalities.

These are the top 4 new features in Swift 6.0, in my opinion.

Happy Coding and Bug Fixing.

GRADLE FLAVORS: BUILDING MULTIPLE ANDROID APP VARIANTS WITH SINGLE CODEBASE

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

Gradle Flavors, also known as product flavors, allow developers to create multiple variants of their app within a single codebase. Each flavor can have its own unique configuration, resources, and dependencies, enabling customization based on factors such as branding, feature sets, or target audiences.

If you're developing free and paid versions of your app, adapting it for different languages or regions, or incorporating variations for testing purposes, Gradle Flavors offer unparalleled flexibility.

Why Use Gradle Flavors?

  • Customization: With Gradle Flavors, developers can easily tailor their app to suit specific user segments or market requirements. This level of customization fosters better user engagement and satisfaction.

  • Efficiency: Rather than maintaining separate codebases for different app variants, Gradle Flavors streamline the development process by centralizing code while allowing for variant-specific configurations. This results in reduced complexity and faster iteration cycles.

  • Consistency: By defining variant-specific resources and dependencies within the Gradle build script, developers ensure consistency across different app versions while minimizing the risk of errors or inconsistencies.

  • Market Segmentation: For businesses targeting diverse demographics or regions, Gradle Flavors facilitate the creation of specialized versions of the app tailored to each market segment's preferences and needs.

Free and Pro Versions

Let's illustrate the power of Gradle Flavors with the above scenario – creating free and pro versions of an app. Suppose you have an app called "WeatherApp" and want to offer both a free version with basic features and a paid pro version with additional functionalities.

android {
...
productFlavors {
free {
dimension "tier"
applicationId "com.example.weather.free"
versionCode 1
versionName "1.0"
// Define flavor-specific configurations
buildConfigField "boolean", "IS_PRO_VERSION", "false"
}
pro {
dimension "tier"
applicationId "com.example.weather.pro"
versionCode 1
versionName "1.0"
// Define flavor-specific configurations
buildConfigField "boolean", "IS_PRO_VERSION", "true"
}
}

buildTypes {
debug {
// Debug-specific configurations
...
}
release {
// Release-specific configurations
...
}
}
...
}

In this example, we define two product flavors: 'free' and 'pro', each with its own unique applicationID. We can then customize the behavior, features, and resources specific to each flavor, such as limiting certain features to the pro version or displaying different branding elements.

In the above example, we define two build types. "debug" and "release," each with its own configurations. These configurations might include signing configurations, proguard rules, or other build-specific settings.

With the flavors and build variants defined, Gradle will generate the following build variants:

  • freeDebug

  • freeRelease

  • proDebug

  • proRelease

Developers can then use these variants to build and test different versions of the app. For instance, they can build the "pro" release variant to generate a signed APK for distribution to users who have purchased the pro version of the app. Similarly, they can build the "free" debug variant to test new features or changes specific to the free version of the app.

Android Project Structure with Gradle Flavors

In the above example, the folder structure for the Android project would typically look like this:

- app
- src
- free
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...
- pro
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...
- main
- java
- com
- example
- weather
- MainActivity.java
- ...
- res
- layout
- drawable
- values
- ...

Here's a breakdown of the folder structure:

  • app: This is the main module of the Android project.

  • src: This directory contains the source code and resources for different build variants.

  • free: This directory contains the source code and resources specific to the "free" flavor.

java: Java source code files for the "free" flavor.

  • com.example.weather: Package directory.

  • MainActivity.java: Example activity class.

  • Other Java files specific to the "free" flavor.

  • res: Resource directory for the "free" flavor.

  • layout: XML layout files.

  • drawable: Image resources.

  • values: Resource files such as strings, colors, dimensions, etc.

  • Other resource directories specific to the "free" flavor.

  • pro: This directory contains the source code and resources specific to the "pro" flavor. The structure is similar to the "free" flavor but with resources and code specific to the "pro" variant.

  • main: This directory contains the main source code and resources shared among all flavors and build types. It serves as the base for all variants and contains code and resources common to both "free" and "pro" flavors.

By organizing the source code and resources in this way, Gradle can easily build different variants of the app by combining the contents of the "main" directory with the specific contents of each flavor directory (free and pro).

Leveraging Gradle flavors and build variants empowers Android developers to efficiently manage and customize multiple versions of their apps, catering to diverse user preferences and market requirements while maintaining codebase integrity and flexibility.

DATA STRUCTURES IN KOTLIN

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

Data structures play a crucial role in software development by enabling efficient storage and manipulation of data. Kotlin, a modern and expressive programming language, offers a variety of data structures to cater to different needs.

In this tutorial, we'll delve into the common data structures available in Kotlin, along with their use cases and implementation examples.

Major Data Structures in Kotlin are

  • Arrays

  • Lists

Immutable List

  • MutableList

  • ArrayList

  • LinkedList

  • Sets

  • Maps

  • Stacks and Queues

  • Trees (Binary Trees)

  • Graphs

Arrays

Arrays in Kotlin are collections of elements with a fixed size. While most languages allow only the same type of data to be stored in an array, Kotlin arrays can hold elements of any type.

// Creating an array of integers
val numbers = arrayOf(1, 2, 3, 4, 5)

// Accessing elements
val firstElement = numbers[0]

// Modifying elements
numbers[2] = 10

// Iterating through an array
for (number in numbers) {
println(number)
}

Arrays are suitable for scenarios requiring a fixed-size collection of elements accessed by index.

Lists

Lists in Kotlin are ordered collections that can grow or shrink in size dynamically.

Kotlin provides several types of lists, including List, MutableList, and specialized implementations like ArrayList and LinkedList.

Immutable List (List):

An immutable list cannot be modified after it is created. You can use the listOf() function to create an immutable list.

// Creating an immutable list of strings
val names: List<String> = listOf("Alice", "Bob", "Charlie")

// Accessing elements
println(names[0]) // Output: Alice

// Iterating through the list
for (name in names) {
println(name)
}

Mutable List

A mutable list allows you to add, remove, and modify elements after it is created. You can use the mutableListOf() function to create a mutable list.

// Creating a mutable list of integers
val numbers: MutableList<Int> = mutableListOf(1, 2, 3, 4, 5)

// Adding elements
numbers.add(6)

// Removing elements
numbers.removeAt(2)

// Modifying elements
numbers[0] = 10

// Iterating through the list
for (number in numbers) {
println(number)
}

ArrayList

An ArrayList is a resizable array implementation of the MutableList interface. It provides dynamic resizing and efficient random access. ArrayList in Kotlin is same as Java.

public actual typealias ArrayList<E> = java.util.ArrayList<E>
// Creating an ArrayList of doubles
val prices: ArrayList<Double> = arrayListOf(10.5, 20.3, 15.8)

// Adding elements
prices.add(25.0)

// Removing elements
prices.removeAt(1)

// Iterating through the ArrayList
for (price in prices) {
println(price)
}

LinkedList

A LinkedList is a doubly linked list implementation of the MutableList interface. It provides efficient insertions and deletions at both ends of the list.

// Creating a LinkedList of characters
val letters: LinkedList<Char> = LinkedList()
letters.add('A')
letters.add('B')
letters.add('C')

// Adding elements at the beginning and end
letters.addFirst('Z')
letters.addLast('D')

// Removing elements
letters.removeFirst()
letters.removeLast()

// Iterating through the LinkedList
for (letter in letters) {
println(letter)
}

These examples demonstrate different types of lists available in Kotlin and how to use them for various purposes. Depending on your requirements, you can choose the appropriate list implementation that suits your needs for immutability, mutability, random access, or efficient insertions and deletions.

Sets

Sets in Kotlin are collections of unique elements with no duplicates.

// Creating a set of integers
val uniqueNumbers = setOf(1, 2, 3, 3, 4, 5)

// Adding and removing elements
val mutableSet = mutableSetOf<Int>()
mutableSet.add(6)
mutableSet.remove(3)

// Checking membership
val containsFive = 5 in mutableSet

Sets are useful when you need to ensure uniqueness or perform set operations like union, intersection, and difference.

Maps

Maps in Kotlin are collections of key-value pairs where each key is unique.

// Creating a map of student grades
val studentGrades = mapOf("Alice" to 90, "Bob" to 85, "Charlie" to 92)

// Accessing values
val aliceGrade = studentGrades["Alice"]

// Modifying values (Not allowed for immutable maps)
val mutableGrades = studentGrades.toMutableMap()
mutableGrades["David"] = 88

// Iterating through a map
for ((name, grade) in studentGrades) {
println("$name: $grade")
}

Maps are ideal for situations requiring key-value associations and efficient lookups based on keys.

Stack and Queues

Stacks follow the Last-In-First-Out (LIFO) principle and support push and pop operations where as Queues adhere to the First-In-First-Out (FIFO) principle and support enqueue and dequeue operations.

ArrayDeque in Kotlin, introduced in version 1.4, is a mutable collection that functions as both a queue and a stack. It is provides efficient operations for adding and removing elements from both ends of the deque.

import kotlin.collections.ArrayDeque

val deque = ArrayDeque<Int>()

// Adding elements to the deque
deque.addFirst(1)
deque.addLast(2)

// Removing elements from the deque
val firstElement = deque.removeFirst()
val lastElement = deque.removeLast()

println("First Element: $firstElement") // Output: First Element: 1
println("Last Element: $lastElement") // Output: Last Element: 2

Trees (Binary Trees)

Binary trees consist of nodes where each node has at most two child nodes.

// Node structure for a binary tree
class BinaryTreeNode<T>(val value: T) {
var left: BinaryTreeNode<T>? = null
var right: BinaryTreeNode<T>? = null
}

// Creating a binary tree
val root = BinaryTreeNode(1)
root.left = BinaryTreeNode(2)
root.right = BinaryTreeNode(3)

//Reading the tree nodes.
fun <T> inOrderTraversal(node: BinaryTreeNode<T>?) {
if (node == null) return
inOrderTraversal(node.left)
println(node.value)
inOrderTraversal(node.right)
}

println("In-order traversal:")
inOrderTraversal(root)

Output:
In-order traversal:
2
1
3

Here we excute an In-order traversal. In-order traversal visits the left subtree, then the root, and finally the right subtree. Binary trees are useful for hierarchical data representation, searching, and sorting algorithms like binary search.

Graphs

Graphs are complex structures consisting of nodes and edges.

Graphs are essential for modelling relationships and solving problems like route finding and network analysis.

// Implementing an adjacency list for a graph
class Graph {
val adjacencyList = mutableMapOf<Int, MutableList<Int>>()

fun addEdge(from: Int, to: Int) {
adjacencyList.getOrPut(from) { mutableListOf() }.add(to)
}
}

// Creating a graph
val graph = Graph()
graph.addEdge(1, 2)
graph.addEdge(1, 3)
graph.addEdge(2, 4)

// Accessing adjacency list
val adjacencyList = graph.adjacencyList

// Reading all vertices and their adjacent vertices
for ((vertex, adjacentVertices) in adjacencyList) {
println("Vertex $vertex is connected to: $adjacentVertices")
}

// Reading adjacent vertices of a specific vertex
val vertexAdjacent = adjacencyList[1]
println("Adjacent vertices of vertex 1: $vertexAdjacent")

Output
Vertex 1 is connected to: [2, 3]
Vertex 2 is connected to: [4]
Adjacent vertices of vertex 1: [2, 3]

The Graph class represents a graph data structure using an adjacency list. In this implementation, the adjacency list is stored as a mutable map where the keys represent the vertices (nodes) of the graph, and the corresponding values are lists containing the adjacent vertices.

adjacencyList:

It's a mutable map where the keys are integers representing the vertices, and the values are mutable lists containing adjacent vertices.

addEdge:

The addEdge function adds an edge between two vertices in the graph. It takes two parameters: from (the source vertex) and to (the destination vertex).

If the from vertex already exists in the adjacency list, getOrPut retrieves its associated mutable list of adjacent vertices. If it doesn't exist, a new entry is created and to vertex is then added to the list of adjacent vertices for the from vertex.

In the above code snippet, the for-in loop prints each vertex along with its adjacent vertices.

The last println statement prints the adjacent vertices of vertex 1, which are [2, 3].

Conclusion

This blog post aims to provides a comprehensive overview of various data structures available in Kotlin, along with their use cases and implementation examples. Experiment with these data structures to enhance your understanding and proficiency in Kotlin programming.

INTEGRATING URL_LAUNCHER IN FLUTTER APPS

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

The mobile app development world has moved from fast to ‘impatiently fast’. One essential aspect of faster user interaction is the ability to navigate to external websites or open other apps directly from within your Flutter application.

This is where the url_launcher plugin for Flutter comes into play. This plugin allows you to open URLs in the default web browser of the device. It also allows for ​​opening URLs that launch other apps installed on the device such as emails or social media apps. 

Installing URL Launcher in Flutter

Installation can be done in a whiff by following the code given below: 

Terminal Command

flutter pub add url_launcher

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
url_launcher: x.y.z.

Supported URL Schemes

url_launcher supports various URL schemes. They are essentially prefixes or protocols that help define how a URL should be handled by Android, iOS or any operating system or apps in general. Common URL Schemes supported by url_launcher include HTTP, HTTPS, mailto, SMS, tel, App Schemes and Custom Schemes. 

Integrating url_launcher

When using the url_launcher package, you can open URLs with these schemes using the launch function. This package will delegate the URL handling to the underlying platform, ensuring compatibility with both Android and iOS. 

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class MyApp extends StatelessWidget {

@override Widget build(BuildContext context) {

    return MaterialApp(

      home: Scaffold(

        appBar: AppBar(
          title: Text('URL Launcher Example'),
        ),

        body: Center(
          child: ElevatedButton(

            onPressed: () {
              _openURL("https://appxiom.com/");
            },

            child: Text('Open URL'),

          ),

        ),

      ),

    );

  }

  // Function to open a URL using url_launcher

  void _openURL(String url) async {

    if (await canLaunchUrl(url)) { //Checking if there is any app installed in the device to handle the url.

      await launch(url);

    } else {

      // Handle error

    }

  }

}

Configuring canLaunchUrl in iOS

Make sure to add the URL schemes passed to canLaunchUrl as LSApplicationQueriesSchemes entries in your info.plist file. Otherwise, it will return false.

<key>LSApplicationQueriesSchemes</key>

<array>

  <string>sms</string>

  <string>tel</string>

</array>

Configuring canLaunchUrl in Android

Add the URL schemes passed to canLaunchUrl as <queries> entries in your AndroidManifest.xml, otherwise, it will return false in most cases starting on Android 11 (API 30) or higher. 

&lt;!-- Provide required visibility configuration for API level 30 and above --&gt;

&lt;queries&gt;

&nbsp;&nbsp;&lt;!-- If your app checks for SMS support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.intent.action.VIEW" /&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;data android:scheme="sms" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&nbsp;&nbsp;&lt;!-- If your app checks for call support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.intent.action.VIEW" /&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;data android:scheme="tel" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&nbsp;&nbsp;&lt;!-- If your application checks for inAppBrowserView launch mode support --&gt;

&nbsp;&nbsp;&lt;intent&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;action android:name="android.support.customtabs.action.CustomTabsService" /&gt;

&nbsp;&nbsp;&lt;/intent&gt;

&lt;/queries&gt;

That’s it for now. For more information on  url_launcher with Flutter, check https://pub.dev/packages/url_launcher/

MANAGING USER EXPERIENCE WHEN BUGS AND PERFORMANCE ISSUES HAPPEN IN ANDROID APP.

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

Even the most meticulously crafted apps may encounter issues or bugs that hinder user satisfaction. To address this challenge, leveraging real-time issue tracking capabilities is essential.

In this blog post, we'll explore how the issue callback listener, the most coveted feature of Appxiom Android SDK can empower developers to proactively improve user experience and manage user retention when performance issues and bugs happen in the app.

Real-Time Issue Tracking with Appxiom Android SDK

The Appxiom Android SDK offers a powerful solution for tracking and reporting issues in real-time within Android apps. One of its standout features is the Issue Callback Listener, which allows developers to receive real-time notifications whenever an issue is detected within their app code-base. By registering a global listener, developers gain access to every issue detected by the SDK.

Get callback when an issue is detected

Integrating the Issue Callback Listener into your Android app is straightforward. Developers can utilize either Java or Kotlin to add the listener (IssueFoundListener) within the Application class.

Java code implementing the IssueFoundListener.

Ax.listenForIssue(new&nbsp;IssueFoundListener()&nbsp;{
@Override
public&nbsp;void&nbsp;issueFoundInBackgroundThread(IssueView&nbsp;issue)&nbsp;{
//Notify the user
}
});
Kotlin code implementing the IssueFoundListener.
Ax.listenForIssue(IssueFoundListener { 
issueView: IssueView? -&gt;
//Notify the user
})

Upon detection of an issue, the callback function is triggered, providing developers with an IssueView object containing datapoints about the issue, including type, severity, occurrence time, and a short description.

Enhance User Experience in Android app by handling issues gracefully

Currently, the developers make use of fields in IssueView class like type and the severity, developers notify the app users (like a toast message) that an issue was detected and will be rectified soon. Read more about adding the listener here.

By promptly identifying and notifying users about issues, developers can enhance user experience, leading to higher user satisfaction and increased app retention rates. Most of the time app users will have no idea if the issues they encounter within the app will be fixed. This feature helps app users to be informed that the issue will be fixed in a future version.

Handle Callbacks in Background Thread

Since the callback function is executed in a background thread, developers should ensure that any UI-related actions are performed on the UI thread to prevent performance issues.

Benefits for Developers

By leveraging the Issue Callback Listener (IssueFoundListener) provided by the Appxiom Android SDK, developers can unlock a multitude of benefits:

  • Proactive Issue Detection: Receive real-time notifications for every issue detected within your app.

  • Comprehensive Issue Information: Gain access to detailed information about each issue, including type, severity, occurrence time, and a short description, facilitating efficient debugging and resolution.

  • Seamless Integration: With a simple integration process, developers can effortlessly incorporate the listener into their Android apps, ensuring minimal disruption to existing workflows.

Visit appxiom.com to learn more about Appxiom.

GUIDE ON OPTIMIZING FLUTTER APP USING DART ANALYZER

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

Flutter projects come equipped with a dart analyzer for static code analysis. When a project is created with Flutter version 2.3.0 and above, a default configuration file named analysis_options.yaml will be generated at the root of the project. Analyzer tool runs checks based on the checks set in this configuration file.

Lints are a set of rules to check the code for potential errors or formatting issues. The configuration file analysis_options.yaml has a set of recommended lints for Flutter applications, packages, and plugins. This is achieved through the automatic inclusion of package:flutter_lints/flutter.yaml.

include: package:flutter_lints/flutter.yaml

linter:
rules:

Dart-enabled Integrated Development Environments (IDEs) like visual studio code typically display the issues detected by the analyzer in their user interface. Alternatively, you can manually run the analyzer by executing flutter analyze from the terminal to identify and address code issues.

In this blog post, we'll delve into how to customize the lint rules, empowering developers to tailor it to their specific needs.

Customizing Lint Rules in Flutter

The real power of the Dart analyzer configuration lies in the ability to customize lint rules according to your project's requirements. The linter section allows developers to fine-tune lint rules, either by disabling those inherited from flutter.yaml or by enabling additional rules.

Warning: Linter rules may throw false positives.

Configuring Lint rules at the Project level

The analysis_options.yaml configuration file allows developers to customize lint rules at the project level.

linter:
rules:
avoid_print: false
prefer_single_quotes: true

In this example, the avoid_print rule is disabled by setting it to false, and the prefer_single_quotes rule is enabled by setting it to true. This level of granular control allows developers to enforce or relax specific rules based on their project's coding standards.

Configuring Lint rules at File/code level

In addition to configuring lint rules in the global scope as shown above, developers can suppress lints for specific lines of code or files using comments.

The syntax // ignore: name_of_lint or // ignore_for_file: name_of_lint can be used to silence lint warnings on a case-by-case basis.

// ignore_for_file: name_of_lint
class&nbsp;Class&nbsp;{
// ignote: name_of_lint
var&nbsp;_count = 0;

var&nbsp;_count2 = 0;
}

Sample Case Studies

Now, let us dive into couple of lint rules to get a better idea of what exactly these rules can do.

Omitting explicit Local variable types

In situations where functions tend to be concise, local variables often have limited scope. Omitting the variable type helps shift the reader's focus toward the variable's name and its initialized value, which are often more crucial aspects.

With explicit types

List&lt;List&lt;FoodItem&gt;&gt; findMatchingMeals(Set&lt;FoodItem&gt; kitchen) {
List&lt;List&lt;FoodItem&gt;&gt; meals = &lt;List&lt;FoodItem&gt;&gt;[];
for (final List&lt;FoodItem&gt; mealRecipe in recipeBook) {
if (kitchen.containsAll(mealRecipe)) {
meals.add(mealRecipe);
}
}
return meals;
}

Without explicit types

List&lt;List&lt;FoodItem&gt;&gt; findMatchingMeals(Set&lt;FoodItem&gt; kitchen) {
var meals = &lt;List&lt;FoodItem&gt;&gt;[];
for (final mealRecipe in recipeBook) {
if (kitchen.containsAll(mealRecipe)) {
meals.add(mealRecipe);
}
}
return meals;
}

To warn if explicit type is used in the local variables, use the lint rule omit_local_variable_types,

linter:
rules:
-&nbsp;omit_local_variable_types

Disable avoid_print in lint rules

It is always advisable to avoid incorporating print statements into production code. Instead, you can opt for debugPrint or enclose print statements within a condition checking for kDebugMode.

void&nbsp;processItem(int&nbsp;itemId)&nbsp;{
debugPrint('debug: $x');
...
}

void processItem(int itemId) {
if (kDebugMode) {
print('debug: $x');
}
...
}

By default, print statements are flagged by the analyzer.

With lint rules you can override this, set avoid_print to false as shown below,

linter:
rules:
-&nbsp;omit_local_variable_types
avoid_print: false

Conclusion

Customizing the Dart analyzer is a pivotal step in elevating your Flutter development. Begin by activating recommended lints for Flutter, encouraging good coding practices.

The real power lies in the ability to finely tune lint rules at both project and file levels, granting granular control over code standards. Use comments judiciously to suppress lints where needed.

Lastly, The Dart language provides an extensive list of available lint rules, each documented on the official Dart website https://dart.dev/tools/linter-rules#rules.

LIVEDATA: AN OBSERVABLE DATA HOLDER FOR ANDROID APPS

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

LiveData is an observable data holder class in Android as part of androidx package. It notifies the UI about data changes, but unlike its regular counterparts, it's lifecycle-aware. This means that it adheres to the lifecycles of activities, fragments, and other app components, ensuring that the data updates only reach the active observers, thus preventing memory leaks.

Consider a use-case where the app displays a screen with the latest blog posts. With LiveData, you,

  • Fetch the blog posts data from database or API call and set it to a MutableLiveData instance.

  • Create an Observer object in your activity/fragment to update the UI.

  • Observe the LiveData object using observe().

Whenever the blog post data changes (due to network calls, etc.), only the active activity/fragment receives the update. If it's in the background, it'll automatically receive the latest data when it resumes. No more manual lifecycle handling or data redundancy.

Working with LiveData

Following steps will help you get started with implementing a LiveData instance in a Kotlin based android app using MVVM architecture. Here we are using LiveData to update the UI when the data changes at the source.

  • Create a LiveData instance: In MVVM architecture, data is fetched in a ViewModel class. Create a LiveData instance in your ViewModel class.

  • Create an Observer object: Define the observer, listen to LiveData and update the UI based on data changes.

  • Attach the Observer: Use observe() with the relevant LifecycleOwner to connect the Observer to the LiveData object.

Defining LiveData in a ViewModel class (MVVM architecture)

class UserViewModel : ViewModel() 
// MutableLiveData to hold post data
private val _posts = MutableLiveData&lt;List&lt;Post&gt;&gt;()
val posts: LiveData&lt;List&lt;Post&gt;&gt;
get() = _posts
// Fetch posts from a repository (replace with your implementation)
fun fetchPosts() {
//This will be a function call to repository class to fetch data
val dummyPosts = listOf(
Post(1, "Title 1", "Content 1"),
Post(2, "Title 2", "Content 2")
)
_posts.postValue(dummyPosts) // Update LiveData with fetched posts from a worker thread.

}

}
  • We define a MutableLiveData called _posts to hold the list of Post objects.

  • We expose a public posts LiveData that other components can observe.

  • The fetchPosts() method simulates fetching posts from a repository and updates the _posts value using postValue().

Observing LiveData

class UserPostsActivity : AppCompatActivity() {
private val model: UserViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Update your UI with the list of posts
val postObserver = Observer&lt;List&lt;Post&gt;&gt; { posts -&gt;
recyclerView.adapter = PostAdapter(posts) // Update your UI with the list of posts
}

// Observe the posts LiveData with UserPostActivity as the lifecycleowner.
model.posts.observe(this, postObserver)

//fetch the post data.
model.fetchPosts()
}
}
  • We get a reference to the UserViewModel.

  • We define an Observer that receives the list of Post objects when the posts LiveData changes.

  • In onCreate(), we observe the posts LiveData using observe() and the current LifecycleOwner (UserViewModel).

  • When new data arrives, the postObserver updates the UI, e.g., by setting the adapter for a RecyclerView with the list of posts.

This is a basic example that demonstrates how LiveData can simplify data management and improve responsiveness of the UI in a Kotlin based Android app. Remember to adapt it to your specific data and UI needs.

*Update LiveData objects using ****setValue() ****from the main thread and postValue() from a worker thread. *

Benefits of using LiveData in MVVM

  • Separation of concerns: ViewModel manages data and updates LiveData, while the activity/fragment handles UI.

  • Lifecycle awareness: Only active components receive data updates, preventing crashes and unnecessary calculations.

  • Single source of truth: posts LiveData acts as the central source for post data, ensuring consistency across the UI.

  • Simplified UI updates: Observer handles data changes and updates the UI automatically.

Using MediatorLiveData

MediatorLiveData is a special type of LiveData in Android, combining data from multiple LiveData sources and creating a single unified information. 

It's useful when you need to:

  • Merge data from multiple sources: Imagine displaying comments and reactions on a post. You'd have separate LiveData objects for both, and MediatorLiveData can combine them into a single "post details" feed.

  • Respond to complex data conditions: You can define custom logic based on values from different sources. For example, show a "New" badge on a post only if both comments and reactions have updates.

  • Simplify data access: Instead of observing multiple sources, you just observe the MediatorLiveData, making your work flow cleaner and more centralized.

Common Scenarios to use LiveData

  • Use LiveData with Room's observable queries to keep your UI in sync with the database.

  • Combine LiveData with Kotlin coroutines for asynchronous data handling especially when making API calls.

  • Leverage MediatorLiveData to merge multiple LiveData sources for unified data access.

LiveData is a powerful tool that simplifies data management, improves UI responsiveness and help avoid memory related issues like leaks.

A CHECKLIST FOR CREATING DOCUMENTATION FOR AN IOS FRAMEWORK

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

iOS Frameworks are share libraries that can be integrated by the developer into the apps and use the functionalities built-in into those libraries. But even the most powerful framework falls short without proper documentation, thus affecting their adoption. The basic purpose of building Frameworks is to help other developers to use the functionalities by integrating the Framework to their app. This requires good documentation that explains each and every steps in the integration process and also about how to make use of the Framework capabilities.

Documentation website is the map that guides developers through using your Framework. So, how do you craft documentation that shines as brightly as your code? Let's delve into the nitty-gritty of crafting top-notch iOS framework documentation website.

Where to Host the Documentation of your iOS Framework

*Choosing the right way to host your documentation is crucial. *

Creating Documentation website from Scratch

Writing documentation from scratch gives you complete control over the structure, design, and features. You can tailor it to fit the specific needs and aesthetics of your iOS framework. But this can be time-consuming, especially for large projects as it requires significant investment in planning, writing, and maintaining content.

Using Documentation Website generators

Website generators like GitPages or Docusaurus are best suited for documentation hosting.

They provide a quick start, allowing you to set up documentation with minimal effort. Availability of templates and pre-built themes helps streamline the process. Website generators enforce consistency in structure and style, making it easier for users to navigate and understand the content. This is particularly useful for maintaining a professional and cohesive look across projects.

When choosing a website generator, it is ideal to choose one that works based on mark-down language. This will help you switch between multiple such tools with ease if needed.

Creating a Readme File/First documentation page

Don't underestimate the power of a well-written First page/Readme file. It's the first impression your framework makes! Keep it updated and consider employing a templating system to streamline maintenance.

  • A concise overview: First page of the documentation website or the Readme file should clearly explain what your framework is. This is important because there will be situations where developers might land on your documentation page first.

  • Prerequisites and dependencies: This section should specify requirements like the minimum supported OS version of the framework or the need to integrate a distribution framework like Cocoapods.

  • Installation instructions: Make it easy for developers to get started. Provide a step-by-step process to help developers kickstart. If you are distributing the framework via cocoapods or any other distribution medium, make sure detailed steps are provided.

  • Basic usage examples: Showcase the core functionalities through code snippets. Code snippets are a must-have for every documentation website. Make sure any API that the developer has to implement when using the framework is explained with code snippets within the documentation website.

Managing documentation of Versions

As your framework evolves, documentation needs to keep pace. Version control systems like Git help you to,

  • Track changes and revert to previous versions if needed.

  • Use the Tag feature in Git to link specific documentation versions to framework releases.

  • Collaborate with fellow developers on documentation updates.

Semantic Versioning

Implement semantic versioning (major.minor.patch) to communicate changes and guide developers through updates. Website generators like GitPages and Docusaurus support versioning out of the box.

Structure for Versioning

A SemVer version number consists of three parts, separated by dots:

  • Major: Indicates significant breaking changes incompatible with previous versions. Increment for major changes in functionality or API.

  • Minor: Introduces new features or enhancements while maintaining backward compatibility. Increment for new features or bug fixes that don't break existing code.

  • Patch: Fixes bugs or makes minor improvements without changing functionality. Increment for bug fixes or performance enhancements.

Guidelines for Versioning

Here are some key guidelines for implementing SemVer:

  • Start with 1.0.0 for your initial release.

  • Only increment the major version number when:

  • You introduce major breaking changes, such as changes that break backward compatibility.

  • You rewrite a significant portion of your codebase.

  • You completely change the functionality of your framework.

  • Increment the minor version number when you:

  • Add new features that don't break existing code.

  • Make significant enhancements to existing functionality.

  • Increment the patch version number when you:

  • Fix bugs in the existing functionality.

  • Make minor improvements to performance or documentation.

  • Always specify a changelog for each release. This will help developers understand what changes were made in each version.

  • Consider using pre-release identifiers like beta or rc before you release a stable version.

  • Follow the SemVer specification strictly to maintain consistency and avoid confusion.

Organizing the Documentation

  • Organize your documentation by features, making it easy for developers to find what they need.

  • Group related functionalities under clear headings. Use internal linking to connect relevant sections.

  • Provide code snippets in abundance.

Organization of different section in Appxiom Documentation

Release Notes

Keep developers informed about changes with detailed release notes. Make sure each release notes include the following,

  • New features and bug fixes: Highlight what's new and improved.

  • API changes: In case of any API changes that the developer needs to update make sure that is mentioned in the release notes.

  • Breaking changes: Clearly explain any potential compatibility issues.

  • Upgrade instructions: Guide developers on transitioning to the new version, if needed.

Deprecating an API

Deprecating an API in an iOS framework is a crucial step when your framework evolves and some functionalities may need retirement. 

Here's a breakdown of the process to ensure a smooth transition for developers,

  • Announce the Deprecation:

Specify the API: Clearly identify the API to be deprecated along with its current version. Provide detailed information about the function, parameters, and return values.

  • Set a Deprecation Timeline: Choose a reasonable timeframe for sunsetting the API. This period allows developers to adjust their code and find alternatives. Consider a minimum of 6 months for major APIs and shorter periods for minor functionalities.

  • Communicate through official channel: Utilize your developer documentation and release notes to make the announcement visible to your user base.

  • Offer Migration Guidance:

Suggest Alternatives: Recommend new or existing APIs that can replace the deprecated functionality. Provide detailed migration guides with code examples and explanations to ease the transition.

  • Versioning Strategies: If relevant, offer compatibility layers or bridge APIs that work with both the old and new versions, helping developers migrate step-by-step.

  • Deprecation Warnings: Implement warnings within your framework code that notify developers when they use the deprecated API. This provides immediate feedback and encourages the switch to newer methods.

Use @available(*, deprecated) in Swift to mark a function as deprecated.

  • Use __deprecated in Objective-c to mark a function as deprecated.

  • Sunset the API:

Remove the Deprecated API: Once the deprecation period ends, remove the deprecated API from your framework codebase. Clearly document this final step in your changelog and release notes.

Remember: Deprecation is a process, not an event. By following these steps, you can minimize disruption for your developers and ensure a smooth evolution of your dynamic framework.

SEO for making your Docs Discoverable

*Discoverable documentation is every developer's dream. *SEO will make the documentation website discoverable and get to the top of search engine results! Here are the basic steps to achieve it,

  • Keyword Research:

Dive into developer minds: Research relevant keywords developers use to find information about your framework or similar technologies. Tools like Google Keyword Planner, Ahrefs, and SEMrush can be your allies.

  • Target long-tail keywords: While "framework" might be competitive, "best practices for XYZ framework API" could be your golden ticket.

  • Focus on intent: Understand the search intent behind keywords. Are developers looking for tutorials, troubleshooting guides, or API references?

  • Content Optimization:

Craft compelling titles and meta descriptions: Optimize titles and meta descriptions for target keywords while remaining informative and engaging.

  • Structure your content wisely: Use clear headings, subheadings, and bullet points for easy navigation and scannability.

  • Internal linking: Link related pages and tutorials within your documentation, creating a strong knowledge network.

  • Technical SEO:

Mobile-friendliness is key: Ensure your website is mobile-responsive to cater to developers on the go.

  • Speed is your friend: Optimize page loading times for a smooth user experience. Tools like Google PageSpeed and PingDom Insights can help identify bottlenecks.

  • HTTPS for trust: Secure your website with HTTPS to build trust and improve search engine ranking. Most browsers warn users if the website is not secure, diminishing the trust.

  • Building Buzz:

Promote your documentation: Share it on social media, relevant forums, and developer communities.

  • Build relationships: Connect with bloggers, influencers, and other developers in your niche. Guest posts and collaborations can amplify your reach.

  • Monitor and analyze: Track your website traffic and search engine rankings. Use tools like Google Search Console and Google Analytics to understand what's working and what needs improvement.

  • Bonus Tips:

Utilize structured data: Implement schema markup to give search engines richer information about your content, potentially leading to richer search results.

  • Localize your documentation: Consider translating your documentation for wider reach if your target audience is global.

  • Embrace video: Videos and code samples can enhance engagement and improve user experience.

Continuous Improvement

Remember, documentation is a living document, not a set-and-forget affair. Gather feedback from users, actively address issues, and keep it updated alongside your framework.

Following these steps, you should be able to craft iOS framework documentation that is helpful for the developers. It's an investment that pays off in developer satisfaction, and the ultimate success of your creation.

About Appxiom Documentation

We used Docusaurus for create oour documentation. It is based on mark-down language which gets built to vanila html files. This helps in deploying the documentation through an webserver. Visit https://docs.appxiom.com to explore our documentation.

DEVELOPING ANDROID APPS FOR FOLDABLE DEVICES

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

The advent of foldable devices has ushered in a new era of innovation in app development. With screens that seamlessly transition between large and small sizes, developers have a unique canvas to create immersive and adaptive user experiences.

In this blog post, we will explore the key considerations and techniques for developing Android apps optimized for foldable devices.

Responsive/Adaptive Design

Responsive design is the cornerstone of developing apps for foldable devices. The ability of these devices to fold inward or outward poses a challenge that can be met with a responsive layout. Whether you're using ConstraintLayout for traditional views or BoxWithConstraints for Jetpack Compose, the goal is to ensure that your app looks and functions seamlessly across various foldable form factors.

One crucial aspect is avoiding reliance on physical, hardware values for layout decisions. Instead, base your decisions on the actual portion of the screen allocated to your app. This approach guarantees flexibility, making your app adapt well to foldable scenarios like multi-window mode.

The use of WindowManager in a Compose app can provide insights into the current window metrics, allowing your app to make informed decisions based on the available screen space. Converting raw sizes into meaningful size classes, as outlined in the WindowSize Class documentation, further enhances your app's adaptability.

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.core.view.WindowCompat
import androidx.window.layout.WindowInfo
import androidx.window.layout.WindowInfoRepository.Companion.windowInfoRepository

class LoadingActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Optimize for foldables by enabling window features
WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
val windowSizeClass = remember {
calculateWindowSizeClass(this)
}
ChatApp(windowSizeClass)
}
}
}

@Composable
fun ChatApp(windowSizeClass: WindowSizeClass) {
// Determine whether to show the top app bar based on size class
val showTopAppBar = windowSizeClass.heightSizeClass != WindowHeightSizeClass.Compact

// MyScreen operates independently of window sizes and utilizes a Boolean flag
ChatScreen(
showTopAppBar = showTopAppBar,
)
}
// Function to calculate window size class using WindowInfo
@Composable
fun calculateWindowSizeClass(activity: ComponentActivity): WindowSizeClass {
val windowInfoRepository = windowInfoRepository(activity)
val windowInfo = windowInfoRepository.getSnapshot().getOrDefault(WindowInfo.EMPTY)

return windowInfo.calculateWindowSizeClass()
}

// Additional Composable for displaying the screen content
@Composable
fun ChatScreen(showTopAppBar: Boolean) {
}

Foldable States and Postures

Understanding foldable states and postures is essential for crafting a seamless user experience. Foldable devices can be in various states, such as FLAT or HALF_OPENED, each offering unique layout possibilities. In the HALF_OPENED state, postures like tabletop and book postures introduce further creative opportunities but also pose challenges.

Developers need to ensure that UI elements remain accessible in all device states. Dialog boxes, pop-up menus, and other controls should be positioned strategically to avoid interference with the fold. Accommodating the limitations imposed by the HALF_OPENED state, such as obscured content near the fold, is crucial for delivering a user-friendly design.

App Continuity

App continuity is a key consideration when developing for foldable devices. As these devices fold and unfold, apps may stop and restart, necessitating the seamless restoration of user states.

From retaining typed text in input fields to restoring keyboard states and scroll positions, maintaining continuity enhances the user experience.

Furthermore, app layouts on folded and unfolded screens should complement each other. Users should be able to experience a natural flow between different screen layouts, with content seamlessly transitioning and enhancing the overall user journey.

Drag and Drop Interactions

Foldable devices with large screens provide an ideal canvas for drag and drop interactions. Multi-window mode on foldables allows users to drag and drop content between apps, creating a productive and engaging experience. Developers can leverage the Android drag and drop framework to implement these interactions, adding a layer of sophistication to the apps.

Why Develop Apps for Foldable Devices?

  • Innovative User Experiences: Foldable devices offer a unique canvas for creative and innovative user experiences. By embracing the flexibility of foldable screens, developers can design apps that stand out in the crowded app landscape.

  • Expanded Market Reach: As foldable devices become more popular, developing apps optimized for these devices opens up new opportunities and expands your app's potential user base. Catering to emerging trends ensures that your app remains relevant in a rapidly evolving tech landscape.

  • Differentiation in a Competitive Market: Developing for foldable devices allows you to differentiate your app from competitors. Users are often drawn to apps that leverage the full potential of their devices, and being an early adopter of foldable technology can set your app apart.

As the market for foldable devices continues to grow, developers who adapt to this evolving landscape position themselves at the forefront will benefit.

A BEGINNERS GUIDE TO INTEGRATING SQFLITE IN FLUTTER PROJECTS

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

In the world of mobile app development, the need for robust and efficient data management solutions is paramount. When building complex Flutter applications that require local data storage and management, this can prove to be a game-changer.

What is Sqflite?

Sqflite is a Flutter plugin that provides a simple and efficient way to implement SQLite databases in your Flutter applications. With Sqflite, you can perform various database operations, such as creating, querying, updating, and deleting data, making it an essential tool for managing local data storage in your Flutter projects.

Its simplicity, coupled with its powerful capabilities, makes it a popular choice for developers looking to incorporate local database functionality into their applications.

How to Integrate Sqflite in Flutter Projects

Integrating Sqflite into your Flutter projects is a straightforward process. Follow these simple steps to get started:

Add the Dependency

Open your project's pubspec.yaml file and add the Sqflite dependency:

dependencies:
sqflite: ^2.3.0

Install the Dependency

After adding the dependency, run the following command in your terminal:

flutter pub get

Import Sqflite

Import the Sqflite package into your Dart code:

import 'package:sqflite/sqflite.dart';

Use Sqflite API

Utilize the Sqflite API to create and manage your SQLite database operations. You can create tables, execute queries, and perform various data manipulation tasks.

Creating a Database and Table

Here we create a SQLite database and a table within that database using Sqflite in Flutter. It utilizes the openDatabase method to create a new database or open an existing one.

The onCreate callback is used to execute a SQL command that creates a table named "Users" with three columns: "id" as the primary key, "username" as a TEXT type, and "age" as an INTEGER type. Additionally, it retrieves the path for the database using getDatabasesPath() and joins it with the database name "example.db".

import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

Future&lt;void&gt; createDatabase() async {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'my_database.db');

// Delete any existing database:
await deleteDatabase(path);

// Create the database
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute('
CREATE TABLE Users (
id INTEGER PRIMARY KEY,
username TEXT,
age INTEGER
)
');
});
}

Inserting a Row

To insert a row into the "Users" table in the SQLite database, it utilizes the insert method, which takes the table name, a map representing the data to be inserted, and an optional conflictAlgorithm parameter to handle conflicts that may arise during the insertion process.

In this case, if there is a conflict, the existing row is replaced with the new data.

Future&lt;void&gt; insertData(Database database) async {
await database.insert(
'Users',
{'username': 'Alice', 'age': 30},
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

Selecting Data

To perform a simple query to retrieve data from the "Users" table in the SQLite database, it utilizes the query method, which takes the table name as a parameter and returns a list of maps representing the queried rows. The retrieved data can then be used for further processing or display purposes.

Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; queryData(Database database) async {
return await database.query('Users');
}

Custom SQL query

To execute a custom SQL query that selects all rows from the "Users" table where the value of the "age" column is greater than 25. The rawQuery method allows you to execute custom SQL queries directly.

Make sure to handle the results appropriately based on the specific requirements of your application.

Future&lt;List&lt;Map&lt;String, dynamic&gt;&gt;&gt; customQuery(Database database) async {
return await database.rawQuery('SELECT * FROM "Users" WHERE age &gt; 25');
}

Deleting a Row

To delete a specific row from the "Users" table in the SQLite database based on a provided condition, it uses the delete method, which takes the table name, a where clause specifying the condition for deletion, and optional whereArgs to provide values for the placeholders in the where clause. The method returns the number of rows deleted as an integer.

Future&lt;int&gt; deleteData(Database database, int id) async {
return await database.delete('Users', where: 'id = ?', whereArgs: [id]);
}

Conclusion

In conclusion, integrating Sqflite in your Flutter projects can significantly enhance the performance and user experience of your applications.

Its simplicity, efficiency, and powerful data management capabilities make it an indispensable tool for managing local data storage and operations. By following the steps outlined in this guide and leveraging Sqflite's robust features, you can create powerful Flutter applications that deliver a seamless and efficient user experience.

UNDERSTANDING FLOW IN KOTLIN

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

Kotlin, a statically-typed, modern programming language, has introduced a powerful asynchronous programming concept called Flow. Flow simplifies asynchronous programming in Kotlin, making it more efficient and expressive.

In this blog post, we'll delve into what Flow is, how to use it, and provide practical examples in the context of Android app development. We'll explore how to send internal notifications to different modules within an Android application using Kotlin Flow.

What is Flow in Kotlin?

Flow is a new asynchronous stream processing library introduced in Kotlin, specifically designed to handle streams of data asynchronously and in a non-blocking way. It's built on top of Kotlin's coroutines and allows you to work with sequences of values as they become available.

Key features of Flow:

  • Asynchronous: Flow is designed for asynchronous operations and is perfect for use cases where you want to handle data asynchronously without blocking the main thread.

  • Non-blocking: Flow works seamlessly with Kotlin's coroutines, which means it is non-blocking and doesn't freeze your app while processing data.

  • Backpressure: Flow can handle backpressure, allowing you to control the flow of data between the producer and consumer, preventing overloading of resources.

  • Composability: Flow can be easily combined, transformed, and modified, making it a versatile tool for stream processing.

How to use Flow in Kotlin?

To use Flow in your Kotlin application, follow these steps:

1. Import the necessary dependencies:

To use Flow in an Android app, you need to include the Kotlin coroutines library in your project.

You can add it to your app-level build.gradle file:

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:x.x.x" // Use the latest version

2. Create a Flow:

You can create a Flow using the following code, providing the values you want to emit.

For example:

fun fetchUserData(): Flow&lt;User&gt; = flow {
// Fetch user data asynchronously
val user = api.fetchUser()
emit(user)
}

3. Collect data from a Flow:

To consume data from a Flow, you can use the collect extension function. This allows you to receive emitted values and handle them:

viewModelScope.launch {
fetchUserData().collect { user -&gt;
// Handle the user data
}
}

4. Transform and combine Flows:

You can use various operators on Flow to transform, filter, and combine multiple Flows. Some common operators include map, filter, zip, and merge.

val transformedFlow = fetchUserData()
.map { user -&gt; user.name }

Now that you understand the basics of using Flow, let's explore a practical example in an Android app.

Sending Internal Notifications in an Android App

In Android app development, you often need to communicate between different modules or components within your app. Using Flow, you can send internal notifications from one module to another without tightly coupling them. Here's how you can achieve this,

Create a Notification Flow:

Define a Flow that represents the notifications you want to send. For example, let's create a simple Flow for sending notifications of new messages:

object MessageNotificationManager {
private val notificationFlow = MutableSharedFlow&lt;Message&gt;()

fun sendNotification(message: Message) {
viewModelScope.launch {
notificationFlow.emit(message)
}
}

fun getNotificationFlow(): Flow&lt;Message&gt; = notificationFlow
}

In this example, MessageNotificationManager provides methods to send notifications and get the Flow to receive notifications.

Subscribe to the Notification Flow:

In the module that needs to receive notifications, subscribe to the Flow.

class ChatFragment : Fragment() {
// ...

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

MessageNotificationManager.getNotificationFlow()
.onEach { message -&gt;
// Handle the new message notification
}
.launchIn(viewLifecycleOwner.lifecycleScope)
}
}

In this example, ChatFragment listens to the notification Flow using onEach and processes incoming messages.

Sending Notifications:

In the module where you want to send notifications, call the sendNotification method.

val newMessage = Message("Hello, world!")
MessageNotificationManager.sendNotification(newMessage)

By using Flow to send and receive internal notifications, you decouple different parts of your Android app, making it more modular and maintainable.

Use Cases for Flow in Android Apps

Flow is a powerful tool for handling asynchronous and non-blocking operations in Android apps. Here are some common use cases for Flow:

  • Data fetching and processing: Use Flow to fetch data from network requests, databases, or other sources asynchronously.

  • Real-time updates: Implement real-time features in your app, such as chat applications or live notifications, using Flow to handle data updates.

  • User interface updates: Update UI components when data changes, ensuring a smooth and responsive user experience.

  • Event handling: Manage and process events, such as button clicks, gestures, or sensor data, using Flow to handle events as they occur.

Conclusion

In Android app development, Flow allows for efficient communication between different modules and components, making your code more modular and maintainable. By following the steps outlined in this blog post and considering the practical examples, you can effectively incorporate Flow into your Kotlin-based Android applications.

IMPLEMENTING AND USING DATA STRUCTURES IN DART

Published: · Last updated: · 5 min read
Robin Alex Panicker
Cofounder and CPO, Appxiom

Dart is a versatile and powerful programming language that has gained popularity for building web, mobile, and desktop applications, thanks to the Flutter framework. To harness its full potential, it's essential to understand and implement various data structures.

In this blog, we'll explore some common data structures and demonstrate how to implement and use them in Dart.

Data Structures in Dart

Data structures are fundamental for organizing and managing data efficiently. Dart provides built-in support for a variety of data structures and allows you to create custom ones. Some common data structures in Dart include:

  • Lists

  • Sets

  • Maps

  • Queues

  • Stacks

  • Trees

  • Graphs

We'll delve into each of these data structures, provide code samples, and discuss their use cases.

Lists

Lists in Dart are ordered collections of objects. They are similar to arrays in other languages and are incredibly versatile. Here's how to create and manipulate lists:

// Creating a List
List&lt;int&gt; numbers = [1, 2, 3, 4, 5];

// Accessing elements
int firstNumber = numbers[0]; // Access the first element (1)

// Modifying elements
numbers[2] = 10; // Update the third element to 10

// Adding elements
numbers.add(6); // Add 6 to the end of the list

// Removing elements
numbers.remove(2); // Remove the element with value 2

// Iterating through a list
for (var number in numbers) {
print(number);
}

Sets

Sets are unordered collections of unique elements. Dart's Set ensures that each element is unique, making them suitable for maintaining unique values:

// Creating a Set
Set&lt;String&gt; uniqueColors = {'red', 'blue', 'green'};

// Adding elements
uniqueColors.add('yellow');

// Removing elements
uniqueColors.remove('red');

// Iterating through a set
for (var color in uniqueColors) {
print(color);
}

Maps

Maps, also known as dictionaries, are collections of key-value pairs. In Dart, maps are implemented using the Map class:

// Creating a Map
Map&lt;String, int&gt; ages = {'Alice': 25, 'Bob': 30, 'Charlie': 22};

// Accessing values
int aliceAge = ages['Alice']; // Access Alice's age (25)

// Modifying values
ages['Bob'] = 31; // Update Bob's age to 31

// Adding new key-value pairs
ages['David'] = 28; // Add a new entry

// Removing key-value pairs
ages.remove('Charlie'); // Remove Charlie's entry

// Iterating through a map
ages.forEach((name, age) {
print('$name is $age years old');
});

Queues

A queue is a data structure that follows the First-In-First-Out (FIFO) principle. In Dart, you can create a simple Queue data structure using a custom class:

class Queue&lt;T&gt; {
List&lt;T&gt; _items = [];

void enqueue(T item) {
_items.add(item);
}

T dequeue() {
if (_items.isNotEmpty) {
return _items.removeAt(0);
}
return null;
}

int get length =&gt; _items.length;
}

You can then use this custom Queue class as follows:

var myQueue = Queue&lt;int&gt;();
myQueue.enqueue(1);
myQueue.enqueue(2);
myQueue.enqueue(3);

print(myQueue.dequeue()); // 1

Queues are useful for tasks that require managing elements in the order they were added, such as task scheduling or breadth-first search in graphs.

Stacks

A stack is another fundamental data structure that follows the Last-In-First-Out (LIFO) principle. While Dart doesn't provide a built-in Stack class, you can easily implement one using a custom class:

class Stack&lt;T&gt; {
List&lt;T&gt; _items = [];

void push(T item) {
_items.add(item);
}

T pop() {
if (_items.isNotEmpty) {
return _items.removeLast();
}
return null;
}

int get length =&gt; _items.length;
}

You can use this custom Stack class as follows:

var myStack = Stack&lt;int&gt;();
myStack.push(1);
myStack.push(2);
myStack.push(3);

print(myStack.pop()); // 3

Stacks are often used for tasks like managing function calls, parsing expressions, and implementing undo/redo functionality in applications.

Trees

Trees are hierarchical data structures with nodes connected by edges. They are commonly used for organizing data, searching, and representing hierarchical relationships. In Dart, you can create tree-like structures by defining custom classes that represent nodes. Here's a basic example of a binary tree:

class TreeNode&lt;T&gt; {
T value;
TreeNode&lt;T&gt; left;
TreeNode&lt;T&gt; right;

TreeNode(this.value);
}

You can then build a tree structure by connecting these nodes. Tree data structures come in various forms, including binary trees, AVL trees, and B-trees, each suited for specific tasks.

Graphs

Graphs are complex data structures that consist of nodes and edges. They are used to represent relationships between objects and solve problems such as network routing, social network analysis, and more. In Dart, you can create a basic graph using a custom class to represent nodes and edges:

class Graph&lt;T&gt; {
Map&lt;T, List&lt;T&gt;&gt; _adjacencyList = {};

void addNode(T node) {
if (!_adjacencyList.containsKey(node)) {
_adjacencyList[node] = [];
}
}

void addEdge(T node1, T node2) {
_adjacencyList[node1].add(node2);
_adjacencyList[node2].add(node1); // For an undirected graph
}

List&lt;T&gt; getNeighbors(T node) {
return _adjacencyList[node];
}
}

void main() {
var graph = Graph&lt;String&gt;();

graph.addNode('A');
graph.addNode('B');
graph.addNode('C');
graph.addNode('D');

graph.addEdge('A', 'B');
graph.addEdge('A', 'C');
graph.addEdge('B', 'D');

print(graph.getNeighbors('A')); // [B, C]
print(graph.getNeighbors('B')); // [A, D]
}

This is a basic implementation of an undirected graph in Dart. You can expand upon this to create more complex graphs and perform various operations.

Conclusion

Understanding and implementing data structures in Dart is essential for efficient and organized data manipulation in your programs.

Lists, Sets, and Maps are the built-in data structures that come in handy for most scenarios, but you can create custom data structures like Queues and Stacks when necessary. Trees and Graphs are more complex data structures that can be implemented through custom classes to solve specific problems.

With this knowledge, you'll be better equipped to tackle a wide range of programming challenges in Dart.

COMPLYING WITH GDPR IN IOS APPS: A COMPREHENSIVE GUIDE

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

The General Data Protection Regulation (GDPR) is a European Union regulation that aims to protect the privacy and personal data of individuals. If your iOS app collects, processes, or stores personal data of EU residents, you must ensure that your app complies with GDPR.

In this guide, we will walk you through the steps to comply with GDPR in your iOS app, complete with Swift code samples.

1. Understand GDPR Principles

Before we dive into the technical aspects, it's crucial to understand the key principles of GDPR:

  • Consent: Users must give informed, explicit consent for their data to be collected and processed.

  • Data Minimization: Only collect and process data that is necessary for your app's functionality.

  • Data Portability: Users have the right to access and transfer their data.

  • Data Security: Implement robust security measures to protect user data.

  • Data Deletion: Allow users to delete their data upon request.

2. Inform Users with a Privacy Policy

Start by providing a clear and concise privacy policy within your app. You can display this during the onboarding process or in the app settings. Ensure that users can easily access and understand your privacy policy.

// Load and display the privacy policy HTML content
if let privacyPolicyURL = URL(string: "https://example.com/privacy-policy.html") {
let request = URLRequest(url: privacyPolicyURL)
webView.load(request)
}

To collect and process user data, you must obtain explicit consent. Create a consent dialog that explains why you need their data and how you will use it. Provide options for users to accept or reject data collection.

// Display a consent dialog
let alertController = UIAlertController(
title: "Data Collection Consent",
message: "We need your data to provide personalized recommendations. Do you consent?",
preferredStyle: .alert
)

let consentAction = UIAlertAction(title: "Consent", style: .default) { _ in
// User consented; start data collection
}

let rejectAction = UIAlertAction(title: "Reject", style: .destructive) { _ in
// User rejected data collection; handle accordingly
}

alertController.addAction(consentAction)
alertController.addAction(rejectAction)
present(alertController, animated: true, completion: nil)

4. Implement Data Minimization

Collect only the data necessary for your app's functionality. Avoid unnecessary data collection to minimize the risk of GDPR violations. Remove any unused data promptly.

5. Secure Data Storage

Protect user data by securely storing it. Use Apple's Keychain Services for sensitive data like passwords and tokens:

import Security

// Store a user's access token securely in the keychain
let accessToken = "user_access_token"
let accessTokenData = accessToken.data(using: .utf8)!
let keychainQuery = [
kSecClass as String: kSecClassGenericPassword as String,
kSecAttrAccount as String: "userAccessToken",
kSecValueData as String: accessTokenData
] as CFDictionary
let status = SecItemAdd(keychainQuery, nil)
if status == errSecSuccess {
print("Access token stored securely.")
}

6. Enable Data Portability

Implement a feature that allows users to access and export their data. Provide options to download data in common formats like JSON or CSV.

// Allow users to export their data
func exportUserData() {
// Prepare user data for export
let userData = ["name": "John Doe", "email": "johndoe@example.com"]

// Convert data to JSON
if let jsonData = try? JSONSerialization.data(withJSONObject: userData, options: []) {
// Offer the JSON file for download
let activityViewController = UIActivityViewController(activityItems: [jsonData], applicationActivities: nil)
present(activityViewController, animated: true, completion: nil)
}
}

7. Implement Data Deletion

Users have the right to request the deletion of their data. Create a feature to allow users to delete their accounts and associated data.

// Delete user account and data
func deleteUserAccount() {
// Perform data deletion// ...// Notify the user
let alertController = UIAlertController(
title: "Account Deleted",
message: "Your account and data have been deleted.",
preferredStyle: .alert
)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
// Handle user acknowledgment
}
alertController.addAction(okAction)
present(alertController, animated: true, completion: nil)
}

8. Handle Data Breaches

In the unfortunate event of a data breach, you must notify both users and authorities as required by GDPR. Implement a mechanism to detect and respond to breaches promptly.

9. Regularly Update and Audit

Stay compliant by regularly updating your app and privacy policy to reflect changes in data handling practices and GDPR regulations. Perform periodic data audits to ensure compliance.

Conclusion

Complying with GDPR in your iOS app is crucial to protect user privacy and avoid legal consequences. By following the principles of consent, data minimization, security, and providing user rights, you can build a GDPR-compliant app. Always stay informed about evolving GDPR regulations and adapt your app accordingly to maintain compliance.

BEST PRACTICES TO AVOID MEMORY LEAKS IN FLUTTER

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

Memory leaks can be a common issue in mobile app development, including Flutter applications. When memory leaks occur, they can lead to reduced performance, increased memory consumption, and ultimately, app crashes. Flutter developers must be proactive in identifying and preventing memory leaks to ensure their apps run smoothly.

In this blog post, we will explore some best practices to help you avoid memory leaks in your Flutter applications, complete with code examples.

1. Use Weak References

One of the most common causes of memory leaks in Flutter is holding strong references to objects that are no longer needed. To prevent this, use weak references when appropriate. Weak references allow objects to be garbage collected when they are no longer in use.

Here's an example of how to use weak references in Flutter:

import 'dart:ui';

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
// Use a weak reference to avoid memory leaks
final _myObject = WeakReference&lt;MyObject&gt;();

@override
void initState() {
super.initState();
// Create an instance of MyObject
_myObject.value = MyObject();
}

@override
Widget build(BuildContext context) {
// Use _myObject.value in your widget
return Text(_myObject.value?.someProperty ?? 'No data');
}
}

2. Dispose of Resources

In Flutter, widgets that use resources such as animations, controllers, or streams should be disposed of when they are no longer needed. Failure to do so can result in memory leaks.

Here's an example of how to dispose of resources using the dispose method:

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
AnimationController _controller;

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

@override
void dispose() {
_controller.dispose(); // Dispose of the animation controller
super.dispose();
}

@override
Widget build(BuildContext context) {
// Use the _controller for animations
return Container();
}
}

3. Use WidgetsBindingObserver

Flutter provides the WidgetsBindingObserver mixin, which allows you to listen for app lifecycle events and manage resources accordingly. You can use it to release resources when the app goes into the background or is no longer active.

Here's an example of how to use WidgetsBindingObserver:

class MyWidget extends StatefulWidget with WidgetsBindingObserver {
@override
_MyWidgetState createState() =&gt; _MyWidgetState();

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
// Release resources when the app goes into the background
_releaseResources();
} else if (state == AppLifecycleState.resumed) {
// Initialize resources when the app is resumed
_initializeResources();
}
}

void _initializeResources() {
// Initialize your resources here
}

void _releaseResources() {
// Release your resources here
}
}

4. Use Flutter DevTools

Flutter DevTools is a powerful set of tools that can help you identify and diagnose memory leaks in your Flutter app. It provides insights into memory usage, object allocation, and more. To use Flutter DevTools, follow these steps:

  • Ensure you have Flutter DevTools installed:
flutter pub global activate devtools
  • Run your app with DevTools:
flutter run
  • Open DevTools in a web browser:
flutter pub global run devtools
  • Use the Memory and Performance tabs to analyze memory usage and detect leaks.

5. Use APM Tools

Even if a thorough testing is done, chances of memory leaks happening in production cannot be ruled out. Use APM tools like Appxiom that monitors memory leaks and reports in real time, both in development phase and production phase.

Conclusion

Memory leaks can be a challenging issue to deal with in Flutter apps, but by following these best practices and using tools like Flutter DevTools and Appxiom, you can significantly reduce the risk of memory leaks and keep your app running smoothly. Remember to use weak references, dispose of resources properly, and manage resources based on app lifecycle events to ensure your Flutter app remains efficient and stable.

Happy Coding!

WHAT ARE THE BEST PRACTICES IN KOTLIN TO AVOID CRASHES IN ANDROID APPS

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

In the world of Android app development, crashes are an unfortunate reality. No matter how well you write your code, there's always a chance that something unexpected will happen on a user's device, leading to a crash. These crashes can result in a poor user experience, negative reviews, and lost revenue. To build robust Android apps, it's crucial to not only prevent crashes but also monitor and report them when they occur.

In this blog post, we'll explore how to avoid crashes in Android apps using Kotlin and how to report crashes using Appxiom, a powerful APM tool.

Avoiding Crashes

1. Null Safety with Kotlin

Kotlin, as a modern programming language, brings a significant advantage to Android development - null safety. Null pointer exceptions (NPEs) are one of the most common causes of app crashes. Kotlin's null safety features, such as nullable types and safe calls, help you prevent NPEs at compile time.

Here's an example of how to use nullable types:

var name: String? = null // Declare a nullable String
name?.length // Safe call: returns null if 'name' is null

By using nullable types and safe calls, you can catch potential null references early in the development process.

2. Exception Handling

While you can't always prevent exceptions, you can handle them gracefully to avoid app crashes. Use try-catch blocks to catch exceptions and provide a fallback or error message to the user.

For example:

try {
// Code that might throw an exception
} catch (e: Exception) {
// Handle the exception, e.g., log it or display an error message
}

By handling exceptions properly, you can prevent crashes and provide a better user experience.

3. Defensive Programming

Adopt defensive programming practices by validating inputs, using assertions, and adding proper checks throughout your code. For instance, when accessing an array or list, ensure that you're within the bounds to avoid index out of bounds exceptions.

val list = listOf(1, 2, 3)
if (index &gt;= 0 &amp;&amp; index &lt; list.size) {
val item = list[index]
// Use 'item' safely
} else {
// Handle the out-of-bounds condition
}

4. Robust API Calls

When making network requests or interacting with external services, always assume that the network may fail or the data may be invalid. Implement retry mechanisms, timeouts, and data validation to handle unexpected scenarios gracefully.

Reporting Crashes with Appxiom

Even with the best preventative measures, crashes may still occur. When they do, it's essential to gather detailed information about the crash to diagnose and fix the issue. Appxiom is a powerful tool for crash reporting and analysis.

1. Integrating Appxiom into Your App

To get started, sign up for a Appxiom account. Then, add the Appxiom SDK to your Android project. You can do this by adding the following dependency to your app's build.gradle file:

dependencies {
implementation 'com.appxiom:appxiomcore:x.x.x'
}

Initialize Appxiom in your app's Application class:

import android.app.Application

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
Ax.init(this)
}
}

2. Capturing and Reporting Crashes

Appxiom automatically captures crashes and unhandled exceptions in your app. When a crash occurs, it collects valuable information, including the stack trace, device details, and user actions leading up to the crash.

You can also manually report non-fatal errors and exceptions using the following code:

try {
// Code that might throw a non-fatal exception
} catch (e: Exception) {
Ax.reportException(this, e, Severity.MAJOR)
}

For more on how to use Appxiom to detect crashes and other issues like memory leak and frame rate issues, check the Appxiom documentation at https://docs.appxiom.com.

3. Analyzing Crash Reports

Once crashes are reported to Appxiom, you can log in to your Appxiom dashboard to view and analyze crash reports. Appxiom provides detailed insights into the root cause of crashes, enabling you to prioritize and fix issues quickly. You can see stack traces, device information, and the activity trail of the user that led to the crash.

Conclusion

Building crash-resilient Android apps is a critical aspect of delivering a positive user experience. By following best practices in Kotlin for avoiding crashes and integrating crash reporting and analysis tools like Appxiom, you can significantly reduce the impact of crashes on your app and ensure that your users have a smooth and trouble-free experience.

Remember that continuous monitoring and improvement are essential for maintaining the reliability of your Android app.