@@ -25,7 +25,8 @@ public final class ProfileSettingViewModel: ViewModelType {
2525 public var state : AnyPublisher < ViewModelState , Never > { currentState. compactMap { $0 } . eraseToAnyPublisher ( ) }
2626 private var currentState : CurrentValueSubject < ViewModelState ? , Never > = . init( nil )
2727 private var currentImage : CurrentValueSubject < Data ? , Never > = . init( nil )
28-
28+ private var profilePictureUrl : CurrentValueSubject < String ? , Never > = . init( nil )
29+
2930 public init ( repository: SettingRepository ) {
3031 self . repository = repository
3132 }
@@ -34,9 +35,11 @@ public final class ProfileSettingViewModel: ViewModelType {
3435 Task {
3536 do {
3637 var url : String ?
37- if let imageData = currentImage. value {
38+ if let imageData = currentImage. value,
39+ let filename = filenameFromFilepath ( profilePictureUrl. value) {
3840 let userToken = try await repository. getUserPrivacy ( ) . data? . userToken ?? UUID ( ) . uuidString
39- url = try await AmplifyManager . shared. uploadImage ( data: imageData, fileName: " profile/ \( userToken) " ) . absoluteString
41+ url = try await AmplifyManager . shared. uploadImage ( data: imageData, fileName: " profile/ \( userToken) _ \( Date ( ) . currentfullDate) " ) . absoluteString
42+ try await AmplifyManager . shared. delete ( fileName: filename)
4043 }
4144 saveUserProfile ( imageUrl: url, message: message)
4245 } catch {
@@ -53,7 +56,7 @@ public final class ProfileSettingViewModel: ViewModelType {
5356 Task {
5457 do {
5558 let response = try await repository. getUserProfile ( )
56-
59+ profilePictureUrl . send ( response . data ? . profilePictureUrl )
5760 currentState. send ( . updateProfileMessage( response. data? . message) )
5861 currentState. send ( . updateProfileImage( response. data? . profilePictureUrl) )
5962 } catch {
@@ -75,4 +78,16 @@ extension ProfileSettingViewModel {
7578 }
7679 }
7780 }
81+
82+ private func filenameFromFilepath( _ filepath: String ? ) -> String ? {
83+ guard let filepath = filepath else {
84+ return nil
85+ }
86+ if filepath. contains ( " profile " ) {
87+ let splitProfile = filepath. components ( separatedBy: " profile " )
88+ return " profile " + splitProfile[ 1 ]
89+ } else {
90+ return nil
91+ }
92+ }
7893}
0 commit comments