[ADD] News, Browse and Settings views ported to SwiftUI

This commit contains WIP SwiftUI versions of most of the views in SideStore.
This commit is contained in:
Fabian Thies
2022-11-23 22:34:02 +01:00
parent ed2270ff46
commit 16a8bce102
30 changed files with 2047 additions and 2 deletions

View File

@@ -0,0 +1,54 @@
//
// AddSourceView.swift
// SideStore
//
// Created by Fabian Thies on 20.11.22.
// Copyright © 2022 Fabian Thies. All rights reserved.
//
import SwiftUI
struct AddSourceView: View {
@State var sourceUrlText: String = ""
var continueHandler: (_ urlText: String) -> ()
var body: some View {
List {
Section {
TextField("https://connect.altstore.ml", text: $sourceUrlText)
.keyboardType(.URL)
.autocapitalization(.none)
.autocorrectionDisabled()
} header: {
Text("Source URL")
} footer: {
VStack(alignment: .leading, spacing: 4) {
Text("Please enter the source url here. Then, tap continue to validate and add the source in the next step.")
HStack(alignment: .top) {
Image(systemName: "exclamationmark.triangle.fill")
Text("Be careful with unvalidated third-party sources! Make sure to only add sources that you trust.")
}
}
}
SwiftUI.Button {
self.continueHandler(self.sourceUrlText)
} label: {
Text("Continue")
}
.disabled(URL(string: self.sourceUrlText)?.host == nil)
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("Add Source")
}
}
struct AddSourceView_Previews: PreviewProvider {
static var previews: some View {
AddSourceView(continueHandler: { _ in })
}
}

View File

@@ -0,0 +1,48 @@
//
// BrowseAppPreviewView.swift
// SideStore
//
// Created by Fabian Thies on 20.11.22.
// Copyright © 2022 Fabian Thies. All rights reserved.
//
import SwiftUI
import AltStoreCore
struct BrowseAppPreviewView: View {
let storeApp: StoreApp
var body: some View {
VStack {
AppRowView(app: storeApp)
if let subtitle = storeApp.subtitle {
Text(subtitle)
}
if !storeApp.screenshotURLs.isEmpty {
HStack {
ForEach(storeApp.screenshotURLs.prefix(2)) { url in
if #available(iOS 15.0, *) {
AsyncImage(url: url) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
} placeholder: {
Color(UIColor.secondarySystemBackground)
}
.cornerRadius(8)
}
}
}
.frame(height: 300)
}
}
}
}
//struct BrowseAppPreviewView_Previews: PreviewProvider {
// static var previews: some View {
// BrowseAppPreviewView()
// }
//}

View File

@@ -0,0 +1,99 @@
//
// BrowseView.swift
// SideStoreUI
//
// Created by Fabian Thies on 18.11.22.
// Copyright © 2022 Fabian Thies. All rights reserved.
//
import SwiftUI
import AltStoreCore
struct BrowseView: View {
@SwiftUI.FetchRequest(sortDescriptors: [
NSSortDescriptor(keyPath: \StoreApp.sourceIdentifier, ascending: true),
NSSortDescriptor(keyPath: \StoreApp.sortIndex, ascending: true),
NSSortDescriptor(keyPath: \StoreApp.name, ascending: true),
NSSortDescriptor(keyPath: \StoreApp.bundleIdentifier, ascending: true)
]/*, predicate: NSPredicate(format: "%K != %@", #keyPath(StoreApp.bundleIdentifier), StoreApp.altstoreAppID)*/)
var apps: FetchedResults<StoreApp>
var filteredApps: [StoreApp] {
apps.filter { $0.matches(self.searchText) }
}
@State
var selectedStoreApp: StoreApp?
@State var searchText = ""
@State var isShowingSourcesView = false
var body: some View {
ScrollView {
LazyVStack(spacing: 24) {
ForEach(filteredApps, id: \.bundleIdentifier) { app in
NavigationLink {
AppDetailView(storeApp: app)
} label: {
BrowseAppPreviewView(storeApp: app)
}
.buttonStyle(PlainButtonStyle())
}
}
.padding()
.searchable(text: self.$searchText, placeholder: "Search")
}
.background(Color(UIColor.systemGroupedBackground).ignoresSafeArea())
.navigationTitle("Browse")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
SwiftUI.Button {
self.isShowingSourcesView = true
} label: {
Text("Sources")
}
.sheet(isPresented: self.$isShowingSourcesView) {
NavigationView {
SourcesView()
}
}
}
}
}
}
struct BrowseView_Previews: PreviewProvider {
static var previews: some View {
BrowseView()
}
}
extension StoreApp {
func matches(_ searchText: String) -> Bool {
searchText.isEmpty ||
self.name.lowercased().contains(searchText.lowercased()) ||
self.developerName.lowercased().contains(searchText.lowercased())
}
}
extension View {
@ViewBuilder func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}
@ViewBuilder func searchable(text: Binding<String>, placeholder: String) -> some View {
if #available(iOS 15.0, *) {
self.searchable(text: text, prompt: Text(placeholder))
} else {
self
}
}
}

View File

@@ -0,0 +1,114 @@
//
// ConfirmAddSourceView.swift
// SideStore
//
// Created by Fabian Thies on 18.11.22.
// Copyright © 2022 Fabian Thies. All rights reserved.
//
import SwiftUI
import AltStoreCore
struct ConfirmAddSourceView: View {
let fetchedSource: FetchedSource
var source: Source {
fetchedSource.source
}
var confirmationHandler: (_ source: FetchedSource) -> ()
var cancellationHandler: () -> ()
var body: some View {
VStack(alignment: .leading) {
List {
Section {
VStack(alignment: .leading) {
Text("\(source.apps.count) Apps")
Text(source.apps.map { $0.name }.joined(separator: ", "))
.font(.callout)
.lineLimit(1)
.foregroundColor(.secondary)
}
VStack() {
Text("\(source.newsItems.count) News Items")
}
} header: {
Text("Source Contents")
}
Section {
VStack(alignment: .leading) {
Text("Source Identifier")
Text(source.identifier)
.font(.callout)
.foregroundColor(.secondary)
}
VStack(alignment: .leading) {
Text("Source URL")
Text(source.sourceURL.absoluteString)
.font(.callout)
.foregroundColor(.secondary)
}
} header: {
Text("Source Info")
}
}
.listStyle(InsetGroupedListStyle())
Spacer()
SwiftUI.Button {
confirmationHandler(fetchedSource)
} label: {
Label(title: { Text("Add Source") }, icon: { Image(systemName: "plus") })
.multilineTextAlignment(.center)
.foregroundColor(.white)
.padding()
.frame(
maxWidth: .infinity,
alignment: .bottomLeading
)
.background(
RoundedRectangle(cornerRadius: 10)
.foregroundColor(Color.accentColor)
)
}
.padding()
}
.background(Color(UIColor.systemGroupedBackground).ignoresSafeArea())
.toolbar {
ToolbarItemGroup(placement: .navigationBarTrailing) {
SwiftUI.Button {
} label: {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
}
}
ToolbarItemGroup(placement: .navigation) {
VStack(alignment: .leading) {
Text(source.name)
.font(.title3)
.bold()
Text(source.identifier)
.font(.callout)
.foregroundColor(.secondary)
}
}
}
.navigationBarTitleDisplayMode(.inline)
}
}
struct ConfirmAddSourceView_Previews: PreviewProvider {
static var previews: some View {
AddSourceView(continueHandler: { _ in })
}
}

View File

@@ -0,0 +1,188 @@
//
// SourcesView.swift
// SideStore
//
// Created by Fabian Thies on 20.11.22.
// Copyright © 2022 Fabian Thies. All rights reserved.
//
import SwiftUI
import AltStoreCore
import CoreData
struct SourcesView: View {
@Environment(\.managedObjectContext)
var managedObjectContext
@SwiftUI.FetchRequest(sortDescriptors: [
NSSortDescriptor(keyPath: \Source.name, ascending: true),
NSSortDescriptor(keyPath: \Source.sourceURL, ascending: true),
NSSortDescriptor(keyPath: \Source.identifier, ascending: true)
])
var installedSources: FetchedResults<Source>
@State var isShowingAddSourceAlert = false
@State var sourceToConfirm: FetchedSource?
var body: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 24) {
// Installed Sources
LazyVStack(alignment: .leading, spacing: 12) {
Text("Sources control what apps are available to download through SideStore.")
.font(.callout)
.foregroundColor(.secondary)
ForEach(installedSources, id: \.identifier) { source in
VStack(alignment: .leading) {
Text(source.name)
.bold()
Text(source.sourceURL.absoluteString)
.font(.callout)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(Color.accentColor.opacity(0.5))
.clipShape(RoundedRectangle(cornerRadius: 30, style: .circular))
.if(source.identifier != Source.altStoreIdentifier) { view in
view.contextMenu(ContextMenu(menuItems: {
SwiftUI.Button {
self.removeSource(source)
} label: {
Label("Remove", systemImage: "trash")
}
}))
}
}
}
// Trusted Sources
LazyVStack(alignment: .leading) {
Text("Trusted Sources")
.font(.title3)
.bold()
Text("SideStore has reviewed these sources to make sure they meet our safety standards.")
.font(.callout)
.foregroundColor(.secondary)
}
}
.padding()
}
.navigationTitle("Sources")
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
SwiftUI.Button {
self.isShowingAddSourceAlert = true
} label: {
Image(systemName: "plus")
}
.sheet(isPresented: self.$isShowingAddSourceAlert) {
NavigationView {
AddSourceView(continueHandler: fetchSource(with:))
}
}
.sheet(item: self.$sourceToConfirm) { source in
if #available(iOS 16.0, *) {
NavigationView {
ConfirmAddSourceView(fetchedSource: source, confirmationHandler: addSource(_:)) {
self.sourceToConfirm = nil
}
}
.presentationDetents([.medium])
}
}
}
ToolbarItem(placement: .navigationBarTrailing) {
SwiftUI.Button {
self.isShowingAddSourceAlert = false
} label: {
Text("Done").bold()
}
}
}
}
func fetchSource(with urlText: String) {
self.isShowingAddSourceAlert = false
guard let url = URL(string: urlText) else {
return
}
AppManager.shared.fetchSource(sourceURL: url) { result in
switch result {
case let .success(source):
self.sourceToConfirm = FetchedSource(source: source)
case let .failure(error):
print(error)
}
}
}
func addSource(_ source: FetchedSource) {
source.context?.perform {
do {
try source.context?.save()
} catch {
print(error)
}
}
self.sourceToConfirm = nil
}
func removeSource(_ source: Source) {
DatabaseManager.shared.persistentContainer.performBackgroundTask { (context) in
let source = context.object(with: source.objectID) as! Source
context.delete(source)
do {
try context.save()
} catch {
print(error)
}
}
}
}
struct SourcesListView_Previews: PreviewProvider {
static var previews: some View {
SourcesView()
}
}
extension Source: Identifiable {
public var id: String {
self.identifier
}
}
struct FetchedSource: Identifiable {
let source: Source
let context: NSManagedObjectContext?
var id: String {
source.identifier
}
init?(source: Source) {
guard let context = source.managedObjectContext else{
return nil
}
self.source = source
self.context = context
}
}