diff --git a/Mixonomer/Model/User.swift b/Mixonomer/Model/User.swift index 6012739..ed7a245 100644 --- a/Mixonomer/Model/User.swift +++ b/Mixonomer/Model/User.swift @@ -29,7 +29,11 @@ class User: Identifiable, Decodable { var last_refreshed: String var spotify_linked: Bool - @Published var lastfm_username: String? + @Published var lastfm_username: String? { + didSet { + self.updateUser(updates: JSON(["lastfm_username": self.lastfm_username])) + } + } //MARK: Initialization @@ -58,6 +62,19 @@ class User: Identifiable, Decodable { self.lastfm_username = lastfm_username } + func updateUser(updates: JSON) { + let api = UserApi.updateUser(updates: updates) + RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in + switch response.response?.statusCode { + case 200, 201: + break + case _: + debugPrint("error: \(updates)") + } + } + //TODO: do better error checking + } + private enum CodingKeys: String, CodingKey { case username case email diff --git a/Mixonomer/Network/UserApi.swift b/Mixonomer/Network/UserApi.swift index 10c021c..d083d7c 100644 --- a/Mixonomer/Network/UserApi.swift +++ b/Mixonomer/Network/UserApi.swift @@ -12,6 +12,7 @@ import SwiftyJSON public enum UserApi { case getUser + case updateUser(updates: JSON) case deleteUser } @@ -24,6 +25,8 @@ extension UserApi: ApiRequest { switch self { case .getUser: return "api/user" + case .updateUser: + return "api/user" case .deleteUser: return "api/user" } @@ -33,17 +36,29 @@ extension UserApi: ApiRequest { switch self { case .getUser: return .get + case .updateUser: + return .post case .deleteUser: return .delete } } var parameters: JSON? { - return nil + switch self { + case .getUser, .deleteUser: + return nil + case .updateUser(let updates): + return updates + } } var parameterType: ParameterEncoder? { - return nil + switch self { + case .getUser, .deleteUser: + return nil + case .updateUser: + return JSONParameterEncoder.default + } } var headers: HTTPHeaders? { diff --git a/Mixonomer/Views/Settings/SettingsList.swift b/Mixonomer/Views/Settings/SettingsList.swift index da80962..12276bc 100644 --- a/Mixonomer/Views/Settings/SettingsList.swift +++ b/Mixonomer/Views/Settings/SettingsList.swift @@ -16,7 +16,13 @@ struct SettingsList: View { @State private var deleteAlertShowing = false var body: some View { - NavigationView { + + let spotify_link_bind = Binding(get: { liveUser.user?.spotify_linked ?? false}, + set: { newVal in liveUser.user?.spotify_linked = newVal }) + let lastfm_bind = Binding(get: { liveUser.user?.lastfm_username ?? ""}, + set: { newVal in liveUser.user?.lastfm_username = newVal }) + + return NavigationView { List{ Section { Button(action: { @@ -40,19 +46,15 @@ struct SettingsList: View { } } - Section(header: Text("Config")) { - if let spotify_linked = self.liveUser.user?.spotify_linked { - Toggle(isOn: .constant(spotify_linked)) { - Text("Spotify Link") - } - .disabled(true) + Section(header: Text("Spotify")) { + Toggle(isOn: spotify_link_bind) { + Text("Account Link") } -// HStack { -// Text("Last.fm Username") -// Spacer() -// -// Text(self.$liveUser.user.lastfm_username ?? .constant("")) -// } + .disabled(true) + } + + Section(header: Text("Last.fm")) { + TextField("Last.fm Username", text: lastfm_bind) } Section {