adding last.fm username changing

This commit is contained in:
andy 2022-08-16 18:01:17 +01:00
parent 1966d0b407
commit e67875df4e
3 changed files with 50 additions and 16 deletions

View File

@ -29,7 +29,11 @@ class User: Identifiable, Decodable {
var last_refreshed: String var last_refreshed: String
var spotify_linked: Bool 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 //MARK: Initialization
@ -58,6 +62,19 @@ class User: Identifiable, Decodable {
self.lastfm_username = lastfm_username 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 { private enum CodingKeys: String, CodingKey {
case username case username
case email case email

View File

@ -12,6 +12,7 @@ import SwiftyJSON
public enum UserApi { public enum UserApi {
case getUser case getUser
case updateUser(updates: JSON)
case deleteUser case deleteUser
} }
@ -24,6 +25,8 @@ extension UserApi: ApiRequest {
switch self { switch self {
case .getUser: case .getUser:
return "api/user" return "api/user"
case .updateUser:
return "api/user"
case .deleteUser: case .deleteUser:
return "api/user" return "api/user"
} }
@ -33,17 +36,29 @@ extension UserApi: ApiRequest {
switch self { switch self {
case .getUser: case .getUser:
return .get return .get
case .updateUser:
return .post
case .deleteUser: case .deleteUser:
return .delete return .delete
} }
} }
var parameters: JSON? { var parameters: JSON? {
return nil switch self {
case .getUser, .deleteUser:
return nil
case .updateUser(let updates):
return updates
}
} }
var parameterType: ParameterEncoder? { var parameterType: ParameterEncoder? {
return nil switch self {
case .getUser, .deleteUser:
return nil
case .updateUser:
return JSONParameterEncoder.default
}
} }
var headers: HTTPHeaders? { var headers: HTTPHeaders? {

View File

@ -16,7 +16,13 @@ struct SettingsList: View {
@State private var deleteAlertShowing = false @State private var deleteAlertShowing = false
var body: some View { var body: some View {
NavigationView {
let spotify_link_bind = Binding<Bool>(get: { liveUser.user?.spotify_linked ?? false},
set: { newVal in liveUser.user?.spotify_linked = newVal })
let lastfm_bind = Binding<String>(get: { liveUser.user?.lastfm_username ?? ""},
set: { newVal in liveUser.user?.lastfm_username = newVal })
return NavigationView {
List{ List{
Section { Section {
Button(action: { Button(action: {
@ -40,19 +46,15 @@ struct SettingsList: View {
} }
} }
Section(header: Text("Config")) { Section(header: Text("Spotify")) {
if let spotify_linked = self.liveUser.user?.spotify_linked { Toggle(isOn: spotify_link_bind) {
Toggle(isOn: .constant(spotify_linked)) { Text("Account Link")
Text("Spotify Link")
}
.disabled(true)
} }
// HStack { .disabled(true)
// Text("Last.fm Username") }
// Spacer()
// Section(header: Text("Last.fm")) {
// Text(self.$liveUser.user.lastfm_username ?? .constant("")) TextField("Last.fm Username", text: lastfm_bind)
// }
} }
Section { Section {