Skip to content

[SASU-0115] - Adding WatchOS application #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SampleAppSwiftUI/Configs/AppStore.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ base_url = https:/$()/min-api.cryptocompare.com/data/

webSocket_base_url = wss:/$()/streamer.cryptocompare.com/v2

personal_api =
personal_api =
2 changes: 1 addition & 1 deletion SampleAppSwiftUI/Configs/Development.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ base_url = https:/$()/min-api.cryptocompare.com/data/

webSocket_base_url = wss:/$()/streamer.cryptocompare.com/v2

personal_api =
personal_api =
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ enum CoinNewsServiceEndpoint: TargetEndpointProtocol {
switch self {
case .coinNews(coinCode: let coinCode):
return BaseEndpoint.base.path + String(format: Constants.coinNewsEndpoint, coinCode, Configuration.coinApiKey)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ enum ExampleServiceEndpoint: TargetEndpointProtocol {
switch self {
case .example(let firstParameter, let secondParameter):
return BaseEndpoint.base.path + String(format: Constants.exampleEndpoint, firstParameter, secondParameter)
}
}
}
}
12 changes: 6 additions & 6 deletions SampleAppSwiftUI/Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>webSocket_base_url</key>
<string>$(webSocket_base_url)</string>
<key>base_url</key>
<string>$(base_url)</string>
<key>personal_api</key>
<string>$(personal_api)</string>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down Expand Up @@ -74,5 +68,11 @@
</array>
<key>WebSocket_Base_URL</key>
<string>$(WebSocket_BASE_URL)</string>
<key>base_url</key>
<string>$(base_url)</string>
<key>personal_api</key>
<string>$(personal_api)</string>
<key>webSocket_base_url</key>
<string>$(webSocket_base_url)</string>
</dict>
</plist>
10 changes: 10 additions & 0 deletions SampleAppSwiftUI/SampleAppSwiftUI.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.tr.com.adesso.SampleAppSwiftUI</string>
</array>
</dict>
</plist>
54 changes: 50 additions & 4 deletions SampleAppSwiftUI/Utility/Managers/StorageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
//

import SwiftUI
import WatchConnectivity

final class StorageManager: ObservableObject {
final class StorageManager: NSObject, ObservableObject {

static let shared = StorageManager()

@AppStorage("favoriteCoins") var favoriteCoins: [CoinData] = [] {
private let watchSession = WCSession.default

@AppStorage("favoriteCoins", store: .standard)
var favoriteCoins: [CoinData] = [] {
didSet {
objectWillChange.send()
sendFavoritesToWatchApp()
}
}

private init() { }
override init() {
super.init()
initWatchConnection()
}

func isCoinFavorite(_ coinCode: CoinCode) -> Bool {
favoriteCoins.contains { coinData in
Expand All @@ -40,6 +47,19 @@ final class StorageManager: ObservableObject {
}
}

private func sendFavoritesToWatchApp() {
if watchSession.isReachable {
guard let data: Data = favoriteCoins.encode() else { return }
let message = ["favoriteCoinsData": data]

watchSession.sendMessage(message, replyHandler: { response in
print(response)
}, errorHandler: { error in
print(error)
})
}
}

private func addFavoriteCoin(coinData: CoinData) {
DispatchQueue.main.async {
self.favoriteCoins.append(coinData)
Expand All @@ -61,5 +81,31 @@ final class StorageManager: ObservableObject {
toggleFavoriteCoin(coinData: coinData)
return output
}
}

extension StorageManager: WCSessionDelegate {

func initWatchConnection() {
if watchSession.activationState == .notActivated {
watchSession.delegate = self
watchSession.activate()
}
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
print("got message: \(message)")
replyHandler(message)
}

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
print("activated: \(activationState)")
}

func sessionDidBecomeInactive(_ session: WCSession) {
print("inactive: \(session)")
}

func sessionDidDeactivate(_ session: WCSession) {
print("deactivated: \(session)")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// SampleWatchAppSwiftUI_Watch_AppTests.swift
// SampleWatchAppSwiftUI Watch AppTests
//
// Created by Yildirim, Alper on 27.09.2023.
//

import XCTest
@testable import SampleWatchAppSwiftUI_Watch_App
// swiftlint:disable type_name
final class SampleWatchAppSwiftUI_Watch_AppTests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Tests marked async will run the test method on an arbitrary thread managed by the Swift runtime.
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
// swiftlint:enable type_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// SampleWatchAppSwiftUI_Watch_AppUITests.swift
// SampleWatchAppSwiftUI Watch AppUITests
//
// Created by Yildirim, Alper on 27.09.2023.
//

import XCTest
// swiftlint:disable type_name
final class SampleWatchAppSwiftUI_Watch_AppUITests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.

// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()

// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
// This measures how long it takes to launch your application.
measure(metrics: [XCTApplicationLaunchMetric()]) {
XCUIApplication().launch()
}
}
}
}
// swiftlint:enable type_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// SampleWatchAppSwiftUIITestsLaunchTests.swift
// SampleWatchAppSwiftUI Watch AppUITests
//
// Created by Yildirim, Alper on 27.09.2023.
//

import XCTest

final class SampleWatchAppSwiftUIUITestsLaunchTests: XCTestCase {

override class var runsForEachTargetApplicationUIConfiguration: Bool {
true
}

override func setUpWithError() throws {
continueAfterFailure = false
}

func testLaunch() throws {
let app = XCUIApplication()
app.launch()

// Insert steps here to perform after app launch but before taking a screenshot,
// such as logging into a test account or navigating somewhere in the app

let attachment = XCTAttachment(screenshot: app.screenshot())
attachment.name = "Launch Screen"
attachment.lifetime = .keepAlways
add(attachment)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "watchos",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions SampleWatchAppSwiftUI/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
15 changes: 15 additions & 0 deletions SampleWatchAppSwiftUI/Configs/AppStore.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AppStore.xcconfig
// SampleWatchAppSwiftUI
//
// Created by Yildirim, Alper on 17.10.2023.
//

// Configuration settings file format documentation can be found at:
// https://help.apple.com/xcode/#/dev745c5c974

base_url = https:/$()/min-api.cryptocompare.com/data/

webSocket_base_url = wss:/$()/streamer.cryptocompare.com/v2

personal_api =
78 changes: 78 additions & 0 deletions SampleWatchAppSwiftUI/Configs/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Configuration.swift
// SampleWatchAppSwiftUI
//
// Created by Yildirim, Alper on 17.10.2023.
//

import Foundation

final class Configuration: ConfigurationProtocol {
static var isProduction: Bool {
#if Production
return true
#else
return false
#endif
}

static var isAppStore: Bool {
#if AppStore
return true
#else
return false
#endif
}

static var isDevelopment: Bool {
#if Development
return true
#else
return false
#endif
}

static var baseURL: String {
let url: String? = try? self.value(for: "base_url")
return url ?? ""
}

static var coinApiKey: String {
let key: String? = try? self.value(for: "personal_api")
guard let key, !key.isEmpty else {
/// Get your API key from https://www.cryptocompare.com/
#warning("Please Enter an API Key")
return ""
}
return key ?? ""
}

static var webSocketBaseUrl: String {
let url: String? = try? self.value(for: "webSocket_base_url")
return url ?? ""
}
}

protocol ConfigurationProtocol {}

extension ConfigurationProtocol {
static func value<T>(for key: String) throws -> T where T: LosslessStringConvertible {
guard let object = Bundle.main.object(forInfoDictionaryKey: key) else {
throw ConfigurationError.missingKey
}

switch object {
case let value as T:
return value
case let string as String:
guard let value = T(string) else { fallthrough }
return value
default:
throw ConfigurationError.invalidValue
}
}
}

enum ConfigurationError: Swift.Error {
case missingKey, invalidValue
}
Loading