Skip to content

Commit

Permalink
added logs view with cool bg, and respose data gets appended in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Aayush9029 committed Sep 3, 2021
1 parent 3bcdbb1 commit 0173355
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 26 deletions.
4 changes: 4 additions & 0 deletions NativeTwitch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
C26994882647486000B3DCF7 /* CleanButtton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C26994872647486000B3DCF7 /* CleanButtton.swift */; };
C26FBE55264679F1006818AB /* TwitchData.swift in Sources */ = {isa = PBXBuildFile; fileRef = C26FBE54264679F1006818AB /* TwitchData.swift */; };
C26FBE5A26467B26006818AB /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = C26FBE5926467B26006818AB /* Constants.swift */; };
C271885726E273E2009C8002 /* LogText.swift in Sources */ = {isa = PBXBuildFile; fileRef = C271885626E273E2009C8002 /* LogText.swift */; };
C2A7BCF026462B030038CFB5 /* NativeTwitchApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2A7BCEF26462B030038CFB5 /* NativeTwitchApp.swift */; };
C2A7BCF226462B030038CFB5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2A7BCF126462B030038CFB5 /* ContentView.swift */; };
C2A7BCF426462B050038CFB5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C2A7BCF326462B050038CFB5 /* Assets.xcassets */; };
Expand All @@ -29,6 +30,7 @@
C26994872647486000B3DCF7 /* CleanButtton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CleanButtton.swift; sourceTree = "<group>"; };
C26FBE54264679F1006818AB /* TwitchData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TwitchData.swift; sourceTree = "<group>"; };
C26FBE5926467B26006818AB /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
C271885626E273E2009C8002 /* LogText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogText.swift; sourceTree = "<group>"; };
C2A7BCEC26462B030038CFB5 /* NativeTwitch.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NativeTwitch.app; sourceTree = BUILT_PRODUCTS_DIR; };
C2A7BCEF26462B030038CFB5 /* NativeTwitchApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeTwitchApp.swift; sourceTree = "<group>"; };
C2A7BCF126462B030038CFB5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -119,6 +121,7 @@
C26994802647255000B3DCF7 /* StreamRowView.swift */,
C269948526472B6500B3DCF7 /* CustomImageStruct.swift */,
C26994872647486000B3DCF7 /* CleanButtton.swift */,
C271885626E273E2009C8002 /* LogText.swift */,
);
path = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -204,6 +207,7 @@
files = (
C26994882647486000B3DCF7 /* CleanButtton.swift in Sources */,
C26FBE5A26467B26006818AB /* Constants.swift in Sources */,
C271885726E273E2009C8002 /* LogText.swift in Sources */,
C26FBE55264679F1006818AB /* TwitchData.swift in Sources */,
C2A7BCF226462B030038CFB5 /* ContentView.swift in Sources */,
C2A7BCF026462B030038CFB5 /* NativeTwitchApp.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,22 @@
uuid = "BB36A529-94AC-451E-B423-A76E2B4DC63E"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "187B7187-BDBB-43DF-88B5-08019410945D"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "NativeTwitch/Model/TwitchData.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "143"
endingLineNumber = "143"
landmarkName = "addToLogs(response:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
12 changes: 8 additions & 4 deletions NativeTwitch/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ struct ContentView: View {

var body: some View{
Group{
if twitchData.status != .finished{
if twitchData.status != .streamLoaded{
Text("Loading Streams")
.font(.title)
.bold()
.foregroundColor(.gray.opacity(0.5))
.onAppear(perform: {

})

}
if (twitchData.status == .streamLoaded && twitchData.getStreamData().count == 0) {
Text("All streams are offline :(")
.font(.title)
.bold()
.foregroundColor(.gray.opacity(0.5))
}
else{
VStack {
Expand Down
13 changes: 11 additions & 2 deletions NativeTwitch/Model/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ struct Stream: Hashable{
var user_logo: String?
}

enum StatusStates {
case badOuath, badClient, badScopes, userValidating, userValidated, userLoading, userLoaded, streamLoading, streamLoaded, finished
enum StatusStates: String {
case starting = "Starting.."
case badOuath = "Incorrect Access Token"
case badClient = "Incorrect Client ID"
case badScopes = "BAD Scopes (Client ID / Access Token)"
case userValidating = "Validating User"
case userValidated = "User Validated"
case userLoading = "Loading User Data"
case userLoaded = "Got User Data"
case streamLoading = "Loading Streams"
case streamLoaded = "Stream Has Been Loaded"
}


Expand Down
36 changes: 24 additions & 12 deletions NativeTwitch/Model/TwitchData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class TwitchData: ObservableObject{
// VIEW MODEL CODE: App is too small to make a seperate viewmodel file
@Published var showingSettings: Bool = false


// Stores current responses and states if it's not something that's not expected
@Published var logs = [String]()

@AppStorage("twitchClientID") var twitchClientID = ""
@AppStorage("oauthToken") var oauthToken = ""
@AppStorage("streamlinkLocation") var streamlinkLocation = "/opt/homebrew/bin/streamlink"
Expand All @@ -25,17 +27,18 @@ class TwitchData: ObservableObject{
var user: User
var streams: [Stream]


init() {
self.user = User(client_id: "", oauthToken: "", name: "", user_id: "", isValid: false)
self.streams = []
self.status = .userValidating
self.status = .starting
self.startFetch()
}

func startFetch(){
self.logs = []
self.user = User(client_id: "", oauthToken: "", name: "", user_id: "", isValid: false)
self.streams = []
self.status = .userValidating

self.validateTokens() // Changes StatusStates that's all
self.fetchUserData() // Fetches user data and excecutes get followed internally
Expand All @@ -59,14 +62,19 @@ class TwitchData: ObservableObject{
if json["client_id"].string == self.twitchClientID{
if json["scopes"] != ["user:read:follows"]{
self.status = .badScopes
self.addToLogs(response: json.rawString())
}else{
self.status = .userValidated
self.addToLogs(response: json.rawString())
}

self.addToLogs(response: json.rawString())
}else{
self.status = .badClient
self.addToLogs(response: json.rawString())
}

}else{
self.status = .badScopes
self.addToLogs(response: response.description)
}
}
}
Expand All @@ -90,19 +98,18 @@ class TwitchData: ObservableObject{
self.user = User(client_id: self.twitchClientID, oauthToken: self.oauthToken, name: name, user_id: json["user_id"].string!, isValid: true)
}else{
self.status = .badOuath
self.addToLogs(response: json.rawString())
}

self.status = .userLoaded
self.addFollowedStreams()

}
}
}


func addFollowedStreams(){
self.status = .streamLoading

self.addToLogs()
let url = "https://api.twitch.tv/helix/streams/followed"

let headers: HTTPHeaders = [
Expand All @@ -122,18 +129,23 @@ class TwitchData: ObservableObject{
let d = subJson

self.streams.append(Stream(user_name: d["user_name"].string!, user_id: d["user_id"].string!, viewer_count: d["viewer_count"].int!, type: d["type"].string!, game_name: d["game_name"].string!, title: d["title"].string!))

}
self.status = .streamLoaded
self.status = .finished
self.addToLogs(response: json.rawString())
}else{
self.status = .badScopes
self.addToLogs()
}
}
}



func addToLogs(response: String? = nil){
logs.append("\(Date()) | \(self.status.rawValue)")
if response != nil{
logs.append("response: \(response!)\n")
}
}

func getUserData() -> User{
return self.user
Expand Down
32 changes: 28 additions & 4 deletions NativeTwitch/NativeTwitchApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import SwiftUI
struct NativeTwitchApp: App {
@State var hightLightWarnings = false
@StateObject var twitchData = TwitchData()

@State var showingLogs = false

var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(twitchData)
.frame(width: 300, height: 400)
.fixedSize()
}
.windowStyle(HiddenTitleBarWindowStyle())
.commands {
Expand All @@ -35,10 +35,34 @@ struct NativeTwitchApp: App {
}
}
Settings {
SettingsView(user: twitchData.user)
.frame(width: 300, height: 200)
SettingsView(showingLogs: $showingLogs).background(VisualEffectView(material: NSVisualEffectView.Material.sidebar, blendingMode: NSVisualEffectView.BlendingMode.behindWindow))
.environmentObject(twitchData)
.frame(width: 300, height: showingLogs ? 500: 270)
.fixedSize()
}

}
}



struct VisualEffectView: NSViewRepresentable
{
let material: NSVisualEffectView.Material
let blendingMode: NSVisualEffectView.BlendingMode

func makeNSView(context: Context) -> NSVisualEffectView
{
let visualEffectView = NSVisualEffectView()
visualEffectView.material = material
visualEffectView.blendingMode = blendingMode
visualEffectView.state = NSVisualEffectView.State.active
return visualEffectView
}

func updateNSView(_ visualEffectView: NSVisualEffectView, context: Context)
{
visualEffectView.material = material
visualEffectView.blendingMode = blendingMode
}
}
25 changes: 25 additions & 0 deletions NativeTwitch/View/LogText.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// BoldTextView.swift
// BoldTextView
//
// Created by Aayush Pokharel on 2021-09-03.
//

import SwiftUI

struct LogText: View {
let text: String
var color: Color = .gray

var body: some View {
Text(text)
.font(.caption2)
.foregroundColor(.gray)
}
}

struct BoldTextView_Previews: PreviewProvider {
static var previews: some View {
LogText(text: "All streams are offline :(", color: .gray)
}
}
48 changes: 44 additions & 4 deletions NativeTwitch/View/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ struct SettingsView: View {
@AppStorage("twitchClientID") var twitchClientID = ""
@AppStorage("oauthToken") var oauthToken = ""
@AppStorage("streamlinkLocation") var streamlinkLocation = "/opt/homebrew/bin/streamlink"
@EnvironmentObject var twitchData: TwitchData

@State var logs = [String]()
@Binding var showingLogs: Bool

var user: User
var body: some View {
VStack{
VStack {
HStack{
VStack(alignment: .leading){
Text("\(user.name)")
Text("\(twitchData.user.name)")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.red)
.lineLimit(1)
}
Spacer()
}.padding(.horizontal)
}.padding()
VStack(spacing: 10){
TextField("Your Twitch Client ID", text: $twitchClientID)

Expand Down Expand Up @@ -52,12 +56,48 @@ struct SettingsView: View {
}
}.padding(.horizontal)

}
Divider()
VStack(alignment: .leading){
HStack{
Text("Logs")
.font(.title3.bold())
.padding(.top, 5)
Spacer()
Image(systemName: showingLogs ? "chevron.up" : "chevron.down")
.font(.title3.bold())
.foregroundColor(.blue)
.padding(5)
.background(Color.blue.opacity(0.25))
.cornerRadius(5)
.padding(.trailing)
.onTapGesture {
withAnimation {
showingLogs.toggle()
}
}
}
Spacer()
Group{
if showingLogs{
ScrollView{
VStack(alignment: .leading){

ForEach(twitchData.logs, id: \.self){log in
LogText(text: log, color: .gray)
}
}
}
}
}
}.padding(.horizontal)
}
}
}

struct UserView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(user: exampleUser)
SettingsView(showingLogs: .constant(true))
.environmentObject(TwitchData())
}
}

0 comments on commit 0173355

Please sign in to comment.