Skip to content

Commit

Permalink
Merge pull request #54 from IsaacMarovitz/part-two
Browse files Browse the repository at this point in the history
Better Discord RPC
  • Loading branch information
Depal1 authored Jan 6, 2023
2 parents f083733 + c036485 commit f420f3f
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions PlayTools/DiscordActivity/DiscordIPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class DiscordIPC {
} else {
ipc = SwordRPC(appId: custom.applicationID)
}
let activity = createActivity(from: custom)
ipc.connect()
ipc.setPresence(activity)
Task.init(priority: .background) {
let activity = await createActivity(from: custom)
ipc.connect()
ipc.setPresence(activity)
}
}
}

func createActivity(from custom: DiscordActivity) -> RichPresence {
func createActivity(from custom: DiscordActivity) async -> RichPresence {
var activity = RichPresence()

if custom.details.isEmpty {
Expand All @@ -48,10 +50,10 @@ class DiscordIPC {
activity.assets.largeText = poweredStr
}

let logo = "https://github.com/PlayCover/playcover-website/raw/master/src/assets/images/play-cover.png"
let logo = "https://raw.githubusercontent.com/PlayCover/PlaySite/master/src/assets/square-logo.png"
if custom.image.isEmpty {
let bundleID = Bundle.main.infoDictionary?["CFBundleIdentifier"] as? String ?? ""
if let appImage = loadImage(bundleID: bundleID) {
if let appImage = await loadImage(bundleID: bundleID) {
activity.assets.largeImage = appImage
activity.assets.largeText = nil
activity.assets.smallImage = logo
Expand All @@ -73,8 +75,72 @@ class DiscordIPC {
return activity
}

// TODO : load App icon image from appstore
func loadImage(bundleID: String) -> String? {
return nil
func loadImage(bundleID: String) async -> String? {
let lookup = "http://itunes.apple.com/lookup?bundleId=\(bundleID)"
guard let url = URL(string: lookup) else { return nil }

do {
let (data, _) = try await URLSession.shared.data(for: URLRequest(url: url))
let decoder = JSONDecoder()
let jsonResult: ITunesResponse = try decoder.decode(ITunesResponse.self, from: data)
if jsonResult.resultCount > 0 {
return jsonResult.results[0].artworkUrl512
} else {
return nil
}
} catch {
return nil
}
}
}

struct ITunesResult: Decodable {
var isGameCenterEnabled: Bool
var supportedDevices: [String]
var features: [String]
var advisories: [String]
var screenshotUrls: [String]
var ipadScreenshotUrls: [String]
var appletvScreenshotUrls: [String]
var artworkUrl60: String
var artworkUrl512: String
var artworkUrl100: String
var artistViewUrl: String
var kind: String
var isVppDeviceBasedLicensingEnabled: Bool
var currentVersionReleaseDate: String
var releaseNotes: String
var description: String
var trackId: Int
var trackName: String
var bundleId: String
var sellerName: String
var genreIds: [String]
var primaryGenreName: String
var primaryGenreId: Int
var currency: String
var formattedPrice: String
var contentAdvisoryRating: String
var averageUserRatingForCurrentVersion: Float
var userRatingCountForCurrentVersion: Int
var trackViewUrl: String
var trackContentRating: String
var averageUserRating: Float
var minimumOsVersion: String
var trackCensoredName: String
var languageCodesISO2A: [String]
var fileSizeBytes: String
var releaseDate: String
var artistId: Int
var artistName: String
var genres: [String]
var price: Float
var version: String
var wrapperType: String
var userRatingCount: Int
}

struct ITunesResponse: Decodable {
var resultCount: Int
var results: [ITunesResult]
}

0 comments on commit f420f3f

Please sign in to comment.