-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
// | ||
// StickerPacks.swift | ||
// Sticker | ||
// | ||
// Created by Gokulprasanth on 11/02/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct StickerPacks: View { | ||
@State private var stickerUrls: [[String]] = [] | ||
|
||
@State private var stickerPackName:String = "https://t.me/addstickers/web_technology_logos" | ||
|
||
@FocusState private var keyboardFocus: Bool | ||
|
||
@State private var packName: String = "" | ||
|
||
@State private var showSheet = false | ||
|
||
@State private var currentIndex:Int = 0 | ||
|
||
var stickerpackName:String { | ||
if (stickerPackName.starts(with: /http.*/.ignoresCase())){ | ||
let name = stickerPackName.split(separator: "/") | ||
return String(name[name.count-1]) | ||
} | ||
return stickerPackName | ||
} | ||
|
||
|
||
var body: some View{ | ||
|
||
TextField("Enter telegram set name or sticker pack link", text: $stickerPackName) | ||
.padding() | ||
.focused($keyboardFocus) | ||
.foregroundStyle(.primary) | ||
.border(.blue) | ||
|
||
|
||
|
||
Button("Fetch Stickers"){ | ||
keyboardFocus = false | ||
|
||
|
||
|
||
FetchStickerUrls(stickerName: stickerpackName){ (stickerUrls, metaData, error) in | ||
|
||
if let error = error{ | ||
debugPrint("Error: ",error) | ||
return | ||
} | ||
|
||
if stickerUrls.count > stickerMaxPackSize { | ||
let noOfPacks = Int(ceil(Double(stickerUrls.count) / Double(stickerMaxPackSize))) | ||
|
||
for packs in 0..<noOfPacks { | ||
|
||
|
||
let startIndex = packs * stickerMaxPackSize | ||
let endIndex = min((packs + 1) * stickerMaxPackSize, stickerUrls.count) | ||
let pack = Array(stickerUrls[startIndex..<endIndex]) | ||
|
||
self.stickerUrls.append(pack) | ||
|
||
} | ||
} else { | ||
self.stickerUrls.append(stickerUrls) | ||
} | ||
|
||
if let metaData = metaData{ | ||
self.packName = metaData.result.title | ||
} | ||
} | ||
} | ||
.disabled(stickerUrls.count>0) | ||
.frame(width: 150,height: 40).background(.blue) | ||
.foregroundStyle(.white) | ||
.clipShape(RoundedRectangle(cornerRadius: 20)) | ||
.contentShape(RoundedRectangle(cornerRadius: 20)) | ||
|
||
ScrollView{ | ||
ForEach(stickerUrls.indices,id: \.self){ index in | ||
|
||
VStack(alignment:.leading,spacing: 10){ | ||
Text("Pack \(index+1)") | ||
LazyVGrid(columns: columns,alignment: .trailing){ | ||
ForEach(stickerUrls[index][0..<grids], id:\.self){ stickerUrl in | ||
AsyncImage(url: URL(string: stickerUrl)!){ img in | ||
switch img { | ||
case .success(let image): | ||
image | ||
.resizable() | ||
.frame(width:70, height:70) | ||
.aspectRatio(contentMode: .fit) | ||
default: | ||
Rectangle() | ||
.foregroundStyle(.gray) | ||
.clipShape(.rect(cornerRadius: 10)) | ||
.frame(width:70, height:70) | ||
} | ||
} | ||
} | ||
}.onTapGesture { | ||
currentIndex = index | ||
showSheet = true | ||
} | ||
} | ||
.sheet(isPresented: $showSheet){ | ||
StickerPackList(stickerUrls: stickerUrls[currentIndex], packName: "\(packName) (\(currentIndex+1) / \(stickerUrls.count))", showSheet: $showSheet) | ||
} | ||
}.navigationTitle("Add Stickers") | ||
}.padding(10) | ||
} | ||
} | ||
|
||
func FetchStickerUrls(stickerName: String, completion: @escaping ([String], StickerMetadata?, Error?) -> Void){ | ||
var stickerIdUrls: [String] = [] | ||
var stickerUrls: [String] = [] | ||
var metaData: StickerMetadata? = nil | ||
let downloadManager = DownloadManager() | ||
|
||
let dispatchGroup = DispatchGroup() | ||
let apiQueue = DispatchQueue.global(qos: .background) | ||
|
||
let stickerPackUrl = "https://api.telegram.org/bot\(token)/getStickerSet?name=\(stickerName)" | ||
|
||
|
||
apiQueue.async { | ||
dispatchGroup.enter() | ||
downloadManager.download(StickerMetadata.self, url: stickerPackUrl){ (data, error) in | ||
defer {dispatchGroup.leave()} | ||
if let error = error { | ||
debugPrint("Error: ", error) | ||
completion([],nil,error) | ||
return | ||
} | ||
guard let data = data else { | ||
debugPrint("Error while unwrapping data: ", error!) | ||
completion([],nil,error) | ||
return | ||
} | ||
metaData = data | ||
data.result.stickers.forEach { stickerMetaData in | ||
let stickerFileUrl = "https://api.telegram.org/bot\(token)/getFile?file_id=\(stickerMetaData.fileId)" | ||
stickerIdUrls.append(stickerFileUrl) | ||
} | ||
|
||
for stickerIdUrl in stickerIdUrls { | ||
dispatchGroup.enter() | ||
downloadManager.download(StickerData.self, url: stickerIdUrl){ (stickerData, error) in | ||
if let error = error { | ||
debugPrint("Error: ", error) | ||
completion([],nil,error) | ||
return | ||
} | ||
|
||
if let stickerData = stickerData { | ||
let stickerUrl = "https://api.telegram.org/file/bot\(token)/\(stickerData.result.filePath)" | ||
|
||
stickerUrls.append(stickerUrl) | ||
} | ||
dispatchGroup.leave() | ||
} | ||
} | ||
|
||
dispatchGroup.notify(queue: .main, execute: { | ||
completion(stickerUrls,metaData, nil) | ||
}) | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// StickerPackList.swift | ||
// Sticker | ||
// | ||
// Created by Gokulprasanth on 11/02/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct StickerPackList: View { | ||
let columns = Array(repeating: GridItem(), count: 4) | ||
|
||
var stickerUrls: [String] = [] | ||
@State private var stickerDatas: [Data] = [] | ||
var index: Int = 0 | ||
var packName: String = "" | ||
@Binding var showSheet: Bool | ||
|
||
var body: some View{ | ||
Text(packName) | ||
.fontWeight(.semibold) | ||
.font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/) | ||
.padding() | ||
ScrollView { | ||
LazyVGrid(columns: columns, alignment: .center){ | ||
ForEach(stickerDatas, id: \.self){ stickerData in | ||
Image(uiImage: UIImage(data: stickerData)!) | ||
.resizable() | ||
.frame(width:70, height:70) | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
} | ||
}.padding() | ||
.onAppear(){ | ||
let processImage = ProcessImage() | ||
for stickerUrl in stickerUrls { | ||
|
||
processImage.addOverlay(url: stickerUrl, size: 50, type: .png){ data in | ||
if let data = data{ | ||
stickerDatas.append(data) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
HStack{ | ||
Button("Close"){ | ||
showSheet = false | ||
} | ||
.frame(maxWidth: .infinity) | ||
.frame(height:50) | ||
.background(.red) | ||
.foregroundStyle(.white) | ||
.clipShape(.rect(cornerRadius: 20)) | ||
|
||
Button("Add to WhatsApp"){ | ||
Task(priority:.high){ | ||
sendToWhatsApp(stickers: stickerUrls, packName: packName ) | ||
} | ||
|
||
} | ||
.frame(maxWidth: .infinity) | ||
.frame(height:50) | ||
.background(.blue) | ||
.foregroundStyle(.white) | ||
.clipShape(.rect(cornerRadius: 20)) | ||
}.padding() | ||
} | ||
} | ||
|
||
|
||
|
||
func sendToWhatsApp(stickers: [String], packName: String) { | ||
let processImage = ProcessImage() | ||
|
||
let stickerSize: CGFloat = 170.6 | ||
|
||
let trayIconSize: CGFloat = 32 | ||
|
||
let dispatchGroup = DispatchGroup() | ||
|
||
let apiQueue = DispatchQueue.global(qos: .background) | ||
|
||
var stickerDatas: [Data] = [] | ||
|
||
let maxStickerSize = 102400 | ||
|
||
|
||
|
||
apiQueue.async { | ||
|
||
for stickerUrl in stickers { | ||
dispatchGroup.enter() | ||
processImage.addOverlay(url: stickerUrl, size: stickerSize, type: .webp) { stickerData in | ||
|
||
defer { dispatchGroup.leave() } | ||
|
||
if let stickerData = stickerData { | ||
if Int64(stickerData.count) < maxStickerSize { | ||
stickerDatas.append(stickerData) | ||
} | ||
} | ||
} | ||
} | ||
|
||
dispatchGroup.notify(queue: .global()) { | ||
processImage.addOverlay(url: stickers[0], size: trayIconSize, type: .png) { thumbData in | ||
if let thumbData = thumbData { | ||
do { | ||
let stickerPack = try StickerPack( | ||
identifier: "com.test.sticker", | ||
name: packName, | ||
publisher: "gokul", | ||
trayImagePNGData: thumbData, | ||
animatedStickerPack: false, | ||
publisherWebsite: "", | ||
privacyPolicyWebsite: "", | ||
licenseAgreementWebsite: "") | ||
|
||
stickerPack.sendToWhatsApp(data: stickerDatas, completionHandler: { | ||
debugPrint("\($0) sent to whatsapp") | ||
}) | ||
} catch { | ||
debugPrint("Error: ", error) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// ContentView.swift | ||
// Sticker | ||
// | ||
// Created by Gokulprasanth on 18/01/24. | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
|
||
let token = "" | ||
let grids:Int = 5 | ||
let stickerMaxPackSize: Int = 30 | ||
let columns = Array(repeating: GridItem(), count: grids) | ||
struct ContentView: View { | ||
|
||
var body: some View { | ||
NavigationStack{ | ||
|
||
ZStack(alignment: .bottomTrailing){ | ||
List{ | ||
ForEach(1..<21){ | ||
Text("dummy list \($0)") | ||
} | ||
} | ||
|
||
NavigationLink{ | ||
StickerPacks() | ||
} label:{ | ||
Image(systemName: "plus") | ||
.padding() | ||
.frame(width: 50, height: 50) | ||
.foregroundStyle(.white) | ||
.background(.blue) | ||
.clipShape(Circle()) | ||
|
||
}.padding() | ||
}.navigationTitle("Stickers") | ||
} | ||
} | ||
} |