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
@@ -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!)
}
}