CCShareUserOC.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // CCShareUserOC.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 30/11/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCShareUserOC.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @interface CCShareUserOC ()
  27. @end
  28. @implementation CCShareUserOC
  29. - (instancetype)initWithCoder:(NSCoder *)coder
  30. {
  31. self = [super initWithCoder:coder];
  32. if (self) {
  33. self.directUser = @"";
  34. [self initializeForm];
  35. }
  36. return self;
  37. }
  38. - (void)initializeForm
  39. {
  40. XLFormDescriptor *form ;
  41. XLFormSectionDescriptor *section;
  42. XLFormRowDescriptor *row;
  43. form = [XLFormDescriptor formDescriptor];
  44. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  45. form.assignFirstResponderOnShow = NO;
  46. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_find_sharee_title_", nil)];
  47. [form addFormSection:section];
  48. section.footerTitle = NSLocalizedString(@"_find_sharee_footer_", nil);
  49. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeAccount];
  50. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_find_sharee_", nil) forKey:@"textField.placeholder"];
  51. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  52. [section addFormRow:row];
  53. section = [XLFormSectionDescriptor formSection];
  54. [form addFormSection:section];
  55. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_direct_sharee_title_", nil)];
  56. [form addFormSection:section];
  57. section.footerTitle = NSLocalizedString(@"_direct_sharee_footer_", nil);
  58. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"directUser" rowType:XLFormRowDescriptorTypeAccount];
  59. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_direct_sharee_", nil) forKey:@"textField.placeholder"];
  60. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  61. [section addFormRow:row];
  62. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_type_title_", nil)];
  63. [form addFormSection:section];
  64. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareType" rowType:XLFormRowDescriptorTypePicker];
  65. row.selectorOptions = @[NSLocalizedString(@"_share_type_user_", nil), NSLocalizedString(@"_share_type_group_", nil), NSLocalizedString(@"_share_type_remote_", nil)];
  66. row.value = NSLocalizedString(@"_share_type_user_", nil);
  67. self.shareType = shareTypeUser;
  68. row.height = 100;
  69. [section addFormRow:row];
  70. section = [XLFormSectionDescriptor formSection];
  71. [form addFormSection:section];
  72. self.form = form;
  73. }
  74. - (void)viewDidLoad
  75. {
  76. [super viewDidLoad];
  77. self.selectedItems = [[NSMutableArray alloc] init];
  78. self.view.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  79. [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
  80. self.endButton.tintColor = [NCBrandColor sharedInstance].brand;
  81. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  82. }
  83. #pragma --------------------------------------------------------------------------------------------
  84. #pragma mark ===== Button =====
  85. #pragma --------------------------------------------------------------------------------------------
  86. - (IBAction)endButtonAction:(id)sender
  87. {
  88. NSInteger permission;
  89. if (self.isDirectory) permission = k_max_folder_share_permission;
  90. else permission = k_max_file_share_permission;
  91. // start share of select users
  92. for (NSString *num in self.selectedItems) {
  93. // fix #166 Crashlytics
  94. if (self.users.count > 0 && [num integerValue] < self.users.count) {
  95. OCShareUser *item = [self.users objectAtIndex:[num integerValue]];
  96. [self.delegate shareUserAndGroup:item.name shareeType:item.shareeType permission:permission];
  97. }
  98. }
  99. // start share with a user if not be i
  100. if ([self.directUser isEqual:[NSNull null]] == NO) {
  101. if ([self.directUser length] > 0 && [self.directUser isEqualToString:app.activeUser] == NO) {
  102. // User/Group/Federate
  103. [self.delegate shareUserAndGroup:self.directUser shareeType:self.shareType permission:permission];
  104. }
  105. }
  106. [self dismissViewControllerAnimated:YES completion:nil];
  107. }
  108. #pragma --------------------------------------------------------------------------------------------
  109. #pragma mark ===== Change Value =====
  110. #pragma --------------------------------------------------------------------------------------------
  111. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  112. {
  113. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  114. if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanCheck]) {
  115. if ([newValue boolValue] == YES)
  116. [self.selectedItems addObject:rowDescriptor.tag];
  117. if ([newValue boolValue] == NO)
  118. [self.selectedItems removeObject:rowDescriptor.tag];
  119. }
  120. if ([rowDescriptor.tag isEqualToString:@"directUser"]) {
  121. self.directUser = newValue;
  122. }
  123. if ([rowDescriptor.tag isEqualToString:@"shareType"]){
  124. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_user_", nil)])
  125. self.shareType = shareTypeUser;
  126. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_group_", nil)])
  127. self.shareType = shareTypeGroup;
  128. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_remote_", nil)])
  129. self.shareType = shareTypeRemote;
  130. }
  131. }
  132. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  133. {
  134. [super endEditing:rowDescriptor];
  135. if ([rowDescriptor.tag isEqualToString:@"findUser"]) {
  136. rowDescriptor.value = [rowDescriptor.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  137. if ([rowDescriptor.value length] > 1)
  138. [self.delegate getUserAndGroup:rowDescriptor.value];
  139. }
  140. }
  141. #pragma --------------------------------------------------------------------------------------------
  142. #pragma mark ===== Delegate =====
  143. #pragma --------------------------------------------------------------------------------------------
  144. - (void)reloadUserAndGroup:(NSArray *)items
  145. {
  146. self.users = [[NSMutableArray alloc] initWithArray:items];
  147. self.form.delegate = nil;
  148. // remove the select users and i
  149. for (OCShareUser *user in items) {
  150. for (OCSharedDto *item in self.itemsShareWith)
  151. if ([item.shareWith isEqualToString:user.name] && ((item.shareType == shareTypeGroup && user.shareeType == 1) || (item.shareType != shareTypeGroup && user.shareeType == 0)))
  152. [self.users removeObject:user];
  153. if ([self.itemsShareWith containsObject:user.name] || [user.name isEqualToString:app.activeUser])
  154. [self.users removeObject:user];
  155. }
  156. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:1];
  157. [section.formRows removeAllObjects];
  158. for (OCShareUser *item in self.users) {
  159. NSInteger num = [self.users indexOfObject:item];
  160. NSString *title;
  161. if (item.shareeType == 1) title = [item.name stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  162. else title = item.name;
  163. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:[@(num) stringValue] rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
  164. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  165. [row.cellConfig setObject:[NCBrandColor sharedInstance].brand forKey:@"self.tintColor"];
  166. [section addFormRow:row];
  167. }
  168. [self.tableView reloadData];
  169. self.form.delegate = self;
  170. }
  171. @end