Skip to content

Instantly share code, notes, and snippets.

View KrauserHuang's full-sized avatar
:bowtie:
Learning iOS right now!

Krauser Huang KrauserHuang

:bowtie:
Learning iOS right now!
  • Taiwan
View GitHub Profile
// fetch google sheet all datas and store in menus
func fetchItem() {
// remember to switch googlesheetid with your own one!
let urlStr = "https://spreadsheets.google.com/feeds/list/googlesheetid/od6/public/values?alt=json"
if let url = URL(string: urlStr) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
let decoder = JSONDecoder()
do {
let searchResponse = try decoder.decode(Menu.self, from: data)
struct Menu: Codable {
let feed: Feed
}
struct Feed: Codable {
let entry: [DrinkData]
}
// 全為自定義,StringType為DrinkData property的下一層,這一層只有gsx$ + property,用CodingKeys去轉換
struct DrinkData: Codable {
let drink: StringType
@IBAction func playPreviousPressed(_ sender: UIButton) {
if repeatType == .one {
playingIndex = songs.firstIndex(of: songPlayed!)
songPlayed = songs![playingIndex]
configurePlayer(song: songPlayed!)
player.play()
} else {
playingIndex = songs.firstIndex(of: songPlayed!)
print("first: List\(playingIndex!)")
playingIndex -= 1
@IBAction func playNextPressed(_ sender: UIButton) {
switch player.timeControlStatus {
case .paused:
playNPause.setImage(UIImage(systemName: SystemName.playFill), for: .normal)
player.pause()
let time = CMTime(value: 0, timescale: 1)
player.seek(to: time)
playingIndex = songs.firstIndex(of: songPlayed!)
playingIndex += 1
if playingIndex == songs?.count {
func repeatStateCheck() {
switch repeatType {
case .none:
playingIndex = songs.firstIndex(of: songPlayed!)
playingIndex += 1
if playingIndex == songs.count {
player.pause()
playNPause.setImage(UIImage(systemName: SystemName.playFill), for: .normal)
} else {
songPlayed = songs[playingIndex]
// 隨機播放,預設無,點選變成有
@IBAction func playShufflePressed(_ sender: UIButton) {
switch shuffleType {
case .no:
shuffleType = .yes
playShuffle.setImage(UIImage(systemName: SystemName.shuffleCircleFill), for: .normal)
songs?.shuffle()
case .yes:
shuffleType = .no
playShuffle.setImage(UIImage(systemName: SystemName.shuffleCircle), for: .normal)
func fetchMusic() {
if let urlStr = "https://itunes.apple.com/search?term=宇多田光&media=music&country=JP".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let url = URL(string: urlStr) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data {
let decoder = JSONDecoder()
do {
let result = try decoder.decode(MusicOnline.self, from: data)
self.showOnlineFile = result.results
} catch {
// AutoLayout
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
let segmentedControl: UISegmentedControl = {
let sc = UISegmentedControl(items: ["Local", "Online Preview"])
sc.addTarget(self, action: #selector(segmentChanged), for: .valueChanged)
sc.selectedSegmentIndex = 0
return sc
}()
let tableView = UITableView(frame: .zero, style: .plain)
let stackView = UIStackView(arrangedSubviews: [segmentedControl, tableView])
stackView.axis = .vertical
func loadUser() {
db.collection(Constant.FireStore.users).getDocuments { (querySnapshot, error) in
if let error = error {
print("There was an issue retrieving data to Firestore. \(error)")
} else {
if let snapshotDocuments = querySnapshot?.documents {
for doc in snapshotDocuments {
let data = doc.data()
if let userName = data[Constant.FireStore.userName] as? String {
// label顯示速度有差,再確認