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 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

View File

@ -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? {

View File

@ -16,7 +16,13 @@ struct SettingsList: View {
@State private var deleteAlertShowing = false
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{
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 {