-
Notifications
You must be signed in to change notification settings - Fork 85
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
Custom Public Chats #57
Changes from 5 commits
3c7369f
683a5c1
d8d3328
28d5e9c
315ba3e
86550c8
ddc9ead
78ce3e2
0f5ac64
1d8bb5f
294d44c
7b0e2d0
48883bf
131c27d
b4af9d1
8e5e113
0dbc309
b1b378e
bb9f59e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,6 @@ @interface AppDelegate () <UNUserNotificationCenterDelegate> | |
// Loki | ||
@property (nonatomic) LKP2PServer *lokiP2PServer; | ||
@property (nonatomic) LKLongPoller *lokiLongPoller; | ||
@property (nonatomic) LKGroupChatPoller *lokiPublicChatPoller; | ||
@property (nonatomic) LKRSSFeedPoller *lokiNewsFeedPoller; | ||
@property (nonatomic) LKRSSFeedPoller *lokiMessengerUpdatesFeedPoller; | ||
|
||
|
@@ -1525,11 +1524,6 @@ - (void)stopLongPollerIfNeeded | |
[self.lokiLongPoller stopIfNeeded]; | ||
} | ||
|
||
- (LKGroupChat *)lokiPublicChat | ||
{ | ||
return [[LKGroupChat alloc] initWithServerID:LKGroupChatAPI.publicChatServerID server:LKGroupChatAPI.publicChatServer displayName:NSLocalizedString(@"Loki Public Chat", @"") isDeletable:true]; | ||
} | ||
|
||
- (LKRSSFeed *)lokiNewsFeed | ||
{ | ||
return [[LKRSSFeed alloc] initWithId:@"loki.network.feed" server:@"https://loki.network/feed/" displayName:NSLocalizedString(@"Loki News", @"") isDeletable:true]; | ||
|
@@ -1542,25 +1536,18 @@ - (LKRSSFeed *)lokiMessengerUpdatesFeed | |
|
||
- (void)createGroupChatsIfNeeded | ||
{ | ||
LKGroupChat *publicChat = self.lokiPublicChat; | ||
NSString *userHexEncodedPublicKey = OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; | ||
NSString *userDefaultsKey = [@"isGroupChatSetUp." stringByAppendingString:publicChat.id]; | ||
BOOL isChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey]; | ||
if (!isChatSetUp || !publicChat.isDeletable) { | ||
TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:publicChat.displayName memberIds:@[ userHexEncodedPublicKey, publicChat.server ] image:nil groupId:[publicChat.id dataUsingEncoding:NSUTF8StringEncoding]]; | ||
__block TSGroupThread *thread; | ||
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { | ||
thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction]; | ||
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; | ||
NSCalendar *calendar = NSCalendar.currentCalendar; | ||
[calendar setTimeZone:timeZone]; | ||
NSDateComponents *dateComponents = [NSDateComponents new]; | ||
[dateComponents setYear:999]; | ||
NSDate *date = [calendar dateByAddingComponents:dateComponents toDate:[NSDate new] options:0]; | ||
[thread updateWithMutedUntilDate:date transaction:transaction]; | ||
}]; | ||
[OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread]; | ||
[NSUserDefaults.standardUserDefaults setBool:YES forKey:userDefaultsKey]; | ||
// Setup our default public chats | ||
for (LKGroupChat *chat in LKGroupChat.defaultChats) { | ||
NSString *userDefaultsKey = [@"isGroupChatSetUp." stringByAppendingString:chat.id]; | ||
BOOL isChatSetUp = [NSUserDefaults.standardUserDefaults boolForKey:userDefaultsKey]; | ||
if (!isChatSetUp || !chat.isDeletable) { | ||
[LKPublicChatManager.shared addChatWithServer:chat.server channel:chat.channel name:chat.displayName]; | ||
[OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { | ||
TSGroupThread *thread = [TSGroupThread threadWithGroupId:chat.idAsData transaction:transaction]; | ||
if (thread != nil) { [OWSProfileManager.sharedManager addThreadToProfileWhitelist:thread]; } | ||
}]; | ||
[NSUserDefaults.standardUserDefaults setBool:YES forKey:userDefaultsKey]; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -1590,18 +1577,6 @@ - (void)createRSSFeedsIfNeeded | |
} | ||
} | ||
|
||
- (void)createGroupChatPollersIfNeeded | ||
{ | ||
// Only create the group chat pollers if their threads aren't deleted | ||
__block TSGroupThread *thread; | ||
[OWSPrimaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { | ||
thread = [TSGroupThread threadWithGroupId:[self.lokiPublicChat.id dataUsingEncoding:NSUTF8StringEncoding] transaction:transaction]; | ||
}]; | ||
if (thread != nil && self.lokiPublicChatPoller == nil) { | ||
self.lokiPublicChatPoller = [[LKGroupChatPoller alloc] initForGroup:self.lokiPublicChat]; | ||
} | ||
} | ||
|
||
- (void)createRSSFeedPollersIfNeeded | ||
{ | ||
// Only create the RSS feed pollers if their threads aren't deleted | ||
|
@@ -1620,8 +1595,7 @@ - (void)createRSSFeedPollersIfNeeded | |
|
||
- (void)startGroupChatPollersIfNeeded | ||
{ | ||
[self createGroupChatPollersIfNeeded]; | ||
if (self.lokiPublicChatPoller != nil) { [self.lokiPublicChatPoller startIfNeeded]; } | ||
[LKPublicChatManager.shared startPollersIfNeeded]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes the function redundant so let's remove it then |
||
} | ||
|
||
- (void)startRSSFeedPollersIfNeeded | ||
|
@@ -1635,10 +1609,6 @@ - (void)handleThreadDeleted:(NSNotification *)notification { | |
NSDictionary *userInfo = notification.userInfo; | ||
NSString *threadID = (NSString *)userInfo[@"threadId"]; | ||
if (threadID == nil) { return; } | ||
if ([threadID isEqualToString:[TSGroupThread threadIdFromGroupId:[self.lokiPublicChat.id dataUsingEncoding:NSUTF8StringEncoding]]] && self.lokiPublicChatPoller != nil) { | ||
[self.lokiPublicChatPoller stop]; | ||
self.lokiPublicChatPoller = nil; | ||
} | ||
if ([threadID isEqualToString:[TSGroupThread threadIdFromGroupId:[self.lokiNewsFeed.id dataUsingEncoding:NSUTF8StringEncoding]]] && self.lokiNewsFeedPoller != nil) { | ||
[self.lokiNewsFeedPoller stop]; | ||
self.lokiNewsFeedPoller = nil; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
@objc(LKNewPublicChatVC) | ||
final class NewPublicChatVC : OWSViewController { | ||
|
||
// MARK: Components | ||
private lazy var serverUrlTextField: UITextField = { | ||
let result = UITextField() | ||
result.textColor = Theme.primaryColor | ||
result.font = UIFont.ows_dynamicTypeBodyClamped | ||
let placeholder = NSMutableAttributedString(string: NSLocalizedString("Enter a Server URL", comment: "")) | ||
placeholder.addAttribute(.foregroundColor, value: Theme.placeholderColor, range: NSRange(location: 0, length: placeholder.length)) | ||
result.attributedPlaceholder = placeholder | ||
result.tintColor = UIColor.lokiGreen() | ||
result.keyboardAppearance = .dark | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would've been nice to also set |
||
return result | ||
}() | ||
|
||
private lazy var addButton: OWSFlatButton = { | ||
let addButtonFont = UIFont.ows_dynamicTypeBodyClamped.ows_mediumWeight() | ||
let addButtonHeight = addButtonFont.pointSize * 48 / 17 | ||
let addButton = OWSFlatButton.button(title: NSLocalizedString("Add", comment: ""), font: addButtonFont, titleColor: .white, backgroundColor: .lokiGreen(), target: self, selector: #selector(handleNextButtonTapped)) | ||
addButton.autoSetDimension(.height, toSize: addButtonHeight) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convention is to not set up constraints here. The reason is that not all constraints can be set up at this point (the view needs to be added as a subview before some of them can be created), so the idea is to wait for that and then set up everything. |
||
return addButton | ||
}() | ||
|
||
// MARK: Lifecycle | ||
override func viewDidLoad() { | ||
// Background color & margins | ||
view.backgroundColor = Theme.backgroundColor | ||
view.layoutMargins = .zero | ||
// Navigation bar | ||
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(close)) | ||
title = NSLocalizedString("Add Public Chat Server", comment: "") | ||
// Separator | ||
let separator = UIView() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This separator is never added as a subview |
||
separator.autoSetDimension(.height, toSize: 1 / UIScreen.main.scale) | ||
separator.backgroundColor = Theme.hairlineColor | ||
|
||
updateButton(enabled: true) | ||
|
||
// Stack view | ||
let stackView = UIStackView(arrangedSubviews: [ | ||
serverUrlTextField, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An explainer label would be nice |
||
UIView.vStretchingSpacer(), | ||
addButton | ||
]) | ||
stackView.axis = .vertical | ||
stackView.alignment = .fill | ||
stackView.layoutMargins = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16) | ||
stackView.isLayoutMarginsRelativeArrangement = true | ||
view.addSubview(stackView) | ||
stackView.autoPinWidthToSuperview() | ||
stackView.autoPin(toTopLayoutGuideOf: self, withInset: 0) | ||
autoPinView(toBottomOfViewControllerOrKeyboard: stackView, avoidNotch: true) | ||
} | ||
|
||
override func viewDidAppear(_ animated: Bool) { | ||
super.viewDidAppear(animated) | ||
serverUrlTextField.becomeFirstResponder() | ||
} | ||
|
||
// MARK: Interaction | ||
@objc private func close() { | ||
dismiss(animated: true, completion: nil) | ||
} | ||
|
||
@objc private func handleNextButtonTapped() { | ||
let serverURL = (serverUrlTextField.text?.trimmingCharacters(in: .whitespaces) ?? "").lowercased().replacingOccurrences(of: "http://", with: "https://") | ||
guard let url = URL(string: serverURL), let scheme = url.scheme, scheme == "https", let _ = url.host else { | ||
showAlert(title: NSLocalizedString("Invalid server URL provided", comment: ""), message: NSLocalizedString("Please make sure you have provided the full url", comment: "")) | ||
return | ||
} | ||
|
||
updateButton(enabled: false) | ||
|
||
// TODO: Upon adding we should fetch previous messages | ||
LokiPublicChatManager.shared.addChat(server: serverURL, channel: 1) | ||
.done(on: .main) { _ in | ||
self.presentingViewController!.dismiss(animated: true, completion: nil) | ||
nielsandriesse marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're capturing |
||
} | ||
.catch(on: .main) { e in | ||
self.updateButton(enabled: true) | ||
self.showAlert(title: NSLocalizedString("Failed to connect to server", comment: "")) | ||
} | ||
} | ||
|
||
private func showAlert(title: String, message: String = "") { | ||
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | ||
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)) | ||
presentAlert(alert) | ||
} | ||
|
||
private func updateButton(enabled: Bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be under the interaction mark |
||
addButton.setEnabled(enabled) | ||
addButton.setTitle(enabled ? NSLocalizedString("Add", comment: "") : NSLocalizedString("Connecting to server", comment: "")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need to be localized |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to update the display name on the server