- What is Kotlin Multiplatform?
- KMP uses Kotlin as programming language and supports MultiPlatform
- KMP supports
- Android
- iOS - macOS - watchOS - tvOS
- Windows
- Linux
- Web
- Kotlin already works on platform that support JVM
- for non JVM platforms it uses the Kotlin Native.
- Kotlin Native compiles Kotlin to Native bytecode that can work natively on Apple OS.
- What is difference between KMP and KMM?
- KMP means we write code using Kotlin programming lanaguage for Multiple Platforms.
- KMM is specially for crossplatform mobile application development.
- KMP includes : Mobile , Desktop, Web and Server
- KMM : Only include the Mobile
- What is the history of Cross-platform? Web Based Frameworks
-
PhoneGap : HTML5, CSS3 and JavaScript
-
Apache Cordova : Fork of PhoneGap and opensource
-
Ionic : Angular, React and Vue UI frameworks
-
Appcelerator Titanium : JavaScript based SDK ..
-
Xamarin : Microsoft C# , .NET runtime , JIT compilation
-
React Native : JavaScript and Web based
-
Flutter : Dart
- Why we should use KMM?
- uses Kotlin programming language
- Compile both JVM and Native platforms
- Ideal also for Server Development with Ktor library
- Kotlin is similar to Swift
- Shared Business Logic both for iOS and Android
- for iOS you use : SwiftUI
- for Android you use : Jetpack Compose
- for Desktop you can use : Compose Multiplatfom
- It is 100% native because UI are created natively only business logic is shared.
-
How to setup your Environment for KMM Application Development?
-
What are different layers of the ANY APP ?
- NETWORK LAYER
- DATABASE LAYER
- REPOSITORY LAYER (interacting with DB or Network layers)
- BUSINESS LOGIC LAYER
- UI LAYER
- What is Business Logic layer in terms of KMM?
- iOS Business Logic
- Android Business Logic
- set of specifications : and each team has to implement them
- finally they have to test each edge cases and make sure that both platform follows the specifications.
- With KMM we have only one business logic and you need to write the test just once.
- Database Layer
- SQLDelight Library
- multiplatfom library designed for all platforms.
- also you need to write the test just once. [https://cashapp.github.io/sqldelight/2.0.0/]
- KMM UI Layer
- SwiftUI for iOS
- Jetpack Compose for Android
- Jetbrains DateTime Library [https://github.com/Kotlin/kotlinx-datetime]
- multiplatform
- working with date and time
- Napier Loggin Library
- [https://github.com/AAkira/Napier]
- logging library
-
How do you create a common business logic in KMM?
-
Understanding Android Jetpack Compose
- is a declarative UI System i.e. it uses the function to create complete UI or parts of the UI
- reduced boiler plate codes
- the UI is drawn with current State no listen for changes is required
- One Entry Point : Activity
- no fragment is required
- setContent{}
- no xml files is required
- @Composable annotation this tells the Jetpack Compose that this function can be drawn on screen.
- no composable function returns a value
- Theming in Jetpack Compose
- Material Compose Library : so we can use MaterialTheme class
- set our colors, fonts and shapes
- also check the system is using dark theme or light theme
- How to run your iOS App from Android Studio in your KMM App?
- select iOS Configuration from EditConfigurations
- Select your Execution Target.
-
What is XCode?
-
Let's understand UI System in iOS.
- StoryBoard
- UIKit
- SwiftUI
"UIKit," "Storyboard," and "SwiftUI" are all terms related to user interface development in the context of iOS and macOS app development using the Swift programming language. Let me explain each of them:
-
UIKit: UIKit is a framework provided by Apple for building user interfaces in iOS and macOS applications. It's been around since the early days of iOS development and provides a collection of classes, views, and controllers that you can use to create your app's user interface. With UIKit, you write code to define and customize UI elements, layout, and behavior. It's a procedural approach to building interfaces.
-
Storyboard: A storyboard is a visual interface builder tool provided by Apple as part of the Xcode development environment. It allows developers to design their app's user interface using a graphical interface rather than writing all the interface code manually. Storyboards enable you to create and connect view controllers, design the layout of screens, and define the flow of navigation between different screens. Storyboards are tightly integrated with UIKit.
-
SwiftUI: SwiftUI is a newer declarative UI framework introduced by Apple. It aims to simplify the process of creating user interfaces by allowing developers to describe the user interface and its behavior using a declarative syntax. With SwiftUI, you define the interface's structure and appearance using a series of views and modifiers. SwiftUI automatically handles many aspects of layout, animation, and responsiveness. It's designed to work across all Apple platforms, including iOS, macOS, watchOS, and tvOS.
Key Differences:
-
Approach:
- UIKit: Requires writing code to define the UI, layout, and behavior. It's a more procedural approach.
- Storyboard: Allows for visual design of interfaces and navigation flows, reducing the need for some code.
- SwiftUI: Uses a declarative approach, where you describe the UI's structure and appearance using code. It aims to minimize the amount of code needed.
-
Complexity:
- UIKit: Can be more complex and require more code for certain tasks.
- Storyboard: Simplifies UI design and navigation flow but can become complex for larger apps with multiple screens.
- SwiftUI: Aims to reduce complexity by abstracting away many of the manual tasks involved in UI development.
-
Flexibility:
- UIKit: Offers a high degree of customization and control over UI elements and behavior.
- Storyboard: Provides a visual way to configure UI components and their connections.
- SwiftUI: Balances customization with built-in behaviors, making it easier to achieve certain common UI patterns.
-
Adoption:
- UIKit: Has been the standard for iOS UI development for many years.
- Storyboard: Widely adopted for creating interfaces, especially in smaller projects.
- SwiftUI: Gaining traction and popularity due to its modern approach and cross-platform capabilities.
In recent years, SwiftUI has gained significant attention due to its streamlined approach and capabilities, while UIKit and storyboards remain relevant for developers who have existing projects or prefer more fine-grained control over their interfaces. Your choice between these options would depend on factors like project requirements, personal preference, and the platform you're targeting.
- Some key concepts of SwiftUI
- @main tag : starting point
- App protocol
- var name body : retruns a Scene
- Scene is a container for the root view of a view hierarchy
- WindowGroup is a Scene
- on iOS you can contain only one window, but on macOS and iPadOS it can contain multiple windows-
-
Commands for Xcode /** 3 important commands in Xcode
-
Command + B -> to build your application
-
Command + R -> to run your applicaiton
-
Command + N -> to create a new file */
-
What is State in SwiftUI?
- @State is used with simple struct types
- it saves states between redraws of screens
- any @State property wrapper meanse that the current view owns this data.
- SwiftUI will keep track of this @State variable changes and redraws whenever it's value changes.
- What is NavigationView?
NavigationViewis a SwiftUI container view that's specifically designed for managing navigation and creating navigation hierarchies within an app's user interface. It's part of the SwiftUI framework, introduced by Apple as a modern way to create user interfaces in a declarative manner.NavigationViewis particularly useful when you want to implement navigation between different views or screens within your app.
With NavigationView, you can create a navigation interface
similar to what you might find in a navigation controller
in UIKit or a UINavigationController in iOS development.
It provides a navigation bar at the top of the screen that
typically contains a title, navigation controls, and back buttons,
making it easier for users to move between different content.
Here's a simple example of how you might use NavigationView:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List {
NavigationLink("First Screen", destination: Text("This is the first screen"))
NavigationLink("Second Screen", destination: Text("This is the second screen"))
}
.navigationBarTitle("Navigation Example")
}
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}In this example, the NavigationView contains a List of items,
each wrapped in a NavigationLink. When a user taps on one of the links,
they'll be taken to a destination view. The navigationBarTitle
modifier sets the title of the navigation bar.
NavigationView also provides support for navigation stacks,
allowing you to push and pop views onto and off the navigation hierarchy,
similar to how you would with a UINavigationController.
It automatically handles the navigation bar and back button behavior for you.
In summary, NavigationView in SwiftUI is a powerful tool for
managing navigation flows and creating hierarchies of views within
your app's user interface. It simplifies the process of creating
navigation-based user experiences and integrates well with other
SwiftUI components.
- What is VStack?
- is a continer view in SwiftUI that arranges its child views vertically in a column.
- VStack stands for Vertical Stack
- Lets learn about List and ForEach?
- ForEach is used to iterate over a collection of data and create views dynamically.
struct Item: Identifiable { let id: UUID let name: String let description: String }
let items: [Item] = [ Item(id: UUID(), name: "Item 1", description: "Description 1"), Item(id: UUID(), name: "Item 2", description: "Description 2"), // Add more items as needed ]
import SwiftUI
struct ContentView: View { var body: some View { NavigationView { List(items) { item in NavigationLink(destination: DetailView(item: item)) { Text(item.name) } } .navigationBarTitle("Items") } } }
struct DetailView: View { let item: Item
var body: some View {
VStack {
Text(item.name)
Text(item.description)
}
.navigationBarTitle(item.name)
}
}
@main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
-
How to add ToolbarItem in SwiftUI NavigationView? .toolbar{ ToolbarItem(placement: .navigationBarLeading){ Button("Edit"){ // handle your edit action here } } // Toolbar ToolbarItem( placement: .navigationBarTrailing ){ Button(action: { // do sth }){ Image(systemName: "plus") .frame(alignment:.trailing) .foregroundColor(.black) } }// ToolbarItem
}// toolbar -
How to use fullScreenCover for presenting your view in full-screen mode? The
fullScreenCovermodifier in SwiftUI allows you to present a view in full-screen mode that covers the entire screen. It's often used for modal presentations, such as displaying a form, settings, or other temporary views. Here's an example of how to usefullScreenCover:
import SwiftUI
struct ContentView: View {
@State private var isPresentingModal = false
var body: some View {
Button("Present Modal") {
isPresentingModal = true
}
.fullScreenCover(isPresented: $isPresentingModal) {
ModalView(dismissAction: {
isPresentingModal = false
})
}
}
}
struct ModalView: View {
let dismissAction: () -> Void
var body: some View {
VStack {
Text("This is a modal view")
Button("Dismiss") {
dismissAction()
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
}
}
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}In this example:
- The
ContentViewhas a button that triggers the presentation of the modal view. - The
fullScreenCovermodifier is used on the button. It takes a binding to a Boolean value (isPresented) that controls whether the modal view is displayed. - When the
isPresentedvalue becomestrue, theModalViewis presented in full-screen mode. - The
ModalViewcontains a dismiss button that, when tapped, sets theisPresentingModalvalue back tofalse, which dismisses the modal view.
The ModalView is presented in full-screen mode and covers the entire screen.
You can customize the content of the modal view as needed.
Remember that fullScreenCover is available in iOS 15 and later.
If you're using an earlier version of iOS, you might need to use
other presentation methods such as sheet or presentationMode.
-
How to create a simple CardView in SwiftUI?
-
Using @EnvironmetalObject In SwiftUI, the
@EnvironmentObjectproperty wrapper is used to share data between different views in your app's hierarchy without passing the data explicitly. It relies on anObservableObjectbeing injected into the environment of a view, allowing any child view to access and observe changes to that object.
Here's an example of how to use @EnvironmentObject to share data between
views:
- Define an ObservableObject class:
import SwiftUI
import Combine
class UserData: ObservableObject {
@Published var username: String = ""
}- Set up the environment object in your app's entry point
(usually in the
Appstruct):
@main
struct MyApp: App {
let userData = UserData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(userData) // Inject the environment object
}
}
}- Use
@EnvironmentObjectin your views:
struct ContentView: View {
@EnvironmentObject var userData: UserData
var body: some View {
VStack {
Text("Welcome, \(userData.username)!")
.padding()
EditUsernameView()
}
}
}
struct EditUsernameView: View {
@EnvironmentObject var userData: UserData
var body: some View {
TextField("Enter your username", text: $userData.username)
.padding()
}
}In this example:
- The
UserDataclass is anObservableObjectthat contains a@Publishedpropertyusername. - In the
MyAppstruct, theUserDatainstance is injected into the environment using the.environmentObjectmodifier. - Both
ContentViewandEditUsernameViewaccess theuserDataobject using the@EnvironmentObjectproperty wrapper. - When the
usernameproperty changes, the@Publishedattribute triggers an update to the views that observe it, causing automatic UI updates.
This pattern is especially useful for sharing data across multiple views without needing to pass it through each level of the view hierarchy. It's a fundamental mechanism for managing and syncing shared data throughout your SwiftUI app.
-
with Examples I am gonna explain you how you structure your code according to KMP's suggested approach to handle Platform Specific APIs
-
how we reuse code between platforms ??
-
expect / actual mechanism
-
expect : think as Interface in Android & protocol in Swift
-
define classes, properties and function/methods using expect
-
actual implementation in each platform
- Compose Navigation added [https://developer.android.com/jetpack/compose/navigation]