addin failed auth handling
This commit is contained in:
parent
4f2e0f245c
commit
216ac84627
@ -2,6 +2,6 @@
|
|||||||
<Workspace
|
<Workspace
|
||||||
version = "1.0">
|
version = "1.0">
|
||||||
<FileRef
|
<FileRef
|
||||||
location = "self:/Users/andy/dev/swift/Music-Tools-iOS/Mixonomer.xcodeproj">
|
location = "self:">
|
||||||
</FileRef>
|
</FileRef>
|
||||||
</Workspace>
|
</Workspace>
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
<BuildableReference
|
<BuildableReference
|
||||||
BuildableIdentifier = "primary"
|
BuildableIdentifier = "primary"
|
||||||
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
||||||
BuildableName = "Music Tools.app"
|
BuildableName = "Mixonomer.app"
|
||||||
BlueprintName = "Mixonomer"
|
BlueprintName = "Mixonomer"
|
||||||
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
||||||
</BuildableReference>
|
</BuildableReference>
|
||||||
@ -33,7 +33,7 @@
|
|||||||
<BuildableReference
|
<BuildableReference
|
||||||
BuildableIdentifier = "primary"
|
BuildableIdentifier = "primary"
|
||||||
BlueprintIdentifier = "E9EA691C23F9A54B0012C3E8"
|
BlueprintIdentifier = "E9EA691C23F9A54B0012C3E8"
|
||||||
BuildableName = "Music ToolsTests.xctest"
|
BuildableName = "MixonomerTests.xctest"
|
||||||
BlueprintName = "MixonomerTests"
|
BlueprintName = "MixonomerTests"
|
||||||
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
||||||
</BuildableReference>
|
</BuildableReference>
|
||||||
@ -43,7 +43,7 @@
|
|||||||
<BuildableReference
|
<BuildableReference
|
||||||
BuildableIdentifier = "primary"
|
BuildableIdentifier = "primary"
|
||||||
BlueprintIdentifier = "E9EA692723F9A54B0012C3E8"
|
BlueprintIdentifier = "E9EA692723F9A54B0012C3E8"
|
||||||
BuildableName = "Music ToolsUITests.xctest"
|
BuildableName = "MixonomerUITests.xctest"
|
||||||
BlueprintName = "MixonomerUITests"
|
BlueprintName = "MixonomerUITests"
|
||||||
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
||||||
</BuildableReference>
|
</BuildableReference>
|
||||||
@ -65,7 +65,7 @@
|
|||||||
<BuildableReference
|
<BuildableReference
|
||||||
BuildableIdentifier = "primary"
|
BuildableIdentifier = "primary"
|
||||||
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
||||||
BuildableName = "Music Tools.app"
|
BuildableName = "Mixonomer.app"
|
||||||
BlueprintName = "Mixonomer"
|
BlueprintName = "Mixonomer"
|
||||||
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
||||||
</BuildableReference>
|
</BuildableReference>
|
||||||
@ -74,7 +74,7 @@
|
|||||||
<EnvironmentVariable
|
<EnvironmentVariable
|
||||||
key = "MTOOLS_SERVER"
|
key = "MTOOLS_SERVER"
|
||||||
value = "http://127.0.0.1:5000/"
|
value = "http://127.0.0.1:5000/"
|
||||||
isEnabled = "YES">
|
isEnabled = "NO">
|
||||||
</EnvironmentVariable>
|
</EnvironmentVariable>
|
||||||
</EnvironmentVariables>
|
</EnvironmentVariables>
|
||||||
</LaunchAction>
|
</LaunchAction>
|
||||||
@ -89,7 +89,7 @@
|
|||||||
<BuildableReference
|
<BuildableReference
|
||||||
BuildableIdentifier = "primary"
|
BuildableIdentifier = "primary"
|
||||||
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
BlueprintIdentifier = "E9EA690623F9A5430012C3E8"
|
||||||
BuildableName = "Music Tools.app"
|
BuildableName = "Mixonomer.app"
|
||||||
BlueprintName = "Mixonomer"
|
BlueprintName = "Mixonomer"
|
||||||
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
ReferencedContainer = "container:Mixonomer.xcodeproj">
|
||||||
</BuildableReference>
|
</BuildableReference>
|
||||||
|
@ -45,8 +45,10 @@ class LiveUser: ObservableObject {
|
|||||||
let api = PlaylistApi.getPlaylists
|
let api = PlaylistApi.getPlaylists
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
switch response.result {
|
self.checkNetworkResponse(response: response)
|
||||||
case .success:
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
guard let data = response.data else {
|
guard let data = response.data else {
|
||||||
fatalError("error getting playlists")
|
fatalError("error getting playlists")
|
||||||
@ -70,14 +72,8 @@ class LiveUser: ObservableObject {
|
|||||||
print("error encoding playlists: \(error)")
|
print("error encoding playlists: \(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
case .failure:
|
case _:
|
||||||
|
break
|
||||||
switch response.response?.statusCode {
|
|
||||||
case 401:
|
|
||||||
self.loggedIn = false
|
|
||||||
case _:
|
|
||||||
print("error")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,31 +83,50 @@ class LiveUser: ObservableObject {
|
|||||||
|
|
||||||
let api = TagApi.getTags
|
let api = TagApi.getTags
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
|
self.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
guard let data = response.data else {
|
guard let data = response.data else {
|
||||||
fatalError("error getting tags")
|
fatalError("error getting tags")
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let json = try? JSON(data: data) else {
|
guard let json = try? JSON(data: data) else {
|
||||||
fatalError("error parsing reponse")
|
fatalError("error parsing reponse")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let tags = json["tags"].arrayValue
|
||||||
|
|
||||||
let tags = json["tags"].arrayValue
|
// update state
|
||||||
|
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
// update state
|
|
||||||
self.tags = TagApi.fromJSON(tag: tags).sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
self.isRefreshingTags = false
|
||||||
|
|
||||||
self.isRefreshingTags = false
|
let encoder = JSONEncoder()
|
||||||
|
do {
|
||||||
let encoder = JSONEncoder()
|
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
||||||
do {
|
} catch {
|
||||||
UserDefaults.standard.set(String(data: try encoder.encode(tags), encoding: .utf8), forKey: "tags")
|
print("error encoding tags: \(error)")
|
||||||
} catch {
|
}
|
||||||
print("error encoding tags: \(error)")
|
|
||||||
|
case _:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkNetworkResponse(response: AFDataResponse<Any>) {
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 401:
|
||||||
|
self.loggedIn = false
|
||||||
|
case _:
|
||||||
|
print("error making request")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func loadUserDefaults() -> LiveUser {
|
func loadUserDefaults() -> LiveUser {
|
||||||
let defaults = UserDefaults.standard
|
let defaults = UserDefaults.standard
|
||||||
let decoder = JSONDecoder()
|
let decoder = JSONDecoder()
|
||||||
|
@ -117,10 +117,10 @@ class Playlist: Identifiable, Equatable, Codable, ObservableObject {
|
|||||||
func updatePlaylist(updates: JSON) {
|
func updatePlaylist(updates: JSON) {
|
||||||
let api = PlaylistApi.updatePlaylist(name: self.name, updates: updates)
|
let api = PlaylistApi.updatePlaylist(name: self.name, updates: updates)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
switch response.result {
|
switch response.response?.statusCode {
|
||||||
case .success:
|
case 200, 201:
|
||||||
break
|
break
|
||||||
case .failure:
|
case _:
|
||||||
debugPrint("error: \(self.name), \(updates)")
|
debugPrint("error: \(self.name), \(updates)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import SwiftyJSON
|
|||||||
|
|
||||||
|
|
||||||
public enum AuthApi {
|
public enum AuthApi {
|
||||||
case token(username: String, password: String)
|
case token(username: String, password: String, expiry: Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AuthApi: ApiRequest {
|
extension AuthApi: ApiRequest {
|
||||||
@ -37,8 +37,8 @@ extension AuthApi: ApiRequest {
|
|||||||
|
|
||||||
var parameters: JSON? {
|
var parameters: JSON? {
|
||||||
switch self {
|
switch self {
|
||||||
case .token(let username, let password):
|
case .token(let username, let password, let expiry):
|
||||||
return JSON(["username": username, "password": password])
|
return JSON(["username": username, "password": password, "expiry": expiry])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,33 +56,4 @@ extension AuthApi: ApiRequest {
|
|||||||
var authMethod: AuthMethod? {
|
var authMethod: AuthMethod? {
|
||||||
return AuthMethod.none
|
return AuthMethod.none
|
||||||
}
|
}
|
||||||
|
|
||||||
static func fromJSON(playlist: Data) -> Playlist? {
|
|
||||||
|
|
||||||
let decoder = JSONDecoder()
|
|
||||||
do {
|
|
||||||
let playlist = try decoder.decode(Playlist.self, from: playlist)
|
|
||||||
return playlist
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
static func fromJSON(playlist: JSON) -> Playlist? {
|
|
||||||
|
|
||||||
let _json = playlist.rawString()?.data(using: .utf8)
|
|
||||||
|
|
||||||
if let data = _json {
|
|
||||||
let decoder = JSONDecoder()
|
|
||||||
do {
|
|
||||||
let playlist = try decoder.decode(Playlist.self, from: data)
|
|
||||||
return playlist
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
print(playlist)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -41,13 +41,13 @@ struct LoginScreen: View {
|
|||||||
keychain["username"] = username
|
keychain["username"] = username
|
||||||
// keychain["password"] = password
|
// keychain["password"] = password
|
||||||
|
|
||||||
let api = AuthApi.token(username: username, password: password)
|
let api = AuthApi.token(username: username, password: password, expiry: 604800)
|
||||||
RequestBuilder.buildRequest(apiRequest: api)
|
RequestBuilder.buildRequest(apiRequest: api)
|
||||||
.validate()
|
.validate()
|
||||||
.responseJSON { response in
|
.responseJSON { response in
|
||||||
|
|
||||||
switch response.result {
|
switch response.response?.statusCode {
|
||||||
case .success:
|
case 200, 201:
|
||||||
|
|
||||||
guard let data = response.data else {
|
guard let data = response.data else {
|
||||||
fatalError("error getting token")
|
fatalError("error getting token")
|
||||||
@ -62,7 +62,7 @@ struct LoginScreen: View {
|
|||||||
keychain["jwt"] = token
|
keychain["jwt"] = token
|
||||||
self.liveUser.loggedIn = true
|
self.liveUser.loggedIn = true
|
||||||
|
|
||||||
case .failure:
|
case _:
|
||||||
|
|
||||||
keychain["username"] = nil
|
keychain["username"] = nil
|
||||||
keychain["password"] = nil
|
keychain["password"] = nil
|
||||||
|
@ -11,6 +11,7 @@ import SwiftyJSON
|
|||||||
|
|
||||||
struct AddPlaylistSheet: View {
|
struct AddPlaylistSheet: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@State private var selectedType = 0
|
@State private var selectedType = 0
|
||||||
@State private var name = ""
|
@State private var name = ""
|
||||||
@State private var errorMessage = ""
|
@State private var errorMessage = ""
|
||||||
@ -92,11 +93,21 @@ struct AddPlaylistSheet: View {
|
|||||||
let api = PlaylistApi.newPlaylist(name: self.name,
|
let api = PlaylistApi.newPlaylist(name: self.name,
|
||||||
type: PlaylistType(rawValue: selectedType) ?? .defaultPlaylist)
|
type: PlaylistType(rawValue: selectedType) ?? .defaultPlaylist)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
self.playlists.append(playlist)
|
|
||||||
self.playlists = self.playlists.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
||||||
|
|
||||||
self.isLoading = false
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
self.presentationMode.wrappedValue.dismiss()
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
|
self.playlists.append(playlist)
|
||||||
|
self.playlists = self.playlists.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
|
|
||||||
|
self.isLoading = false
|
||||||
|
self.presentationMode.wrappedValue.dismiss()
|
||||||
|
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,14 @@ struct PlaylistList: View {
|
|||||||
indexSet.forEach { index in
|
indexSet.forEach { index in
|
||||||
let api = PlaylistApi.deletePlaylist(name: self.liveUser.playlists[index].name)
|
let api = PlaylistApi.deletePlaylist(name: self.liveUser.playlists[index].name)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import SwiftyJSON
|
|||||||
|
|
||||||
struct PlaylistRow: View {
|
struct PlaylistRow: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@Binding var playlist: Playlist
|
@Binding var playlist: Playlist
|
||||||
@State private var showingNetworkError = false
|
@State private var showingNetworkError = false
|
||||||
|
|
||||||
@ -29,10 +30,13 @@ struct PlaylistRow: View {
|
|||||||
RequestBuilder.buildRequest(apiRequest: api)
|
RequestBuilder.buildRequest(apiRequest: api)
|
||||||
.validate()
|
.validate()
|
||||||
.responseJSON{ response in
|
.responseJSON{ response in
|
||||||
switch response.result {
|
|
||||||
case .success:
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
break
|
break
|
||||||
case .failure:
|
case _:
|
||||||
self.showingNetworkError = true
|
self.showingNetworkError = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import SwiftUICharts
|
|||||||
|
|
||||||
struct PlaylistView: View {
|
struct PlaylistView: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@Binding var playlist: Playlist
|
@Binding var playlist: Playlist
|
||||||
|
|
||||||
@State private var showingSheet = false
|
@State private var showingSheet = false
|
||||||
@ -214,10 +215,13 @@ struct PlaylistView: View {
|
|||||||
RequestBuilder.buildRequest(apiRequest: api)
|
RequestBuilder.buildRequest(apiRequest: api)
|
||||||
.validate()
|
.validate()
|
||||||
.responseJSON{ response in
|
.responseJSON{ response in
|
||||||
switch response.result {
|
|
||||||
case .success:
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
break
|
break
|
||||||
case .failure:
|
case _:
|
||||||
self.showingNetworkError = true
|
self.showingNetworkError = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -228,6 +232,14 @@ struct PlaylistView: View {
|
|||||||
let api = PlaylistApi.refreshStats(name: playlist.name)
|
let api = PlaylistApi.refreshStats(name: playlist.name)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//TODO: do better error checking
|
//TODO: do better error checking
|
||||||
}
|
}
|
||||||
@ -241,10 +253,13 @@ struct PlaylistView: View {
|
|||||||
func updatePlaylist(updates: JSON) {
|
func updatePlaylist(updates: JSON) {
|
||||||
let api = PlaylistApi.updatePlaylist(name: playlist.name, updates: updates)
|
let api = PlaylistApi.updatePlaylist(name: playlist.name, updates: updates)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
switch response.result {
|
|
||||||
case .success:
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
debugPrint("success")
|
debugPrint("success")
|
||||||
case .failure:
|
case _:
|
||||||
debugPrint("error")
|
debugPrint("error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -254,11 +269,22 @@ struct PlaylistView: View {
|
|||||||
func refreshPlaylist() {
|
func refreshPlaylist() {
|
||||||
let api = PlaylistApi.getPlaylist(name: self.playlist.name)
|
let api = PlaylistApi.getPlaylist(name: self.playlist.name)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
guard let data = response.data else {
|
|
||||||
fatalError("error getting playlist")
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
|
guard let data = response.data else {
|
||||||
|
fatalError("error getting playlist")
|
||||||
|
}
|
||||||
|
|
||||||
|
self.playlist = PlaylistApi.fromJSON(playlist: data)!
|
||||||
|
|
||||||
|
case _:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
self.playlist = PlaylistApi.fromJSON(playlist: data)!
|
|
||||||
self.isRefreshing = false
|
self.isRefreshing = false
|
||||||
}
|
}
|
||||||
//TODO: do better error checking
|
//TODO: do better error checking
|
||||||
|
@ -11,6 +11,7 @@ import SwiftyJSON
|
|||||||
|
|
||||||
struct AddTagSheet: View {
|
struct AddTagSheet: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@State private var name = ""
|
@State private var name = ""
|
||||||
@State private var errorMessage = ""
|
@State private var errorMessage = ""
|
||||||
@State private var isLoading = false
|
@State private var isLoading = false
|
||||||
@ -74,11 +75,21 @@ struct AddTagSheet: View {
|
|||||||
isLoading = true
|
isLoading = true
|
||||||
let api = TagApi.newTag(tag_id: tag_id)
|
let api = TagApi.newTag(tag_id: tag_id)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
self.tags.append(tag)
|
|
||||||
self.tags = self.tags.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
|
||||||
|
|
||||||
self.isLoading = false
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
self.presentationMode.wrappedValue.dismiss()
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
|
self.tags.append(tag)
|
||||||
|
self.tags = self.tags.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
|
||||||
|
|
||||||
|
self.isLoading = false
|
||||||
|
self.presentationMode.wrappedValue.dismiss()
|
||||||
|
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,14 @@ struct TagList: View {
|
|||||||
let api = TagApi.deleteTag(tag_id: self.liveUser.tags[index].tag_id)
|
let api = TagApi.deleteTag(tag_id: self.liveUser.tags[index].tag_id)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import SwiftyJSON
|
|||||||
|
|
||||||
struct TagRow: View {
|
struct TagRow: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@Binding var tag: Tag
|
@Binding var tag: Tag
|
||||||
@State private var showingNetworkError = false
|
@State private var showingNetworkError = false
|
||||||
|
|
||||||
@ -29,10 +30,13 @@ struct TagRow: View {
|
|||||||
RequestBuilder.buildRequest(apiRequest: api)
|
RequestBuilder.buildRequest(apiRequest: api)
|
||||||
.validate()
|
.validate()
|
||||||
.responseJSON{ response in
|
.responseJSON{ response in
|
||||||
switch response.result {
|
|
||||||
case .success:
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
break
|
break
|
||||||
case .failure:
|
case _:
|
||||||
self.showingNetworkError = true
|
self.showingNetworkError = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import SwiftUICharts
|
|||||||
|
|
||||||
struct TagView: View {
|
struct TagView: View {
|
||||||
|
|
||||||
|
@EnvironmentObject var liveUser: LiveUser
|
||||||
@Binding var tag: Tag
|
@Binding var tag: Tag
|
||||||
|
|
||||||
@State private var isRefreshing = false
|
@State private var isRefreshing = false
|
||||||
@ -119,6 +120,14 @@ struct TagView: View {
|
|||||||
let api = TagApi.runTag(tag_id: tag.tag_id)
|
let api = TagApi.runTag(tag_id: tag.tag_id)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//TODO: do better error checking
|
//TODO: do better error checking
|
||||||
}
|
}
|
||||||
@ -127,6 +136,14 @@ struct TagView: View {
|
|||||||
let api = TagApi.updateTag(tag_id: tag.tag_id, updates: updates)
|
let api = TagApi.updateTag(tag_id: tag.tag_id, updates: updates)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
|
|
||||||
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//TODO: do better error checking
|
//TODO: do better error checking
|
||||||
}
|
}
|
||||||
@ -134,17 +151,28 @@ struct TagView: View {
|
|||||||
func refreshTag() {
|
func refreshTag() {
|
||||||
let api = TagApi.getTag(tag_id: self.tag.tag_id)
|
let api = TagApi.getTag(tag_id: self.tag.tag_id)
|
||||||
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
RequestBuilder.buildRequest(apiRequest: api).responseJSON{ response in
|
||||||
guard let data = response.data else {
|
|
||||||
fatalError("error getting tag")
|
self.liveUser.checkNetworkResponse(response: response)
|
||||||
|
|
||||||
|
switch response.response?.statusCode {
|
||||||
|
case 200, 201:
|
||||||
|
|
||||||
|
guard let data = response.data else {
|
||||||
|
fatalError("error getting tag")
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let json = try? JSON(data: data) else {
|
||||||
|
fatalError("error parsing reponse")
|
||||||
|
}
|
||||||
|
let _tag = TagApi.fromJSON(tag: json["tag"])
|
||||||
|
if let tag = _tag {
|
||||||
|
self.tag = tag
|
||||||
|
}
|
||||||
|
|
||||||
|
case _:
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
guard let json = try? JSON(data: data) else {
|
|
||||||
fatalError("error parsing reponse")
|
|
||||||
}
|
|
||||||
let _tag = TagApi.fromJSON(tag: json["tag"])
|
|
||||||
if let tag = _tag {
|
|
||||||
self.tag = tag
|
|
||||||
}
|
|
||||||
self.isRefreshing = false
|
self.isRefreshing = false
|
||||||
}
|
}
|
||||||
//TODO: do better error checking
|
//TODO: do better error checking
|
||||||
|
Loading…
Reference in New Issue
Block a user