Skip to content
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

feat: implement SetupPreferences to allow usage without Xcode / Android SDK #74

Merged
merged 3 commits into from
Oct 16, 2023
Merged
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
12 changes: 12 additions & 0 deletions MiniSim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF42AD4361C0008D38B /* SetupPreferences.swift */; };
76059BF72AD449DC0008D38B /* OnboardingHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF62AD449DC0008D38B /* OnboardingHeader.swift */; };
76059BF92AD558C30008D38B /* SetupItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF82AD558C30008D38B /* SetupItemView.swift */; };
7610992D2A3F95850067885A /* MiniSim.sdef in Resources */ = {isa = PBXBuildFile; fileRef = 7610992C2A3F95850067885A /* MiniSim.sdef */; };
7610992F2A3F95D90067885A /* NSScriptCommand+utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7610992E2A3F95D90067885A /* NSScriptCommand+utils.swift */; };
7625140B2992B46D0060A225 /* Pasteboard+utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7625140A2992B46D0060A225 /* Pasteboard+utils.swift */; };
Expand Down Expand Up @@ -76,6 +79,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
76059BF42AD4361C0008D38B /* SetupPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupPreferences.swift; sourceTree = "<group>"; };
76059BF62AD449DC0008D38B /* OnboardingHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingHeader.swift; sourceTree = "<group>"; };
76059BF82AD558C30008D38B /* SetupItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupItemView.swift; sourceTree = "<group>"; };
7610992C2A3F95850067885A /* MiniSim.sdef */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = MiniSim.sdef; sourceTree = "<group>"; };
7610992E2A3F95D90067885A /* NSScriptCommand+utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSScriptCommand+utils.swift"; sourceTree = "<group>"; };
7625140A2992B46D0060A225 /* Pasteboard+utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Pasteboard+utils.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -297,6 +303,9 @@
767A9CC829C320ED00554193 /* OnboardingButton.swift */,
76630F2A29C718F500FB64F9 /* AndroidPathInput.swift */,
767DDF6729D32ABC005E6F32 /* ReadyPopOver.swift */,
76059BF42AD4361C0008D38B /* SetupPreferences.swift */,
76059BF62AD449DC0008D38B /* OnboardingHeader.swift */,
76059BF82AD558C30008D38B /* SetupItemView.swift */,
);
path = Onboarding;
sourceTree = "<group>";
Expand Down Expand Up @@ -480,11 +489,13 @@
76489D5C29BFCA330070EF03 /* OnboardingItem.swift in Sources */,
7645D5012982E6FA00019227 /* main.swift in Sources */,
76F2A914299050F9002D4EF6 /* UserDefaults+Configuration.swift in Sources */,
76059BF72AD449DC0008D38B /* OnboardingHeader.swift in Sources */,
763121902A12B45000EE7F48 /* CustomCommandFormViewModel.swift in Sources */,
7630B2772986D65800D8B57D /* Bundle+appName.swift in Sources */,
763121892A12AF9C00EE7F48 /* Command.swift in Sources */,
767DDF6829D32ABC005E6F32 /* ReadyPopOver.swift in Sources */,
764BA3E92A5AD418003A78AF /* GetDevicesCommand.swift in Sources */,
76059BF92AD558C30008D38B /* SetupItemView.swift in Sources */,
76630F2929BE09D800FB64F9 /* Parameter.swift in Sources */,
7630B27C2987207200D8B57D /* Menu.swift in Sources */,
762CF1E02981968F00099999 /* String+match.swift in Sources */,
Expand All @@ -495,6 +506,7 @@
76F2A91929924242002D4EF6 /* AndroidSubMenuItem.swift in Sources */,
764BA3ED2A5AD478003A78AF /* GetCommands.swift in Sources */,
7630B26D2986B4FD00D8B57D /* KeyboardShortcuts.swift in Sources */,
76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */,
7684FAAF29D202F500230BB0 /* AndroidHomeError.swift in Sources */,
7645D4BE2982A1B100019227 /* DeviceService.swift in Sources */,
765ABF382A8BECD900A063CB /* ExecuteCommand.swift in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions MiniSim/Assets.xcassets/android_studio.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "android_studio.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions MiniSim/Assets.xcassets/xcode.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "xcode.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file added MiniSim/Assets.xcassets/xcode.imageset/xcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions MiniSim/Extensions/UserDefaults+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extension UserDefaults {
static let commands = "commands"
static let androidHome = "androidHome"
static let isOnboardingFinished = "isOnboardingFinished"
static let enableiOSSimulators = "enableiOSSimulators"
static let enableAndroidEmulators = "enableAndroidEmulators"
}

@objc dynamic public var androidHome: String? {
Expand All @@ -34,4 +36,14 @@ extension UserDefaults {
get { object(forKey: Keys.commands) as? Data }
set { set(newValue, forKey: Keys.commands) }
}

public var enableiOSSimulators: Bool {
get { bool(forKey: Keys.enableiOSSimulators) }
set { set(newValue, forKey: Keys.enableiOSSimulators) }
}

public var enableAndroidEmulators: Bool {
get { bool(forKey: Keys.enableAndroidEmulators) }
set { set(newValue, forKey: Keys.enableAndroidEmulators) }
}
}
13 changes: 7 additions & 6 deletions MiniSim/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ class Menu: NSMenu {
}

func getDevices() {
if UserDefaults.standard.androidHome == nil {
return
}
DispatchQueue.global(qos: .userInitiated).async {
do {
var devicesArray: [Device] = []
try devicesArray.append(contentsOf: DeviceService.getAndroidDevices())
try devicesArray.append(contentsOf: DeviceService.getIOSDevices())
if UserDefaults.standard.enableiOSSimulators {
try devicesArray.append(contentsOf: DeviceService.getIOSDevices())
}
if UserDefaults.standard.enableAndroidEmulators && UserDefaults.standard.androidHome != nil {
try devicesArray.append(contentsOf: DeviceService.getAndroidDevices())
}
self.devices = devicesArray
} catch {
NSAlert.showError(message: error.localizedDescription)
Expand Down Expand Up @@ -163,7 +164,7 @@ class Menu: NSMenu {

DispatchQueue.main.async {
let iosDevicesCount = self.devices.filter({ $0.platform == .ios }).count
self.safeInsertItem(menuItem, at: isAndroid ? (isFirst ? index : iosDevicesCount) + 3 : 1)
self.safeInsertItem(menuItem, at: isAndroid && UserDefaults.standard.enableiOSSimulators ? (isFirst ? index : iosDevicesCount) + 3 : 1)
}

}
Expand Down
11 changes: 11 additions & 0 deletions MiniSim/MenuItems/MenuSections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ enum MenuSections: Int, CaseIterable {
return item
}

var attachItem: Bool {
switch self {
case .iOSHeader, .separator1, .clearDerrivedData:
return UserDefaults.standard.enableiOSSimulators
case .androidHeader, .separator2:
return UserDefaults.standard.enableAndroidEmulators
default:
return true
}
}

var keyEquivalent: String {
switch self {
case .quit:
Expand Down
51 changes: 33 additions & 18 deletions MiniSim/MiniSim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ class MiniSim: NSObject {

override init() {
super.init()
menu = Menu()
statusItem.menu = menu

settingsController.window?.delegate = self

appendMenu()
menu.getDevices()
setDefaultValues()
initObservers()

setup()
}

deinit {
Expand Down Expand Up @@ -77,24 +76,35 @@ class MiniSim: NSObject {
self.statusItem.button?.performClick(self)
}

private func setup() {
if !UserDefaults.standard.isOnboardingFinished {
onboarding.show()
return
}
menu = Menu()
statusItem.menu = menu
setMenuImage()
populateSections()

menu.getDevices()
}

private func initObservers() {
isOnboardingFinishedObserver = UserDefaults.standard.observe(\.isOnboardingFinished, options: .new) { _, _ in
if UserDefaults.standard.isOnboardingFinished == true {
self.appendMenu()
self.setup()
self.onboarding.showPopOver(button: self.statusItem.button)
}
}
NotificationCenter.default.addObserver(self, selector: #selector(toggleSuccessCheckmark), name: .commandDidSucceed, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handleDeviceDeleted), name: .deviceDeleted, object: nil)
}

private func appendMenu() {
if !(UserDefaults.standard.androidHome != nil && UserDefaults.standard.isOnboardingFinished) {
onboarding.show()
return
}
setMenuImage()
populateSections()
private func setDefaultValues() {
UserDefaults.standard.register(defaults: [
UserDefaults.Keys.enableAndroidEmulators: true,
UserDefaults.Keys.enableiOSSimulators: true
])
}

private func setMenuImage() {
Expand Down Expand Up @@ -158,14 +168,19 @@ class MiniSim: NSObject {
if !menu.items.isEmpty {
return
}
MenuSections.allCases.map({$0.menuItem}).forEach { item in
if item.tag >= MenuSections.clearDerrivedData.rawValue {
item.action = #selector(menuItemAction)
item.target = self
MenuSections.allCases.forEach { item in
if (!item.attachItem) {
return
}

let menuItem = item.menuItem
if menuItem.tag >= MenuSections.clearDerrivedData.rawValue {
menuItem.action = #selector(menuItemAction)
menuItem.target = self
} else {
item.isEnabled = false
menuItem.isEnabled = false
}
menu.addItem(item)
menu.addItem(menuItem)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion MiniSim/Views/Onboarding/AndroidPathInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct AndroidPathInput: View {
} catch {
if let onSave { onSave(false) }
androidHomeError = error
print(error.localizedDescription)
}
}

Expand Down
21 changes: 21 additions & 0 deletions MiniSim/Views/Onboarding/OnboardingHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// OnboardingHeader.swift
// MiniSim
//
// Created by Oskar Kwaśniewski on 09/10/2023.
//

import SwiftUI

struct OnboardingHeader: View {
var title: String
var subTitle: String

var body: some View {
Text(title)
.font(.largeTitle)
.padding(.bottom, 5)
Text(subTitle)
.multilineTextAlignment(.center)
}
}
32 changes: 27 additions & 5 deletions MiniSim/Views/Onboarding/OnboardingPager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

enum OnboardingPages: CaseIterable {
case welcome
case setupPreferences
case setup
case permissions

Expand All @@ -17,6 +18,8 @@ enum OnboardingPages: CaseIterable {
switch self {
case .welcome:
WelcomeView(goToNextPage: goToNextPage)
case .setupPreferences:
SetupPreferences(goToNextPage: goToNextPage)
case .setup:
SetupView(goToNextPage: goToNextPage)
case .permissions:
Expand All @@ -27,6 +30,9 @@ enum OnboardingPages: CaseIterable {

struct OnboardingPager: View {
@State private var currentPage: OnboardingPages = .welcome
var pageIndex: Int {
OnboardingPages.allCases.firstIndex(of: currentPage) ?? 0
}

func goToNextPage() {
let allPages = OnboardingPages.allCases
Expand All @@ -35,12 +41,28 @@ struct OnboardingPager: View {
}
}

func goToPreviousPage() {
let allPages = OnboardingPages.allCases
if let index = allPages.firstIndex(of: currentPage), index > 0 {
currentPage = allPages[index - 1]
}
}

var body: some View {
VStack {
ForEach(OnboardingPages.allCases, id: \.self) { page in
if currentPage == page {
page.view(goToNextPage: goToNextPage)
.frame(maxWidth: 350, alignment: .center)
ZStack {
if (pageIndex > 0) {
Button("Go Back") {
goToPreviousPage()
}
.buttonStyle(.link)
.position(x: 35, y: 20)
}
VStack {
ForEach(OnboardingPages.allCases, id: \.self) { page in
if currentPage == page {
page.view(goToNextPage: goToNextPage)
.frame(maxWidth: 350, alignment: .center)
}
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions MiniSim/Views/Onboarding/PermissionsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ struct PermissionsView: View {
var body: some View {
VStack {
Spacer()
Text("Permissions")
.font(.largeTitle)
.padding(.bottom, 5)
Text("MiniSim needs access to system APIs that require your permission.")
.multilineTextAlignment(.center)
OnboardingHeader(
title: "Permissions",
subTitle: "MiniSim needs access to system APIs that require your permission."
)

Spacer()
VStack (alignment: .leading) {
Label("Accessibility", systemImage: "figure.roll")
Expand Down
Loading