mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-06-08 02:37:13 +00:00
Upload of pre-existing files
This commit is contained in:
commit
4c21152830
150 changed files with 730703 additions and 0 deletions
14
iOS/App (XCode 12).md
Normal file
14
iOS/App (XCode 12).md
Normal file
|
@ -0,0 +1,14 @@
|
|||
# App.swift
|
||||
|
||||
```swift
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct <Project>App: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
75
iOS/AppDelegate (XCode 11) .md
Normal file
75
iOS/AppDelegate (XCode 11) .md
Normal file
|
@ -0,0 +1,75 @@
|
|||
# AppDelegate
|
||||
|
||||
First loaded file. It prepares the app visualiation by calling the *scene* UI.
|
||||
|
||||
```swift
|
||||
import UIKit
|
||||
|
||||
@UIApplicationMain
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
// Override point for customization after application launch.
|
||||
return true
|
||||
}
|
||||
|
||||
// MARK: UISceneSession Lifecycle
|
||||
|
||||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
|
||||
// Called when a new scene session is being created.
|
||||
// Use this method to select a configuration to create the new scene with.
|
||||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
|
||||
// Called when the user discards a scene session.
|
||||
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
|
||||
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
|
||||
}
|
||||
|
||||
// MARK: - Core Data stack
|
||||
|
||||
lazy var persistentContainer: NSPersistentContainer = {
|
||||
/*
|
||||
The persistent container for the application. This implementation
|
||||
creates and returns a container, having loaded the store for the
|
||||
application to it. This property is optional since there are legitimate
|
||||
error conditions that could cause the creation of the store to fail.
|
||||
*/
|
||||
let container = NSPersistentContainer(name: "CoreDataExample")
|
||||
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
|
||||
if let error = error as NSError? {
|
||||
// Replace this implementation with code to handle the error appropriately.
|
||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||
|
||||
/*
|
||||
Typical reasons for an error here include:
|
||||
* The parent directory does not exist, cannot be created, or disallows writing.
|
||||
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
|
||||
* The device is out of space.
|
||||
* The store could not be migrated to the current model version.
|
||||
Check the error message to determine what the actual problem was.
|
||||
*/
|
||||
fatalError("Unresolved error \(error), \(error.userInfo)")
|
||||
}
|
||||
})
|
||||
return container
|
||||
}()
|
||||
|
||||
// MARK: - Core Data Saving support
|
||||
|
||||
func saveContext () {
|
||||
let context = persistentContainer.viewContext
|
||||
if context.hasChanges {
|
||||
do {
|
||||
try context.save()
|
||||
} catch {
|
||||
// Replace this implementation with code to handle the error appropriately.
|
||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||
let nserror = error as NSError
|
||||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
277
iOS/ContentView.md
Normal file
277
iOS/ContentView.md
Normal file
|
@ -0,0 +1,277 @@
|
|||
# ContentView
|
||||
|
||||
A page of the app.
|
||||
|
||||
## Views, Functions & Variables
|
||||
|
||||
`@State` allows the view to respond to every change of the anotated variable. This variables get initialized by the view in which they belong and are not "recieved" from external objects.
|
||||
SwiftUI memorizes internally the value of the `@State` property and updates the view every time it changes.
|
||||
|
||||
`@Binding` is used for properties that are passed to the view from another. The recieveing view can read the binfing value, react to changes and modify it's value.
|
||||
`@Binding` variables are passed with the prefix `$`,
|
||||
|
||||
### Simple View
|
||||
|
||||
- Simplest view.
|
||||
- Permits the visualization of simple UIs.
|
||||
- Constituited bay a body of type `View`
|
||||
|
||||
```swift
|
||||
struct SimpleViewName: View {
|
||||
|
||||
let CONSTANT: Type
|
||||
@State var variable: Type
|
||||
|
||||
func func(){
|
||||
@Binding var variable: Type
|
||||
// code here
|
||||
}
|
||||
|
||||
// property needed
|
||||
var body: some View {
|
||||
// view contents
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### HStack, VStack, ZStack
|
||||
|
||||
Used to organize elements without dealing with constraints or forcing the visualization on devices wih differents screen sizes.
|
||||
|
||||
```swift
|
||||
struct ContentView: View {
|
||||
var body: some View {
|
||||
|
||||
// cannot have multiple stack at the same level
|
||||
VStack {
|
||||
HStack {
|
||||
View()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Table View
|
||||
|
||||
Most common view to present array contents, it automatically hendles the scrolling of the page with the *bounce* effect.
|
||||
I can be inegrate d in a `NavigaionView` to handle a `DeailView` of a selectted item in the list.
|
||||
|
||||
The basic object that creates the table view is the `List()`. It's job is to create a "cell" for every element in the array.
|
||||
The array can be filttered with a *search bar*.
|
||||
The array elements can be grouped with the `Section()` object that groups cells under a common name un the table.
|
||||
|
||||
```swift
|
||||
// view name can be any
|
||||
struct TableView: View {
|
||||
var array = [...]
|
||||
|
||||
var body: some View {
|
||||
List(array) { iem in
|
||||
TableCell(item: item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// view name can be any
|
||||
struct TableCell: View {
|
||||
let item: Any
|
||||
|
||||
var body: some View {
|
||||
// cell content
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Every cell can have a link to visualize he details of the selected object. This is done by using `NavigationView` and `NavigationLink`.
|
||||
|
||||
The `NavigationView` contains the list and the property `.navigationBarTitle()` sets the view title.
|
||||
It's possible to add other controls in the top part of the view (buttons, ...) using the property `.navigationBarItems()`.
|
||||
|
||||
```swift
|
||||
struct ContentView: View {
|
||||
let array = [...]
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
List(array) { item in
|
||||
NavigationLink(destination: View()) {
|
||||
// link UI
|
||||
}
|
||||
}.navigationBarTitle(Text("Title"))
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Tab Bar View
|
||||
|
||||
This view handles a bar on the botom of the screen with links to simple or more comlpex views.
|
||||
This is useful for designing pages that can be easely navigated by the user.
|
||||
|
||||
```swift
|
||||
struct TabBarView: View {
|
||||
var body: some View {
|
||||
|
||||
// first tab
|
||||
Text("Tab Title")
|
||||
.tabItem{
|
||||
// tab selector design example
|
||||
Image(systemImage: "house.fill")
|
||||
Text("Home")
|
||||
}
|
||||
|
||||
// n-th tab
|
||||
Text("Tab Title")
|
||||
.tabItem{
|
||||
// tab selector design
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The `TabBar` conostruction is made applying the `.tabItem{}` parameter to the object or page that the tab will link to.
|
||||
It's possible to specify up to 5 `.tabItem{}` elements that will be displayed singularly in the `TabBar`.
|
||||
|
||||
Fron the 6th elemnet onwards, the first 4 elemens will appear normally, meanwhile the 5th will become a "more" element that will open a `TableView` with the list of the other `.tabItem{}` elements. This page permis to define which elements will be visible.
|
||||
|
||||
It's possible to integrate the NavigationView in the TabBar in two ways:
|
||||
|
||||
- inserting it as a container for the whole `TabBar` (at the moment of the transistion to the detail page the `TabBar` will be hidden)
|
||||
- inserting it as a container of a single `.tabItem{}` (the transition will happen inside the `TabBar` that will then remain visible)
|
||||
|
||||
## View Elements
|
||||
|
||||
### Text
|
||||
|
||||
```swift
|
||||
Text("")
|
||||
```
|
||||
|
||||
### Shapes
|
||||
|
||||
```swift
|
||||
Rectangle()
|
||||
Circle()
|
||||
```
|
||||
|
||||
### Spacing
|
||||
|
||||
```swift
|
||||
Divider()
|
||||
Spacer()
|
||||
```
|
||||
|
||||
### Image
|
||||
|
||||
[System Images](https://developer.apple.com/design/human-interface-guidelines/sf-symbols/overview/)
|
||||
|
||||
```swift
|
||||
Image(systemName: "sfsymbol")
|
||||
```
|
||||
|
||||
### Button
|
||||
|
||||
```swift
|
||||
Button(action: { /* statements */ }) {
|
||||
Text("Label")
|
||||
//or
|
||||
Image(systemName = "sfsymbol")
|
||||
}
|
||||
|
||||
// button with alert popup
|
||||
Button(action: { /* statements */ }) {
|
||||
Text("abel")
|
||||
}.action(isPresented: $boolBinding) {
|
||||
Alert(title: Text("Alert Popup Title"), message: Text("Alert Message"))
|
||||
}
|
||||
```
|
||||
|
||||
### Style Options
|
||||
|
||||
Common syle options:
|
||||
|
||||
- `padding()`: adds an internal padding to the object.
|
||||
- `foregroundColor()`: defines the color of the text or containd object.
|
||||
- `background()`: defines the background color.
|
||||
- `font()`: sets font type, size, weight, ...
|
||||
- `cornerRadius()`: modifies the angles of the containing box.
|
||||
- `frame()`: sets a fixed size for the object.
|
||||
- `resizable()`, `scaleToFill()`, `scaleToFit()`: habled the resizing of an object inside another.
|
||||
- `clipShape()`: overlays a shape over the object
|
||||
- `overlay()`: overlays an element over the object, more complex than clipShape
|
||||
- `shadow()`: Sets the object's shadow
|
||||
- `lineLimit()`: limits the number of visible lines in `TextField`
|
||||
|
||||
```swift
|
||||
View().styleOption()
|
||||
// or
|
||||
View {
|
||||
|
||||
}.styleOPtion()
|
||||
```
|
||||
|
||||
## Forms & Input
|
||||
|
||||
```swift
|
||||
Form {
|
||||
Section (header: Text("Section Title")) {
|
||||
// form components
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Picker
|
||||
|
||||
```swift
|
||||
// list item picker
|
||||
Picker(selction: $index, label: Text("Selection Title")) {
|
||||
ForEach(0..<itemArray.count){
|
||||
Text(itemArray[$0]).tag($0) // tag adds the index of the selected item to the info of the Text()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Stepper
|
||||
|
||||
```swift
|
||||
Stepper("\(number)", value: $number, in: start...end)
|
||||
```
|
||||
|
||||
### TextField
|
||||
|
||||
```swift
|
||||
TextField("Placeholder Text", text: $result)
|
||||
.keyboardType(.<kb_type>)
|
||||
```
|
||||
|
||||
### Slider
|
||||
|
||||
```swift
|
||||
Slider(value: $numVariable)
|
||||
```
|
||||
|
||||
## API Interaction
|
||||
|
||||
```swift
|
||||
@State private var apiItems = [<struct>]()
|
||||
|
||||
// struct sould be Identifiable & Codable
|
||||
|
||||
func loadData() {
|
||||
guard let url = URL(string: "https://jsonplaceholder.typicode.com/todos")
|
||||
else { print("Invalid URL") return }
|
||||
|
||||
let request = URLRequest(url: url)
|
||||
|
||||
URLSession.shared.dataTask(with: request) { data, response, error in
|
||||
if let data = data {
|
||||
if let response = try? JSONDecoder().decode([TaskEntry].self, from: data) {
|
||||
DispatchQueue.main.async {
|
||||
self.itemsApi = response
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}.resume()
|
||||
}
|
||||
```
|
15
iOS/CoreData.md
Normal file
15
iOS/CoreData.md
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Core Data
|
||||
|
||||
Internal device data memorization in integrated DB.
|
||||
Used to store data from API for offline use.
|
||||
|
||||
The data added or update at app launch from the APIs.
|
||||
|
||||
## CoreData Structure
|
||||
|
||||
CoreData are handled by the file `.xcdatamodel` which contains info about the *Entities* that will contain data.
|
||||
|
||||
The structure is similar to a DB on file (postgreSQL, sqlite).
|
||||
Entities are equivalent to relational DBs tables.
|
||||
|
||||
An Entity is identified by a name what will be used in read/write operations.
|
58
iOS/SceneDelegate (XCode 11).md
Normal file
58
iOS/SceneDelegate (XCode 11).md
Normal file
|
@ -0,0 +1,58 @@
|
|||
# SceneDelegate
|
||||
|
||||
Loads the *scene* and set the `rootController` to control the app. The `ContentView` gets called, which contains the UI elements to be visualized.
|
||||
|
||||
```swift
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
|
||||
var window: UIWindow?
|
||||
|
||||
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
|
||||
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
|
||||
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
|
||||
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
|
||||
|
||||
// Create the SwiftUI view that provides the window contents.
|
||||
let contentView = ContentView()
|
||||
|
||||
// Use a UIHostingController as window root view controller.
|
||||
if let windowScene = scene as? UIWindowScene {
|
||||
let window = UIWindow(windowScene: windowScene)
|
||||
window.rootViewController = UIHostingController(rootView: contentView)
|
||||
self.window = window
|
||||
window.makeKeyAndVisible()
|
||||
}
|
||||
}
|
||||
|
||||
func sceneDidDisconnect(_ scene: UIScene) {
|
||||
// Called as the scene is being released by the system.
|
||||
// This occurs shortly after the scene enters the background, or when its session is discarded.
|
||||
// Release any resources associated with this scene that can be re-created the next time the scene connects.
|
||||
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
|
||||
}
|
||||
|
||||
func sceneDidBecomeActive(_ scene: UIScene) {
|
||||
// Called when the scene has moved from an inactive state to an active state.
|
||||
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
|
||||
}
|
||||
|
||||
func sceneWillResignActive(_ scene: UIScene) {
|
||||
// Called when the scene will move from an active state to an inactive state.
|
||||
// This may occur due to temporary interruptions (ex. an incoming phone call).
|
||||
}
|
||||
|
||||
func sceneWillEnterForeground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the background to the foreground.
|
||||
// Use this method to undo the changes made on entering the background.
|
||||
}
|
||||
|
||||
func sceneDidEnterBackground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the foreground to the background.
|
||||
// Use this method to save data, release shared resources, and store enough scene-specific state information
|
||||
// to restore the scene back to its current state.
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue