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

Vertical tab switcher + folders settings developer info #144

Merged
merged 10 commits into from
Sep 12, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ public class TdFoldersPrefService: FoldersPrefService {
tdApi.client.updateSubject
}

public init() {}
public init() { }

public func getFilters() async throws -> [ChatFilterInfo] {
try! StorageService.shared.getRecords(as: Storage.ChatFolder.self, ordered: [Column("order").asc])
.map { cached in
ChatFilterInfo(from: cached)
}
.map(ChatFilterInfo.init(from:))
}

public func getFilter(by id: Int) async throws -> TDLibKit.ChatFilter {
Expand Down
4 changes: 4 additions & 0 deletions Moc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
43C227C728C73E9900AE237C /* FormattedTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C227C628C73E9900AE237C /* FormattedTextView.swift */; };
43C227CB28CA344E00AE237C /* ConnectionState+Title.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C227CA28CA344E00AE237C /* ConnectionState+Title.swift */; };
43C227CD28CA4F5800AE237C /* View+IntrospectNSSplitView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C227CC28CA4F5800AE237C /* View+IntrospectNSSplitView.swift */; platformFilters = (macos, ); };
43C227D128CB097A00AE237C /* View+Faded.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C227D028CB097A00AE237C /* View+Faded.swift */; };
43C29C04287D8D9E00D10AB8 /* Animation+FastStartSlowStop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43C29C03287D8D9E00D10AB8 /* Animation+FastStartSlowStop.swift */; };
43EC18E22856782F00FCAD43 /* SearchField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43EC18E12856782F00FCAD43 /* SearchField.swift */; platformFilter = ios; };
43F610FB28AFB8AE0098C3BD /* VisualEffectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43F610FA28AFB8AE0098C3BD /* VisualEffectView.swift */; };
Expand Down Expand Up @@ -289,6 +290,7 @@
43C227C628C73E9900AE237C /* FormattedTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FormattedTextView.swift; sourceTree = "<group>"; };
43C227CA28CA344E00AE237C /* ConnectionState+Title.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ConnectionState+Title.swift"; sourceTree = "<group>"; };
43C227CC28CA4F5800AE237C /* View+IntrospectNSSplitView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+IntrospectNSSplitView.swift"; sourceTree = "<group>"; };
43C227D028CB097A00AE237C /* View+Faded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+Faded.swift"; sourceTree = "<group>"; };
43C29C03287D8D9E00D10AB8 /* Animation+FastStartSlowStop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Animation+FastStartSlowStop.swift"; sourceTree = "<group>"; };
43CA7FC9283E3988006E7AC9 /* Utilities */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = Utilities; sourceTree = "<group>"; };
43EC18D92856207C00FCAD43 /* libSystem.B.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libSystem.B.tbd; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.0.sdk/usr/lib/libSystem.B.tbd; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -400,6 +402,7 @@
435C9C0B28A13FB600E5D682 /* CompactChatItemView.swift */,
43051C8B287791D0003C5CE2 /* CornerRectangle.swift */,
438B2284285E558F000D65C8 /* ProfilePlaceholderView.swift */,
43C227D028CB097A00AE237C /* View+Faded.swift */,
43C227C628C73E9900AE237C /* FormattedTextView.swift */,
);
path = Common;
Expand Down Expand Up @@ -936,6 +939,7 @@
43807D8E27E213350056A3D3 /* DevicesPrefView.swift in Sources */,
43B1029B28831A8D009FCF53 /* Image+Data.swift in Sources */,
439BA4282863C3CC00339375 /* LoginView+Welcome.swift in Sources */,
43C227D128CB097A00AE237C /* View+Faded.swift in Sources */,
43051C9228779363003C5CE2 /* ChatView+Inspector.swift in Sources */,
438B227D285DCA88000D65C8 /* AsyncTdImage.swift in Sources */,
439E0E27287EE06900013A0F /* AboutCommand.swift in Sources */,
Expand Down
16 changes: 10 additions & 6 deletions Shared/Commands/AppCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct AppCommands: Commands {
Button {
Task {
let me = try await TdApi.shared.getMe()
print(type(of: me.id))
SystemUtils.post(notification: .openChatWithId, with: me.id)
}
} label: {
Expand All @@ -60,18 +61,21 @@ struct AppCommands: Commands {
}
}
Divider()
Button(action: {
Button {
Analytics.trackEvent("Opened \"Telegram Tips\" channel from the menubar")
}, label: {
print(type(of: -1001224624669))
SystemUtils.post(notification: .openChatWithId, with: -1001224624669)
} label: {
Image(systemName: "text.book.closed")
Text("Telegram Tips")
})
Button(action: {
}
Button {
Analytics.trackEvent("Opened \"Moc Updates\" channel from the menubar")
}, label: {
SystemUtils.post(notification: .openChatWithId, with: -1001734933131)
} label: {
Image(systemName: "newspaper")
Text("Moc Updates")
})
}
Divider()
}
}
Expand Down
2 changes: 2 additions & 0 deletions Shared/MocApp+Injection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ struct MocApp: App {
Analytics.self,
Crashes.self
])
#if !DEBUG
Analytics.enabled = true
#endif
}

#if os(macOS)
Expand Down
13 changes: 7 additions & 6 deletions Shared/View Models/Chat/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ChatViewModel: ObservableObject {
@Published var chatPhoto: File?
@Published var isChannel = false

var subscribers: [AnyCancellable] = []
var subscribers = Set<AnyCancellable>()
var logger = Logs.Logger(category: "UI", label: "ChatViewModel")
var inputMessageSubject = CurrentValueSubject<String, Never>("")

Expand All @@ -60,7 +60,9 @@ class ChatViewModel: ObservableObject {
.store(in: &subscribers)
SystemUtils.ncPublisher(for: .openChatWithId)
.sink { notification in
self.logger.debug("Got openChatWithId notification")
guard let chatId = notification.object as? Int64 else { return }
self.logger.debug("Got chat ID from notification")

Task {
try await self.update(chat: try await TdApi.shared.getChat(chatId: chatId))
Expand All @@ -69,8 +71,10 @@ class ChatViewModel: ObservableObject {
.store(in: &subscribers)
SystemUtils.ncPublisher(for: .openChatWithInstance)
.sink { notification in
self.logger.debug("Got openChatWithInstance notification")
guard let chat = notification.object as? Chat else { return }

self.logger.debug("Got Chat objected")

Task {
try await self.update(chat: chat)
}
Expand Down Expand Up @@ -178,13 +182,10 @@ class ChatViewModel: ObservableObject {
return $0.mediaAlbumID == $1.mediaAlbumID
}
})
.map {
Array($0)
}
.map(Array.init(_:))

logger.debug("Chunked message history, length: \(messageHistory.count)")


DispatchQueue.main.async {
self.chatPhoto = chat.photo?.small
switch chat.type {
Expand Down
42 changes: 23 additions & 19 deletions Shared/View Models/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class MainViewModel: ObservableObject {
return chats(from: .archive)
case .folder(let id):
return chats(from: .filter(.init(chatFilterId: id)))
case .none:
return []
}
}

Expand All @@ -82,26 +84,28 @@ class MainViewModel: ObservableObject {
@Published var allChats: OrderedSet<Chat> = []
@Published var chatPositions: [Int64: [ChatPosition]] = [:]

@Published var openChatList: Storage.ChatList = .main {
@Published var openChatList: Storage.ChatList? = .main {
didSet {
logger.trace("openChatList: \(openChatList)")
if openChatList != .archive {
openChatListBuffer = openChatList
}
Task {
switch openChatList {
case .main:
_ = try await TdApi.shared.loadChats(
chatList: .main,
limit: 30)
case .archive:
_ = try await TdApi.shared.loadChats(
chatList: .archive,
limit: 30)
case .folder(let id):
_ = try await TdApi.shared.loadChats(
chatList: .filter(.init(chatFilterId: id)),
limit: 30)
if let openChatList {
logger.trace("openChatList: \(openChatList)")
if openChatList != .archive {
openChatListBuffer = openChatList
}
Task {
switch openChatList {
case .main:
_ = try await TdApi.shared.loadChats(
chatList: .main,
limit: 30)
case .archive:
_ = try await TdApi.shared.loadChats(
chatList: .archive,
limit: 30)
case .folder(let id):
_ = try await TdApi.shared.loadChats(
chatList: .filter(.init(chatFilterId: id)),
limit: 30)
}
}
}
}
Expand Down
30 changes: 20 additions & 10 deletions Shared/Views/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ struct ChatView: View {
@StateObject var viewModel = ChatViewModel()
@FocusState var isInputFieldFocused
@Default(.showDeveloperInfo) var showDeveloperInfo
let tempChat: Chat

let logger = Logger(category: "UI", label: "ChatView")

init(_ chat: Chat) {
self.tempChat = chat
}

var chatView: some View {
ZStack {
ScrollViewReader { proxy in
Expand Down Expand Up @@ -161,16 +166,21 @@ struct ChatView: View {
.toolbar {
toolbar
}
.onAppear {
Task {
try await viewModel.update(chat: tempChat)
}
}
}
}

struct ChatView_Previews: PreviewProvider {
init() {
Resolver.register { MockChatService() as (any ChatService) }
}

static var previews: some View {
ChatView()
.frame(width: 800, height: 600)
}
}
//struct ChatView_Previews: PreviewProvider {
// init() {
// Resolver.register { MockChatService() as (any ChatService) }
// }
//
// static var previews: some View {
// ChatView()
// .frame(width: 800, height: 600)
// }
//}
4 changes: 2 additions & 2 deletions Shared/Views/Chat/Message/MessageView+Photo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension MessageView {
// swiftlint:disable function_body_length
func makePhoto(from info: MessagePhoto, contentMode: ContentMode = .fit) -> some View {
ZStack {
if let size = info.photo.sizes.getSize(.sBox) {
if let size = info.photo.sizes.getSize(.yBox) {
AsyncTdImage(
id: size.photo.id
) { image in
Expand All @@ -33,7 +33,7 @@ extension MessageView {
}
.frame(minWidth: 0, maxWidth: 350, minHeight: 0, maxHeight: 200)
.background {
if let size = info.photo.sizes.getSize(.sBox) {
if let size = info.photo.sizes.getSize(.yBox) {
AsyncTdImage(
id: size.photo.id
) { image in
Expand Down
30 changes: 19 additions & 11 deletions Shared/Views/Common/FolderItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private extension SidebarSize {
}

struct FolderItem<Icon: View>: View {
let name: String
let name: String?
let icon: Icon
let unreadCount: Int
let horizontal: Bool
Expand All @@ -66,7 +66,7 @@ struct FolderItem<Icon: View>: View {
private let selectedColor = Color("FolderItemSelectedColor")

init(
name: String,
name: String? = nil,
icon: @autoclosure () -> Icon,
unreadCount: Int = 0,
horizontal: Bool = false
Expand All @@ -87,14 +87,18 @@ struct FolderItem<Icon: View>: View {
private var content: some View {
if horizontal {
HStack {
Label {
Text(name)
.lineLimit(1)
.fixedSize()
} icon: {
icon
if let name {
Label {
Text(name)
.lineLimit(1)
.fixedSize()
} icon: {
icon
}
.font(sidebarSize.textFont)
} else {
icon.font(sidebarSize.textFont)
}
.font(sidebarSize.textFont)

if unreadCount != 0 {
counter
Expand All @@ -119,7 +123,9 @@ struct FolderItem<Icon: View>: View {
} else {
VStack {
icon.font(sidebarSize.iconFont)
Text(name).font(sidebarSize.textFont)
if let name {
Text(name).font(sidebarSize.textFont)
}
}
.padding(.vertical, 8)
.onHover { isHovered in
Expand All @@ -129,7 +135,9 @@ struct FolderItem<Icon: View>: View {
backgroundColor = Color.clear
}
}
.frame(width: sidebarSize.itemWidth, height: sidebarSize.itemHeight)
.frame(
width: sidebarSize.itemWidth,
height: name == nil ? sidebarSize.itemHeight - 15 : sidebarSize.itemHeight)
}
}

Expand Down
32 changes: 32 additions & 0 deletions Shared/Views/Common/View+Faded.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// View+Faded.swift
// Moc
//
// Created by Егор Яковенко on 09.09.2022.
//

import SwiftUI

extension View {
func faded(top: Bool, bottom: Bool) -> some View {
self.mask {
VStack(spacing: 0) {
if top {
Rectangle()
.fill(.linearGradient(colors: [.white.opacity(0), .white], startPoint: .top, endPoint: .bottom))
.frame(height: 30)
.transition(.move(edge: .top))
}
Rectangle()
if bottom {
Rectangle()
.fill(.linearGradient(colors: [.white.opacity(0), .white], startPoint: .bottom, endPoint: .top))
.frame(height: 30)
.transition(.move(edge: .bottom))
}
}
.animation(.fastStartSlowStop(0.4), value: top)
.animation(.fastStartSlowStop(0.4), value: bottom)
}
}
}
Loading