Skip to content

Commit

Permalink
Phone prefix dropdown fix (#134)
Browse files Browse the repository at this point in the history
* fix: country code dropdown

* feat: started implementing support for countries with multiple calling codes available for them

* feat: implement "saved messages" shortcut

* feat: added support for multiple phone numbers in one country
  • Loading branch information
ggoraa committed Aug 25, 2022
1 parent 9177372 commit 3ac8aa9
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Moc.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
43431C8528A00BAB002A72A0 /* TDLibKit in Frameworks */ = {isa = PBXBuildFile; productRef = 43431C8428A00BAB002A72A0 /* TDLibKit */; };
43431C8928A00D43002A72A0 /* TDLibKit in Embed Frameworks */ = {isa = PBXBuildFile; productRef = 43431C8428A00BAB002A72A0 /* TDLibKit */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
43431C8B28A013BC002A72A0 /* PinCodeTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43431C8A28A013BC002A72A0 /* PinCodeTextField.swift */; platformFilters = (macos, ); };
434889EE28B798B200EE4455 /* PhoneNumberInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434889ED28B798B200EE4455 /* PhoneNumberInfo.swift */; };
434A07172869003200A2BBA5 /* AVPlayerViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434A07162869003200A2BBA5 /* AVPlayerViewWrapper.swift */; platformFilters = (macos, ); };
434A60802848AA000086B866 /* ChatPickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434A607F2848AA000086B866 /* ChatPickerView.swift */; };
435C9C0C28A13FB700E5D682 /* CompactChatItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 435C9C0B28A13FB600E5D682 /* CompactChatItemView.swift */; };
Expand Down Expand Up @@ -188,6 +189,7 @@
433EDD962860E9C0009D03D5 /* QuickLookView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickLookView.swift; sourceTree = "<group>"; };
433EDD992860EBAA009D03D5 /* AsyncTdQuickLookView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AsyncTdQuickLookView.swift; sourceTree = "<group>"; };
43431C8A28A013BC002A72A0 /* PinCodeTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinCodeTextField.swift; sourceTree = "<group>"; };
434889ED28B798B200EE4455 /* PhoneNumberInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PhoneNumberInfo.swift; sourceTree = "<group>"; };
434A07162869003200A2BBA5 /* AVPlayerViewWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AVPlayerViewWrapper.swift; sourceTree = "<group>"; };
434A607F2848AA000086B866 /* ChatPickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatPickerView.swift; sourceTree = "<group>"; };
435C9C0B28A13FB600E5D682 /* CompactChatItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompactChatItemView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -434,7 +436,7 @@
43C0F49E285A6997009F2419 /* Settings */,
43008542283B59C200ED5961 /* Utils */,
43807D4A27E213350056A3D3 /* Models */,
43807D7827E213350056A3D3 /* ViewModels */,
43807D7827E213350056A3D3 /* View Models */,
43807D5527E213350056A3D3 /* Views */,
43F610FC28B01E6A0098C3BD /* Shapes */,
43807D6C27E213350056A3D3 /* Commands */,
Expand Down Expand Up @@ -468,6 +470,7 @@
43ABC05C28500648009F529E /* ChatFolder.swift */,
4371AC162873126A0081776F /* ReplyMessage.swift */,
438CFBAA287F6ECE005B9990 /* SidebarSize.swift */,
434889ED28B798B200EE4455 /* PhoneNumberInfo.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -552,15 +555,15 @@
path = SwiftGen;
sourceTree = "<group>";
};
43807D7827E213350056A3D3 /* ViewModels */ = {
43807D7827E213350056A3D3 /* View Models */ = {
isa = PBXGroup;
children = (
43807D7927E213350056A3D3 /* Settings */,
43051C9528779924003C5CE2 /* Chat */,
43807D7C27E213350056A3D3 /* MainViewModel.swift */,
2D229DCE28759E000056A294 /* EmojiViewModel.swift */,
);
path = ViewModels;
path = "View Models";
sourceTree = "<group>";
};
43807D7927E213350056A3D3 /* Settings */ = {
Expand Down Expand Up @@ -884,6 +887,7 @@
43051C8C287791D0003C5CE2 /* RoundedCorners.swift in Sources */,
43C0F4A0285A69AA009F2419 /* FolderLayout.swift in Sources */,
434A60802848AA000086B866 /* ChatPickerView.swift in Sources */,
434889EE28B798B200EE4455 /* PhoneNumberInfo.swift in Sources */,
43A0AF872865197400C0A8EF /* AsyncTdFile.swift in Sources */,
436707232861A8CA00587A63 /* View+QuickLook.swift in Sources */,
43807D9927E213350056A3D3 /* AppCommands.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion Shared/Commands/AppCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI
import AppCenterAnalytics
import Defaults
import Utilities
import TDLibKit

struct AppCommands: Commands {
#if os(macOS)
Expand All @@ -31,7 +32,10 @@ struct AppCommands: Commands {
#endif
CommandGroup(after: .appSettings) {
Button {
// TODO: implement opening saved messages chat
Task {
let me = try await TdApi.shared[0].getMe()
SystemUtils.post(notification: .openChatWithId, with: me.id)
}
} label: {
Image(systemName: "bookmark")
Text("Saved messages")
Expand Down
12 changes: 12 additions & 0 deletions Shared/Models/PhoneNumberInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// PhoneNumber.swift
// Moc
//
// Created by Егор Яковенко on 25.08.2022.
//

struct PhoneNumberInfo: Hashable {
/// A two-letter ISO 3166-1 alpha-2 country code
let country: String
let phoneNumberPrefix: String
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 11 additions & 7 deletions Shared/Views/Login/LoginView+PhoneNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ extension LoginView {
Picker("", selection: $selectedNumberCode) {
#if DEBUG
Text("TEST (999)")
.tag(Int(999))
.tag(PhoneNumberInfo(country: "TEST", phoneNumberPrefix: "999"))
Divider()
#endif
ForEach(phoneNumberCodes, id: \.name) { info in
Text("\(info.countryCode) (+\(info.callingCodes[0]))")
.tag(Int(info.callingCodes[0])!)
ForEach(phoneNumberCodes, id: \.countryCode) { info in
if !info.isHidden {
ForEach(info.callingCodes, id: \.self) { code in
Text("\(info.countryCode) (+\(code))")
.tag(PhoneNumberInfo(country: info.countryCode, phoneNumberPrefix: code))
}
}
}
}
.frame(width: 110)
Expand All @@ -32,13 +36,13 @@ extension LoginView {
Task {
withAnimation { showLoadingSpinner = true }
do {
if selectedNumberCode == 999 {
if selectedNumberCode.phoneNumberPrefix == "999" {
try await service.checkAuth(
phoneNumber: "\(selectedNumberCode)\(phoneNumber)"
phoneNumber: "\(selectedNumberCode.phoneNumberPrefix)\(phoneNumber)"
)
} else {
try await service.checkAuth(
phoneNumber: "+\(selectedNumberCode)\(phoneNumber)"
phoneNumber: "+\(selectedNumberCode.phoneNumberPrefix)\(phoneNumber)"
)
}
} catch {
Expand Down
14 changes: 10 additions & 4 deletions Shared/Views/Login/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct LoginView: View {
@State var qrCodeLink = ""

@State var phoneNumberCodes: [CountryInfo] = []
@State var selectedNumberCode: Int = 0
@State var selectedNumberCode = PhoneNumberInfo(country: "", phoneNumberPrefix: "")

@State var openedScreen = OpenedLoginScreen.welcome

Expand Down Expand Up @@ -115,11 +115,14 @@ struct LoginView: View {
guard var countries = try? await service.countries else { return }
let countryCode = (try? await service.countryCode) ?? "EN"

var callingCode = 0
var callingCode = PhoneNumberInfo(country: "", phoneNumberPrefix: "")

for country in countries where country.countryCode == countryCode {
callingCode = Int(country.callingCodes[0])!
logger.info("Country code: \(self.selectedNumberCode)")
callingCode = PhoneNumberInfo(
country: country.countryCode,
phoneNumberPrefix: country.callingCodes[0])

logger.info("Country code: \(String(describing: self.selectedNumberCode))")
}

for (index, country) in countries.enumerated() where country.countryCode.lowercased() == "ru" {
Expand All @@ -134,6 +137,9 @@ struct LoginView: View {
at: index)
}

countries.sort(by: { $0.countryCode < $1.countryCode })
countries.removeDuplicates()

self.phoneNumberCodes = countries
self.selectedNumberCode = callingCode

Expand Down

0 comments on commit 3ac8aa9

Please sign in to comment.