CCShareUserOC.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // CCShareUserOC.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 30/11/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. {
  28. AppDelegate *appDelegate;
  29. }
  30. @end
  31. @implementation CCShareUserOC
  32. - (instancetype)initWithCoder:(NSCoder *)coder
  33. {
  34. self = [super initWithCoder:coder];
  35. if (self) {
  36. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  37. self.directUser = @"";
  38. [self initializeForm];
  39. }
  40. return self;
  41. }
  42. - (void)initializeForm
  43. {
  44. XLFormDescriptor *form ;
  45. XLFormSectionDescriptor *section;
  46. XLFormRowDescriptor *row;
  47. form = [XLFormDescriptor formDescriptor];
  48. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  49. form.assignFirstResponderOnShow = NO;
  50. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_find_sharee_title_", nil)];
  51. [form addFormSection:section];
  52. section.footerTitle = NSLocalizedString(@"_find_sharee_footer_", nil);
  53. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"findUser" rowType:XLFormRowDescriptorTypeAccount];
  54. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_find_sharee_", nil) forKey:@"textField.placeholder"];
  55. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  56. [section addFormRow:row];
  57. section = [XLFormSectionDescriptor formSection];
  58. [form addFormSection:section];
  59. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_direct_sharee_title_", nil)];
  60. [form addFormSection:section];
  61. section.footerTitle = NSLocalizedString(@"_direct_sharee_footer_", nil);
  62. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"directUser" rowType:XLFormRowDescriptorTypeAccount];
  63. [row.cellConfigAtConfigure setObject:NSLocalizedString(@"_direct_sharee_", nil) forKey:@"textField.placeholder"];
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  65. [section addFormRow:row];
  66. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_share_type_title_", nil)];
  67. [form addFormSection:section];
  68. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"shareType" rowType:XLFormRowDescriptorTypePicker];
  69. row.selectorOptions = @[NSLocalizedString(@"_share_type_user_", nil), NSLocalizedString(@"_share_type_group_", nil), NSLocalizedString(@"_share_type_remote_", nil)];
  70. row.value = NSLocalizedString(@"_share_type_user_", nil);
  71. self.shareType = shareTypeUser;
  72. row.height = 100;
  73. [section addFormRow:row];
  74. section = [XLFormSectionDescriptor formSection];
  75. [form addFormSection:section];
  76. self.form = form;
  77. }
  78. - (void)viewDidLoad
  79. {
  80. [super viewDidLoad];
  81. self.selectedItems = [[NSMutableArray alloc] init];
  82. self.view.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  83. [self.endButton setTitle:NSLocalizedString(@"_done_", nil) forState:UIControlStateNormal];
  84. self.endButton.tintColor = [UIColor blackColor];
  85. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark ===== Button =====
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (IBAction)endButtonAction:(id)sender
  91. {
  92. NSInteger permission;
  93. if (self.isDirectory) permission = k_max_folder_share_permission;
  94. else permission = k_max_file_share_permission;
  95. // start share of select users
  96. for (NSString *num in self.selectedItems) {
  97. // fix #166 Crashlytics
  98. if (self.users.count > 0 && [num integerValue] < self.users.count) {
  99. OCShareUser *item = [self.users objectAtIndex:[num integerValue]];
  100. [self.delegate shareUserAndGroup:item.name shareeType:item.shareeType permission:permission];
  101. }
  102. }
  103. // start share with a user if not be i
  104. if ([self.directUser isEqual:[NSNull null]] == NO) {
  105. if ([self.directUser length] > 0 && [self.directUser isEqualToString:appDelegate.activeUser] == NO) {
  106. // User/Group/Federate
  107. [self.delegate shareUserAndGroup:self.directUser shareeType:self.shareType permission:permission];
  108. }
  109. }
  110. [self dismissViewControllerAnimated:YES completion:nil];
  111. }
  112. #pragma --------------------------------------------------------------------------------------------
  113. #pragma mark ===== Change Value =====
  114. #pragma --------------------------------------------------------------------------------------------
  115. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  116. {
  117. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  118. if ([rowDescriptor.rowType isEqualToString:XLFormRowDescriptorTypeBooleanCheck]) {
  119. if ([newValue boolValue] == YES)
  120. [self.selectedItems addObject:rowDescriptor.tag];
  121. if ([newValue boolValue] == NO)
  122. [self.selectedItems removeObject:rowDescriptor.tag];
  123. }
  124. if ([rowDescriptor.tag isEqualToString:@"directUser"]) {
  125. self.directUser = newValue;
  126. }
  127. if ([rowDescriptor.tag isEqualToString:@"shareType"]){
  128. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_user_", nil)])
  129. self.shareType = shareTypeUser;
  130. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_group_", nil)])
  131. self.shareType = shareTypeGroup;
  132. if ([newValue isEqualToString:NSLocalizedString(@"_share_type_remote_", nil)])
  133. self.shareType = shareTypeRemote;
  134. }
  135. }
  136. - (void)endEditing:(XLFormRowDescriptor *)rowDescriptor
  137. {
  138. [super endEditing:rowDescriptor];
  139. if ([rowDescriptor.tag isEqualToString:@"findUser"]) {
  140. rowDescriptor.value = [rowDescriptor.value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  141. if ([rowDescriptor.value length] > 1)
  142. [self.delegate getUserAndGroup:rowDescriptor.value];
  143. }
  144. }
  145. #pragma --------------------------------------------------------------------------------------------
  146. #pragma mark ===== Delegate =====
  147. #pragma --------------------------------------------------------------------------------------------
  148. - (void)reloadUserAndGroup:(NSArray *)items
  149. {
  150. self.users = [[NSMutableArray alloc] initWithArray:items];
  151. self.form.delegate = nil;
  152. // remove the select users and i
  153. for (OCShareUser *user in items) {
  154. for (OCSharedDto *item in self.itemsShareWith)
  155. if ([item.shareWith isEqualToString:user.name] && ((item.shareType == shareTypeGroup && user.shareeType == 1) || (item.shareType != shareTypeGroup && user.shareeType == 0)))
  156. [self.users removeObject:user];
  157. if ([self.itemsShareWith containsObject:user.name] || [user.name isEqualToString:appDelegate.activeUser])
  158. [self.users removeObject:user];
  159. }
  160. XLFormSectionDescriptor *section = [self.form formSectionAtIndex:1];
  161. [section.formRows removeAllObjects];
  162. for (OCShareUser *item in self.users) {
  163. NSInteger num = [self.users indexOfObject:item];
  164. NSString *title;
  165. if (item.shareeType == 1) {
  166. if (item.displayName)
  167. title = [item.displayName stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  168. else
  169. title = [item.name stringByAppendingString:NSLocalizedString(@"_user_is_group_", nil)];
  170. } else {
  171. if (item.displayName)
  172. title = item.displayName;
  173. else
  174. title = item.name;
  175. }
  176. XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:[@(num) stringValue] rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
  177. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  178. [row.cellConfig setObject:[NCBrandColor sharedInstance].brandElement forKey:@"self.tintColor"];
  179. [section addFormRow:row];
  180. }
  181. [self.tableView reloadData];
  182. self.form.delegate = self;
  183. }
  184. @end