lecture 1 - Mobile apps design.pdf
Document Details
Uploaded by LegendaryDecagon
2024
Tags
Full Transcript
CSIT242Mobile Application Development LECTURE 1 - 2 – MOBILE APPLICATION DESIGN 1 CSIT242 – SIM 2024 Lecture Plan 1. Design patterns 2. Mobile application life cycle 3. App’s components/objects 2 CSIT242 – SIM 2024 App architecture design Good architecture design involves: modularity well structured...
CSIT242Mobile Application Development LECTURE 1 - 2 – MOBILE APPLICATION DESIGN 1 CSIT242 – SIM 2024 Lecture Plan 1. Design patterns 2. Mobile application life cycle 3. App’s components/objects 2 CSIT242 – SIM 2024 App architecture design Good architecture design involves: modularity well structured reusability no need to design your app code every time from zero efficiency fast and responsive Concerns testability maintenance if you release your app on google store, you need to maintain it and keep updating it Challenges limited computing capability and resources (CPU, memory, screen size, battery) 3 CSIT242 – SIM 2024 Design patterns: Model-View-Controller (MVC) Programming paradigm for iOS and Android application. Splits the application into manageable parts and assigns objects in an application in one of three roles: Model View displaying Controller reactions and behaviours of your application Model, View and controller each do their own part Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer. MVC is central to a good design for a Cocoa application. Many objects in these applications tend to be more reusable, and their interfaces tend to be better defined. Knows which role to focus more on based on problem Applications having an MVC design are also more easily extensible than other applications. Moreover, many Cocoa technologies and architectures are based on MVC and require that your custom objects play one of the MVC roles. 4 CSIT242 – SIM 2024 Design patterns: Model-View-Controller (MVC) based on the user action, the controller will update the model from the user action, the controller will know what to do most important because intermediate between view and model and help them communicate Model is data. For example, when messaging your friend, the controller updates the model and saves them in the database. User can interact with the view(screen/ layout) like swiping, messaging your friends, etc. Then the controller will update the screen for the user to see the new message that you just sent on your screen Model will notify the controller that it has received data and stored in such space. source: https://developer.apple.com/library/content/documentation/General/Conceptual/DevPediaCocoaCore/MVC.html 5 CSIT242 – SIM 2024 Design patterns: Model-View-Controller (MVC) Model Objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data (for example, a model object might represent a character in a game or a contact in an address book). Everything about the data is included in the model role like processing data, manipulating data, etc. A model object can have to-one and to-many relationships with other model objects. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Model objects represent knowledge and expertise related to a specific problem domain, so they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues. User actions in the view layer that create or modify data are communicated through a controller object and result in the creation or updating of a model object. When a model object changes, it notifies a controller object, which updates the appropriate view objects. 6 CSIT242 – SIM 2024 Design patterns: Model-View-Controller (MVC) View Objects is an object in an application that users can see. A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data. Despite this, view objects are typically decoupled from model objects in an MVC application. Because we reuse and reconfigure them, view objects provide consistency between applications. Both the UIKit and Android UI frameworks provide collections of view classes, and their respective Interface Builders offers dozens of view objects in its Library. View objects learn about changes in model data through the application’s controller objects and communicate user-initiated changes - for example, text entered in a text field - through controller objects to an application’s model objects. 7 CSIT242 – SIM 2024 Design patterns: Model-View-Controller (MVC) Controller Objects acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects. A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it. 8 CSIT242 – SIM 2024 Design patterns: Model-View-Presenter (MVP) Model View Presenter (MVP) is derived from MVC pattern. MVP is somewhat able to minimize high dependency on View as it separates view layer from business logic layer by presentation layer. Presentation layer decides what to be displayed on view. View - renders information to users and contains UI Component.xml file, Activity, fragments, Dialog.Views are what’s displayed to the user. They also handle any interaction a user may have with the screen (for example click listeners). It should not contain any business logic. Model - plays role of domain or business layer and is data source of pattern. It describes the main logic of application and decides from where the data should be fetched. Presenter - performs the functionality of Controller and act as middle layer between view and model layer. But unlike controller, it is not much dependent on View. View contacts presenter for the data to be presented, then Presenter takes data from model and returns to view in presentable format. Presenter is, for example, a simple java class (interface) that do not contain any UI components, it just manipulates data from model and displays it on view. MVC is more dependent on view 9 CSIT242 – SIM 2024 Design patterns: MVC/MVP Why use MVC/MVP? Models and views become reusable Separating code makes testing easier check if the model(data) is correct then the UI(view) then combine them App is easier to maintain and extend over time if you want to change the data or add in data, go to the model, if you want to change the view, go to view, etc. so it is easier to extend MVVM Other design patterns? 10 CSIT242 – SIM 2024 Design patterns: Delegation if you mean you delegate your work to someone else, they will do the work for you and you don't have to work anymore. Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object when that object encounters an event in a program. The delegating object keeps a reference to the other object - the delegate - and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows us to easily customize the behavior of several objects in one central object. 11 CSIT242 – SIM 2024 Design patterns: Delegation We can also define Delegation as – objects talking to objects! Delegation is implemented using “protocols” and “delegates” in Swift “interfaces” and “implements” in Java 12 CSIT242 – SIM 2024 Android Application Architecture An Android application consists of many “moving parts” whose natures and interactions need to be understood in order to develop effectively. An Android application consists of one or more of the following components, written as Java or Kotlin classes: Activity Service Broadcast receivers Content providers FP - explained in fast pace 13 CSIT242 – SIM 2024 Android – Application Components Activity - is a single, standalone module of application functionality that usually correlates directly to a single user interface screen and its corresponding functionality An Activity comprises the visual components (“views”) for one screen as well as the code that displays data into that screen and can respond to user events on that screen. Activities are created as subclasses of the Android Activity class and must be implemented so as to be entirely independent of other activities in the application. One activity cannot directly call methods or access instance data of another activity. Fragment - is a section of an activity Each fragment (subclass of the Android Fragment class) consists of part of the user interface layout and a matching class file. A Fragment can only exist inside an Activity. A single activity can switches between different fragments, each representing a different app screen. 15 CSIT242 – SIM 2024 Android – Application Components Service - is a component that runs in the background to perform long running operations or to perform work for remote processes. A service does not provide a user interface. playing music, or downloading files in the background Although services lack user interface, they can still notify the user by providing notifications and toasts. Services can issue intents. Service can be declared to run in the foreground - only for situations where example, when you're charging your phone and the termination would be detrimental to the user experience.For temperature gets high so it alerts you in the foreground. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity. 16 CSIT242 – SIM 2024 Android – Application Components Broadcast Receiver - is a component that enables the system to deliver events to the app outside of a regular user flow. Many broadcasts originate from the system - for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. It allows the app to respond to system-wide broadcast announcements. Apps can also initiate broadcasts - for example, to let other apps know that some data has been downloaded to the device and is available for them to use. Broadcast receivers don’t display user interface, they can create status bar notifications. Content providers are used when one application needs to share its data with other applications. Sync adapters synchronize data with cloud services; the best-known examples are the Contacts and Calendar apps on the device, which can easily be synchronized to your Google account. Since it is synchronized, you can easily retrieve data and may get same notification from different devices. 17 CSIT242 – SIM 2024 Android – Activating Components Creation of these objects requests using an Intent, an object that specifies your intention to have something done. Intents can start Activities within our application (by class name), start Activities in other applications (by specifying type of action and other information), start Services or Broadcast receiver, and request other operations. Source: Android Cookbook (2nd edition), Ian Darwin, O’Reilly Media, 2017. 18 CSIT242 – SIM 2024 Android – Activating Components Activities, services, and broadcast receivers - are activated by an Intent. Content provider, is not activated by intents. Rather, it is activated when targeted by a request from a ContentResolver. The content resolver handles all direct transactions with the content provider. This leaves a layer of abstraction between the content provider and the component requesting information (for security). FP 19 CSIT242 – SIM 2024 Android – Intents and Intent Filters An Intent is a messaging object used to request an action from another app component. There are three fundamental use-cases: To start an activity - a new instance of an Activity can be started by passing an Intent to startActivity(); To start a service - a service can be started by passing an Intent to startService(); To deliver a broadcast - the system delivers various broadcasts for system events (for example when the system boots up or the device starts charging). 20 CSIT242 – SIM 2024 Android – Intents and Intent Filters Explicit intents specify the component to start by name (the fullyqualified class name). Typically is used to start a component with-in the app. For example, start a new activity in response to a user action or start a service to download a file in the background. Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, to take a photo, the app can use an implicit intent to request another app to take the photo. 21 CSIT242 – SIM 2024 Android – Intents and Intent Filters An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. By declaring an intent filter for an activity, it is possible for other apps to directly start that activity with a certain kind of intent. If the intent filter(s) is not declared for an activity, then that activity can be started only with an explicit intent. The power of intents lies in the concept of implicit intents – it allows the system to find a component on the device that can perform the action and start it. If there are multiple components that can perform the action described by the intent, then the user selects which one to use. Caution: For security always use an explicit intent when starting a Service and do not declare intent filters for your services. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent. 22 CSIT242 – SIM 2024 Android Activity Life Cycle Activity is the most basic component of Android application. The class android.app.Activity provides a number of well-defined life-cycle methods that are called when an application is started, suspended, restarted,... An Android app can be in one of three states: open the app Active - in which the app is visible to the user and is running; Paused - in which the app is partly obscured and has lost mouse cursor. the input focus (e.g. when a dialog is in front of app’s Activity); when playing a game and a phone call comes in Stopped - in which the app is completely hidden from view. Android apps do not have a “main” method 23 CSIT242 – SIM 2024 Source: https://developer.android.com/guide/components/activities/activity-lifecycle Android – The Manifest File it tells the app store the manifest of your application so that the store can decide whether your app should be on the store Before the Android system can start an app component, the system must know that the component exists by reading the app's AndroidManifest.xml file. The app must declare all its components in this file, which must be at the root of the app project directory. The manifest does a number of things in addition to declaring the app's components, such as: Identify any user permissions the app requires, such as Internet access or readaccess to the user's contacts. Declare the minimum API Level required by the app. Declare hardware and software features used or required by the app, such as a camera, Bluetooth services, or a multitouch screen. API libraries the app needs to be linked against (other than the Android framework APIs), such as the Google Maps library. … 24 CSIT242 – SIM 2024 Android – App Resources An Android app requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the app. For example, animations, menus, styles, colours, and the layout of activity user interfaces defined with XML files. Using app resources makes it easy to update various characteristics of the app without modifying code. For every resource, the SDK build tools define a unique integer ID, which can be used as reference to the resource from the app code or from other resources defined in XML. For example, if the app contains an image file named appimage.jpg (saved in the res/drawable/ directory), the SDK tools generate a resource ID named R.drawable.appimage, which can be used as reference to the image and insert it in the user interface. reusable 25 CSIT242 – SIM 2024 Android – App Resources strings.xml is the default resource file used to hold name/value pairs of strings so that they can be referenced throughout the app. Source: Head First Android Development, Dawn Griffiths and David Griffiths, O’Reilly Media, 2017. 26 CSIT242 – SIM 2024 Android – Application Fundamentals Android apps are written in the Java or Kotlin programming language. The Android SDK tools compile app’s code along with any data and resource files into an APK: an Android package, which is an archive file with an.apk suffix. One APK file contains all the contents of an Android app and is the file that Androidpowered devices use to install the app. Once installed on a device, each Android app lives in its own security sandbox: The Android operating system is a multi-user Linux system in which each app is a different user. Each app has a unique Linux user ID (used only by the system and is unknown to the app). The system sets permissions for all the files in an app so that only the user ID assigned to that app can access them. Each process has its own virtual machine (VM), so an app's code runs in isolation from other apps. Android starts the process when any of the app's components need to be executed, then shuts down the process when it's no longer needed or when the system must recover memory for other apps. 27 CSIT242 – SIM 2024 Android – Application Fundamentals Security of Android Apps: Android system implements the principle of least privilege - each app has access only to the components that it requires to do its work and no more. An app cannot access parts of the system for which it is not given permission. An app can share data with other apps and can access system services: permission to use camera If two apps share the same Linux user ID - they are able to access each other's files. The apps with the same user ID can also arrange to run in the same Linux process and share the same VM. An app must request permission to access device data (user's contacts, SMS messages, SD card, camera, Bluetooth, …). 28 CSIT242 – SIM 2024 Android – Declaring app requirements There are a variety of devices powered by Android and not all of them provide the same features and capabilities. The profile for the types of devices and software requirements should be declared in the manifest file - it will prevent the app to be installed on devices that lack needed features. Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read it in order to provide filtering for users when they search for apps from their device. 29 CSIT242 – SIM 2024 Android Application Development Workflow Setup setting up your Android Studio and install SDK Creating AVD – target device emulator Development / Write Creating application Layout and code/activities Deciding on services to use fp Source: http://www.androiddocs.com/tools/workflow/index.html 30 CSIT242 – SIM 2024 Android Application Development Workflow Build and run / Debugging and Testing Use of AVD and debugging tools Testing and performance tune Publishing Prepare for release Maintenance and decision for marketing… fp Source: http://www.androiddocs.com/tools/workflow/index.html 31 CSIT242 – SIM 2024 Android APIs API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform. The Android platform provides a framework API that applications can use to interact with the underlying Android system. The framework API consists of: A core set of packages and classes, A set of XML elements and attributes for declaring a manifest file, A set of XML elements and attributes for declaring and accessing resources, A set of Intents, A set of permissions that applications can request, as well as permission enforcements included in the system. fp 32 CSIT242 – SIM 2024 iOS App and Environment During startup, the UIApplicationMain function sets up several key objects and starts running the app: creating the core objects of the app, loading app’s user interface (from the available storyboard files), calling custom code so some initial setup can be done, putting the app’s run loop in motion. In iOS 13 and later, the user can create and manage multiple instances of the app's user interface simultaneously switch between them using the app switcher, or on iPad, multiple instances of the app can be displayed side by side. Each instance of the UI displays different content, or displays the same content in a different way. iOS apps do have a “main” method in Objective-C or UIApplicationMain function - @UIApplicationMain special attribute in Swift. 33 CSIT242 – SIM 2024 iOS App and Environment Lunching the app skip In iOS 12 and before UIApplicationMain: instantiates UIApplication that serve as the shared application instance; instantiates the app delegate class - the application instance’s delegate; If the app uses a main storyboard instantiates the main storyboard’s initial view controller, instantiates UIWindow (assigns the window instance to the app delegate’s window property), assigns the initial view controller instance to the window’s rootViewController property; calls the app delegate’s application(_:didFinishLaunchingWithOptions:); calls the window’s instance method makeKeyAndVisible (to make the app's interface visible). 34 CSIT242 – SIM 2024 In iOS 13 and later UIApplicationMain instantiates UIApplication that serve as the shared application instance; instantiates the app delegate class - the application instance’s delegate; calls the app delegate’s application(_:didFinishLaunchingWithOptions:); creates a UISceneSession, a UIWindowScene, and an instance that will serve as the window scene’s delegate; If the initial scene uses a storyboard: instantiates that storyboard’s initial view controller, instantiates UIWindow (assigns the window instance to the scene delegate’s window property), assigns the initial view controller instance to the window instance’s rootViewController property; calls the window’s instance method makeKeyAndVisible (to make the app's interface visible). The scene delegate’s scene(_:willConnectTo:options:) is called. iOS App’s objects iOS13 additional objects - UIWindowScene - UIWindowSceneDelegate Source: https://developer.apple.com 35 CSIT242 – SIM 2024 iOS App’s objects Every iOS app has exactly one instance of UIApplication object that handle the initial routing of incoming user events, dispatches action messages forwarded to it by control objects to appropriate target objects, maintains a list of open windows (UIWindow objects) and through those can retrieve any of the app’s UIView objects. The app delegate object (protocol UIApplicationDelegate) is the root object of the app that manages app’s shared behaviors: initialize app’s central data structures, configure app’s scenes, respond to notifications originating from outside the app (e.g. low-memory warnings), respond to events that target the app itself, and are not specific to the app’s scenes, views, or view controllers, register any required services at launch time (e.g. Apple Push Notification service). 36 CSIT242 – SIM 2024 iOS App’s objects UIWindow work with app’s view controllers to handle events and to perform many tasks that are fundamental for app’s operation: Provide a main window to display app’s content, Create additional windows (as needed) to display additional content. A UIWindowScene object manages one instance of the app's UI, including one or more windows that are displayed from that scene: manages the display of the windows on the user's device, and the life cycle of that scene as the user interacts with it. UIWindowSceneDelegate object manage the life cycle of one instance of the app's user interface, respond to changes in the underlying environment of the scene. UISceneDelegate object manage life-cycle events in one instance of the app's user interface, defines methods for responding to state transitions that affect the scene, including when the scene enters the foreground and becomes active, and when it enters the background. 37 CSIT242 – SIM 2024 iOS App’s objects Data model objects store app’s content and are specific for every app. Apps can also use Document objects (custom subclasses of UIDocument) to manage discrete portions of app’s data Document objects are not required but offer a convenient way to group data that belongs in a single file or file package. View Controller objects manage the presentation of the app’s content on screen Views and controls provide the visual representation of your app’s content. 38 CSIT242 – SIM 2024 iOS - App’s Life Cycle Scene-Based Life-Cycle Events If the app supports scenes - UIKit delivers separate life-cycle events for each. Source: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle 39 CSIT242 – SIM 2024 iOS - App’s Life Cycle App-Based Life-Cycle Events In iOS 12 and earlier, and in apps that don't support scenes - UIKit delivers all life-cycle events to the UIApplicationDelegate object. Source: https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle 40 CSIT242 – SIM 2024 iOS App resources and metadata Every app must have the following set of resources and metadata so that it can be displayed properly on iOS devices: An information property-list file - The Info.plist file contains metadata about the app, which the system uses to interact with the app. (Xcode creates this file automatically based on the project’s configuration and settings.) A declaration of the app’s required capabilities - Every app must declare the hardware capabilities or features that it requires to run. The App Store uses this information to determine whether or not a user can run the app on a specific device. One or more icons - The system displays the app icon on the home screen of a user’s device. Launch Screen - When an app is launched, the system displays a temporary lunch screen until the app is able to present its user interface. When you build your iOS app, Xcode packages it as a bundle. A bundle is a directory in the file system that groups related resources together in one place. An iOS app bundle contains the app executable file and supporting resource files such as app icons, image files, and localized content. 41 CSIT242 – SIM 2024 Resources Android Studio 4.1 Essentials - Java Edition, Neil Smyth, Payload Media Inc., 2020. Android Cookbook (2nd edition), Ian Darwin, O’Reilly Media, 2017. Programming iOS 13: Dive Deep into Views, View Controllers, and Frameworks, Matt Neuburg, O'Reilly Media Inc., 2020. Programming iOS 14: Dive Deep into Views, View Controllers, and Frameworks, Matt Neuburg, O'Reilly Media Inc., 2020. iOS 12 App Development Essentials, Neil Smyth, Payload Media, Inc., 2018. https://developer.apple.com/library/content/documentation/General/Conceptual/DevPediaCocoaCore/MVC.html https://developer.apple.com/library/content/documentation/General/Conceptual/DevPediaCocoaCore/Delegation.html https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app _s_life_cycle https://developer.android.com/guide/components/fundamentals http://www.androiddocs.com/tools/workflow/index.html 42 CSIT242 – SIM 2024