VMConfigSystemArgumentsViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "VMConfigSystemArgumentsViewController.h"
  17. #import "VMConfigDriveDetailViewController.h"
  18. #import "UTMQemuConfiguration+System.h"
  19. @interface VMConfigSystemArgumentsViewController ()
  20. @end
  21. @implementation VMConfigSystemArgumentsViewController
  22. @synthesize configuration;
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. self.navigationItem.rightBarButtonItems = @[ self.addButtonItem, self.editButtonItem ];
  26. }
  27. - (void)viewDidAppear:(BOOL)animated {
  28. [super viewDidAppear:animated];
  29. [self refreshViewFromConfiguration];
  30. }
  31. - (void)refreshViewFromConfiguration {
  32. [self.tableView reloadData];
  33. }
  34. #pragma mark - Table view data source
  35. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  36. return 1;
  37. }
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  39. NSAssert(section == 0, @"Invalid section.");
  40. NSInteger count = self.configuration.countArguments;
  41. // Insert an empty argument if there are no existing arguments.
  42. if (count == 0) {
  43. [self.configuration newArgument:@""];
  44. return count + 1;
  45. }
  46. return count;
  47. }
  48. - (VMConfigSystemArgumentsTextCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  49. VMConfigSystemArgumentsTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"argumentCell" forIndexPath:indexPath];
  50. NSAssert(indexPath.section == 0, @"Invalid section");
  51. NSAssert(cell, @"Invalid cell");
  52. NSInteger row = indexPath.row;
  53. cell.argTextItem.tag = row;
  54. cell.argTextItem.text = [self.configuration argumentForIndex:row];
  55. cell.configuration = configuration;
  56. return cell;
  57. }
  58. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  59. NSAssert(indexPath.section == 0, @"Invalid section");
  60. return YES;
  61. }
  62. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  63. NSAssert(indexPath.section == 0, @"Invalid section");
  64. if (editingStyle == UITableViewCellEditingStyleDelete) {
  65. // If there's only one argument, add a new item at 1 before deleting the first so we're never at 0 rows.
  66. if ([self.configuration countArguments] == 1) {
  67. [self.configuration updateArgumentAtIndex:0 withValue:@""];
  68. } else {
  69. // Delete the row and coorresponding argument.
  70. [self.configuration removeArgumentAtIndex:indexPath.row];
  71. [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  72. }
  73. [tableView reloadData];
  74. }
  75. }
  76. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  77. NSAssert(fromIndexPath.section == 0, @"Invalid section");
  78. NSAssert(toIndexPath.section == 0, @"Invalid section");
  79. [self.configuration moveArgumentIndex:fromIndexPath.row to:toIndexPath.row];
  80. }
  81. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
  82. NSAssert(indexPath.section == 0, @"Invalid section");
  83. return YES;
  84. }
  85. - (IBAction)addButton:(id *)sender {
  86. // Insert a new empty argument.
  87. [self.configuration newArgument:@""];
  88. [self.tableView reloadData];
  89. }
  90. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  91. // done button was pressed - dismiss keyboard
  92. [textField resignFirstResponder];
  93. return YES;
  94. }
  95. @end
  96. #pragma mark - Text input from cell
  97. @interface VMConfigSystemArgumentsTextCell()
  98. @end
  99. @implementation VMConfigSystemArgumentsTextCell
  100. @synthesize configuration;
  101. - (IBAction)editingDidEnd:(UITextField *)sender {
  102. [self.configuration updateArgumentAtIndex:sender.tag withValue:sender.text];
  103. [self.argTextItem endEditing:true];
  104. }
  105. - (IBAction)valueWasChanged:(UITextField *)sender {
  106. [self.argTextItem endEditing:true];
  107. }
  108. - (IBAction)touchWasCancelled:(UITextField *)sender {
  109. [self.argTextItem endEditing:true];
  110. }
  111. @end