a decent place of creation of recording and uploading just speed coded in 30 minutes or so

This commit is contained in:
talksik
2021-12-22 05:30:29 -08:00
parent fa9c4561c6
commit 3b8041a17d
5 changed files with 91 additions and 10 deletions
+4
View File
@@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */
8904DEC8277055E90071E6CA /* OnboardingTrioView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8904DEC7277055E90071E6CA /* OnboardingTrioView.swift */; };
891060E4277359B900F14509 /* CloudStorageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891060E3277359B900F14509 /* CloudStorageService.swift */; };
89110714277166D6005797FA /* UserFriends.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89110713277166D6005797FA /* UserFriends.swift */; };
891A15E127653A7000B26659 /* nirvana_iosApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891A15E027653A7000B26659 /* nirvana_iosApp.swift */; };
891A15E327653A7000B26659 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 891A15E227653A7000B26659 /* ContentView.swift */; };
@@ -80,6 +81,7 @@
/* Begin PBXFileReference section */
8904DEC7277055E90071E6CA /* OnboardingTrioView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnboardingTrioView.swift; sourceTree = "<group>"; };
891060E3277359B900F14509 /* CloudStorageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudStorageService.swift; sourceTree = "<group>"; };
89110713277166D6005797FA /* UserFriends.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserFriends.swift; sourceTree = "<group>"; };
891A15DD27653A7000B26659 /* nirvana-ios.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "nirvana-ios.app"; sourceTree = BUILT_PRODUCTS_DIR; };
891A15E027653A7000B26659 /* nirvana_iosApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = nirvana_iosApp.swift; sourceTree = "<group>"; };
@@ -344,6 +346,7 @@
children = (
89AD557B276DF86A001658E0 /* FirestoreService.swift */,
89F18AED276EB55C00115B1E /* ServiceState.swift */,
891060E3277359B900F14509 /* CloudStorageService.swift */,
);
path = Repositories;
sourceTree = "<group>";
@@ -528,6 +531,7 @@
buildActionMask = 2147483647;
files = (
891A15E327653A7000B26659 /* ContentView.swift in Sources */,
891060E4277359B900F14509 /* CloudStorageService.swift in Sources */,
896CE7002770847400DB474E /* CircleFooterView.swift in Sources */,
894E80BE276ED7A700624B72 /* avatars.swift in Sources */,
896CE7052770AE5D00DB474E /* InboxView.swift in Sources */,
@@ -0,0 +1,47 @@
//
// CloudStorageService.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/22/21.
//
import Foundation
import Firebase
class CloudStorageService {
// Get a reference to the storage service using the default Firebase App
private let storage = Storage.storage()
func uploadLocalUrl(localFileUrl: URL, completion: @escaping((_ audioDataUrl: URL?) -> ())) {
// Create a storage reference from our storage service
let storageRef = storage.reference()
let audioFileName = UUID().uuidString // for the firestore and cloud storage connection
let audioMessageRef = storageRef.child("messages/\(audioFileName).m4a")
// Upload the file to the path "images/{fileName}.m4a"
let uploadTask = audioMessageRef.putFile(from: localFileUrl, metadata: nil) { metadata, error in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
print("error in uploading audio file to cloud storage")
completion(nil)
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
audioMessageRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
completion(nil)
return
}
print("successfully got the download online url that I need\(downloadURL)")
completion(downloadURL)
}
}
}
}
@@ -146,4 +146,15 @@ class FirestoreService {
}
func createMessage(message: Message, completion: @escaping((_ state: ServiceState) -> ())) {
do {
let _ = try db.collection(Collection.messages.rawValue).addDocument(from: message)
completion(ServiceState.success("user created"))
} catch {
print("error in updating user \(error.localizedDescription)")
completion(ServiceState.error(ServiceError(description: error.localizedDescription)))
}
}
}
@@ -132,7 +132,14 @@ struct CircleGridView: View {
.onEnded { gestureValue in
guard case .second(true, let drag?) = gestureValue else { return }
self.recordingGestureDeactived()
// stop recording
print("stopped recording")
self.selectedFriendIndex = nil
self.innerCircleVM.stopRecording(senderId: self.authSessionStore.user!.id!, receiverId: friend.id!)
// self.recordingGestureDeactived()
}
)
// .simultaneousGesture(
@@ -368,12 +375,8 @@ extension CircleGridView {
self.innerCircleVM.startRecording()
}
// TODO: fill in here
private func recordingGestureDeactived() {
// stop recording
print("stopped recording")
self.selectedFriendIndex = nil
self.innerCircleVM.stopRecording()
}
}
@@ -16,6 +16,9 @@ class InnerCircleViewModel: ObservableObject {
private var audioLocalUrl: URL?
let cloudStorageService = CloudStorageService()
let firestoreService = FirestoreService()
func startRecording() {
let recordingSession = AVAudioSession.sharedInstance()
@@ -51,18 +54,31 @@ class InnerCircleViewModel: ObservableObject {
}
}
func stopRecording(){
// sender should be current user
// receiver is the person we are sending to
func stopRecording(senderId:String, receiverId:String) {
audioRecorder.stop()
isRecording = false
if self.audioLocalUrl != nil {
// upload to the cloud storage
// store in firestore for receiving user to automatically get notified
self.cloudStorageService.uploadLocalUrl(localFileUrl: self.audioLocalUrl!) {[weak self] audioDataUrl in
if audioDataUrl == nil {
print("there was an error in uploading file")
return
}
let newMessage = Message(receiverId: receiverId, senderId: senderId, listenCount: 0, audioDataUrl: audioDataUrl!.absoluteString)
// create a new message in firestore with the url for receiving user to automatically get notified
self?.firestoreService.createMessage(message: newMessage) {[weak self] res in
print(res)
}
}
// delete local audio file from user's phone so that it doesn't get crazy
print("stopped recording: file about to get deleted from \(getTemporaryDirectory()) with filename: \(self.audioLocalUrl)")
try? FileManager.default.removeItem(at: self.audioLocalUrl!)
}
}