mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-15 01:33:25 +01:00
Reorganize AltStore project into UIKit and SwiftUI folders
This commit is contained in:
129
AltStore/SwiftUI/Views/News/NewsItemView.swift
Normal file
129
AltStore/SwiftUI/Views/News/NewsItemView.swift
Normal file
@@ -0,0 +1,129 @@
|
||||
//
|
||||
// NewsItemView.swift
|
||||
// SideStoreUI
|
||||
//
|
||||
// Created by Fabian Thies on 18.11.22.
|
||||
// Copyright © 2022 Fabian Thies. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AsyncImage
|
||||
import AltStoreCore
|
||||
|
||||
struct NewsItemView: View {
|
||||
typealias TapHandler<T> = (T) -> Void
|
||||
|
||||
let newsItem: NewsItem
|
||||
|
||||
private var newsSelectionHandler: TapHandler<NewsItem>? = nil
|
||||
private var appSelectionHandler: TapHandler<StoreApp>? = nil
|
||||
|
||||
init(newsItem: NewsItem) {
|
||||
self.newsItem = newsItem
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 12) {
|
||||
newsContent
|
||||
.onTapGesture {
|
||||
newsSelectionHandler?(newsItem)
|
||||
}
|
||||
|
||||
if let connectedApp = newsItem.storeApp {
|
||||
NavigationLink {
|
||||
AppDetailView(storeApp: connectedApp)
|
||||
} label: {
|
||||
AppRowView(app: connectedApp)
|
||||
}
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var newsContent: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
VStack(alignment: .leading) {
|
||||
Text(newsItem.title)
|
||||
.font(.title2)
|
||||
.bold()
|
||||
.foregroundColor(.white)
|
||||
|
||||
HStack(spacing: 0) {
|
||||
if let sourceName = newsItem.source?.name {
|
||||
Text(sourceName)
|
||||
.italic()
|
||||
}
|
||||
|
||||
if let externalURL = newsItem.externalURL {
|
||||
Text(" • ")
|
||||
|
||||
HStack(spacing: 0) {
|
||||
Image(systemSymbol: .link)
|
||||
Text(externalURL.host ?? "")
|
||||
.italic()
|
||||
}
|
||||
}
|
||||
}
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(.white.opacity(0.7))
|
||||
}
|
||||
|
||||
Text(newsItem.caption)
|
||||
.foregroundColor(.white.opacity(0.7))
|
||||
}
|
||||
.padding(24)
|
||||
|
||||
if let imageUrl = newsItem.imageURL {
|
||||
AsyncImage(url: imageUrl) { image in
|
||||
image
|
||||
.resizable()
|
||||
.aspectRatio(contentMode: .fit)
|
||||
} placeholder: {
|
||||
Color.secondary
|
||||
.frame(maxWidth: .infinity, maxHeight: 100)
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(
|
||||
maxWidth: .infinity,
|
||||
alignment: .topLeading
|
||||
)
|
||||
.background(Color(newsItem.tintColor))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 24, style: .circular))
|
||||
}
|
||||
|
||||
|
||||
func onNewsSelection(_ handler: @escaping TapHandler<NewsItem>) -> Self {
|
||||
var newSelf = self
|
||||
newSelf.newsSelectionHandler = handler
|
||||
return newSelf
|
||||
}
|
||||
|
||||
func onAppSelection(_ handler: @escaping TapHandler<StoreApp>) -> Self {
|
||||
var newSelf = self
|
||||
newSelf.appSelectionHandler = handler
|
||||
return newSelf
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: Identifiable {
|
||||
public var id: String {
|
||||
return self.absoluteString
|
||||
}
|
||||
}
|
||||
|
||||
//struct NewsItemView_Previews: PreviewProvider {
|
||||
// static var previews: some View {
|
||||
// NewsItemView()
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
extension NewsItemView: Equatable {
|
||||
/// Prevent re-rendering of the view if the parameters didn't change
|
||||
static func == (lhs: NewsItemView, rhs: NewsItemView) -> Bool {
|
||||
lhs.newsItem.identifier == rhs.newsItem.identifier
|
||||
}
|
||||
}
|
||||
97
AltStore/SwiftUI/Views/News/NewsView.swift
Normal file
97
AltStore/SwiftUI/Views/News/NewsView.swift
Normal file
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// NewsView.swift
|
||||
// SideStoreUI
|
||||
//
|
||||
// Created by Fabian Thies on 18.11.22.
|
||||
// Copyright © 2022 Fabian Thies. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AltStoreCore
|
||||
|
||||
struct NewsView: View {
|
||||
|
||||
@SwiftUI.FetchRequest(sortDescriptors: [
|
||||
NSSortDescriptor(keyPath: \NewsItem.date, ascending: false),
|
||||
NSSortDescriptor(keyPath: \NewsItem.sortIndex, ascending: true),
|
||||
NSSortDescriptor(keyPath: \NewsItem.sourceIdentifier, ascending: true)
|
||||
])
|
||||
var news: FetchedResults<NewsItem>
|
||||
|
||||
@State
|
||||
var activeExternalUrl: URL?
|
||||
|
||||
@State
|
||||
var selectedStoreApp: StoreApp?
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
self.announcementsCarousel
|
||||
|
||||
VStack(alignment: .leading) {
|
||||
Text(L10n.NewsView.Section.FromSources.title)
|
||||
.font(.title2)
|
||||
.bold()
|
||||
|
||||
LazyVStack(spacing: 24) {
|
||||
ForEach(news, id: \.objectID) { newsItem in
|
||||
NewsItemView(newsItem: newsItem)
|
||||
.onNewsSelection { newsItem in
|
||||
self.activeExternalUrl = newsItem.externalURL
|
||||
}
|
||||
.frame(
|
||||
maxWidth: .infinity,
|
||||
alignment: .topLeading
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
.background(Color(UIColor.systemGroupedBackground).ignoresSafeArea())
|
||||
.navigationTitle(L10n.NewsView.title)
|
||||
.sheet(item: self.$activeExternalUrl) { url in
|
||||
SafariView(url: url)
|
||||
.ignoresSafeArea()
|
||||
}
|
||||
.onAppear(perform: fetchNews)
|
||||
}
|
||||
|
||||
var announcementsCarousel: some View {
|
||||
TabView {
|
||||
ForEach(0..<5) { _ in
|
||||
RoundedRectangle(cornerRadius: 10)
|
||||
.foregroundColor(.secondary)
|
||||
.shadow(radius: 5, y: 3)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
.tabViewStyle(PageTabViewStyle())
|
||||
.frame(maxWidth: .infinity)
|
||||
.aspectRatio(16/9, contentMode: .fit)
|
||||
}
|
||||
|
||||
|
||||
func fetchNews() {
|
||||
AppManager.shared.fetchSources { result in
|
||||
do {
|
||||
do {
|
||||
let (_, context) = try result.get()
|
||||
try context.save()
|
||||
} catch let error as AppManager.FetchSourcesError {
|
||||
try error.managedObjectContext?.save()
|
||||
throw error
|
||||
}
|
||||
} catch {
|
||||
print(error)
|
||||
NotificationManager.shared.reportError(error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NewsView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
NewsView()
|
||||
}
|
||||
}
|
||||
22
AltStore/SwiftUI/Views/News/NewsViewModel.swift
Normal file
22
AltStore/SwiftUI/Views/News/NewsViewModel.swift
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// NewsViewModel.swift
|
||||
// SideStoreUI
|
||||
//
|
||||
// Created by Fabian Thies on 18.11.22.
|
||||
// Copyright © 2022 Fabian Thies. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AltStoreCore
|
||||
|
||||
class NewsViewModel: ViewModel {
|
||||
|
||||
@SwiftUI.FetchRequest(sortDescriptors: [
|
||||
NSSortDescriptor(keyPath: \NewsItem.date, ascending: false),
|
||||
NSSortDescriptor(keyPath: \NewsItem.sortIndex, ascending: true),
|
||||
NSSortDescriptor(keyPath: \NewsItem.sourceIdentifier, ascending: true)
|
||||
])
|
||||
var news: FetchedResults<NewsItem>
|
||||
|
||||
init() {}
|
||||
}
|
||||
Reference in New Issue
Block a user