Skip to content

Commit

Permalink
added get data, read data, download documents, upload image files met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
XinyiXiang committed Oct 13, 2019
1 parent 9d46128 commit 8699934
Show file tree
Hide file tree
Showing 4,506 changed files with 1,011,664 additions and 4,794 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 5 additions & 0 deletions Feel Better/.firebaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": {
"default": "feel-better-48ac1"
}
}
65 changes: 65 additions & 0 deletions Feel Better/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
firebase-debug.log*

# Firebase cache
.firebase/

# Firebase config

# Uncomment this if you'd like others to create their own Firebase project.
# For a team working on the same Firebase project(s), it is recommended to leave
# it commented so all members can deploy to the same project(s) in .firebaserc.
# .firebaserc

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
8 changes: 8 additions & 0 deletions Feel Better/Feel Better.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1C0E06FC23529F3000A916D7 /* images */ = {
isa = PBXGroup;
children = (
);
path = images;
sourceTree = "<group>";
};
5BFBAE34B11FFDF7D581CD18 /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -84,6 +91,7 @@
D20CB0432352746F00C8BE8D /* Feel Better */ = {
isa = PBXGroup;
children = (
1C0E06FC23529F3000A916D7 /* images */,
D20CB0442352746F00C8BE8D /* AppDelegate.swift */,
D20CB0462352746F00C8BE8D /* SceneDelegate.swift */,
D20CB0482352746F00C8BE8D /* FirstViewController.swift */,
Expand Down
7 changes: 4 additions & 3 deletions Feel Better/Feel Better/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

import UIKit
import Firebase

import FirebaseAuth
import FirebaseDatabase
import FirebaseFirestore

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()

// Override point for customization after application launch.
return true
}
Expand Down
121 changes: 121 additions & 0 deletions Feel Better/Feel Better/FirstViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,131 @@
//

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseFirestore


let db = Firestore.firestore()
let diariesRef = db.collection("diaries")
let docRef = db.collection("diaries").document("today")
let data = Data()
let storage = Storage.storage()
let storageRef = storage.reference()

class FirstViewController: UIViewController {

var ref: DatabaseReference!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// create record into Firebases
ref = Database.database().reference()
ref.child("name").childByAutoId().setValue("visual")
ref.child("name").childByAutoId().setValue("phanith")

}

func setDocument(){
// Add a new document in collection "diaries"
db.collection("diaries").document("today").setData([
"title": "Journal",
"content": "content",
"sentiments":"happy",
"saveDate": "10/12/19"
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
}

func readData(){
// read the data
diariesRef.document("diaries").setData([
"title": "Jorunal",
"content": "content",
"sentiments":"happy",
"saveDate": "10/12/19"
])}

func getDoc(){
//get the document from designated collection
docRef.getDocument {(document, error) in
if let document = document, document.exists {
let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
print("Document data: \(dataDescription)")
} else {
print("Document does not exist")
}
}
}

func errorHandler(ErrorReporter : String) -> Void {
let _ = "upload unsuccessful"
}
func uploadFilesFromMemory(ErrorReporter : (String) -> Void){

// Create a reference to the file you want to upload
let picsRef = storageRef.child("images/user.jpg")

// Upload the file to the path "images/rivers.jpg"
_ = picsRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
_ = metadata.size

// You can also access to download URL after upload.
picsRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
}


func uploadFilesFromLocal (ErrorReporter : (String) -> Void ) -> Void {
// File located on disk
let localFile = URL(string: "path/to/file")!

// Create a reference to the file you want to upload
let picsRef = storageRef.child("images/user.jpg")

// Upload the file to the path "images/rivers.jpg"
_ = picsRef.putFile(from: localFile, metadata: nil) { metadata, error in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
_ = metadata.size
// You can also access to download URL after upload.
picsRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!

return

}
}
}
}

}

extension Array {
func toFirebaseDictionary() -> [Int: Element] {
return Dictionary<Int, Element>(uniqueKeysWithValues: zip(indices, self))
}
}

5 changes: 4 additions & 1 deletion Feel Better/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ target 'Feel Better' do
pod 'Firebase/Analytics'
# add pods for any other desired Firebase products
# https://firebase.google.com/docs/ios/setup#available-pods

pod 'Firebase/Core'
pod 'Firebase/Firestore'
pod 'Firebase/Database'
pod 'Firebase/Auth'
pod 'Firebase/Storage'

end
Loading

0 comments on commit 8699934

Please sign in to comment.