Guest User

Untitled

a guest
Dec 11th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. /*
  2. The most trivial implementation of what I need. Introduces code
  3. duplication and can become a hassle to maintain if more and more
  4. data sources are introduced.
  5.  
  6. What would be the best approach to keep the code organization as
  7. simple as possible but on the other hand keep the clarity of this
  8. two data source example?
  9.  
  10. When should a programmer "feel the pain" of code duplication?
  11. For example I think in this example I'd keep it like it is
  12. now to make the intention and code structure more clear. I think this
  13. is effectively in its current form just "code unrolling". There are
  14. less function calls and the functionality is more centralized and comprehendable.
  15.  
  16. An obvious way to optimize this is to introduce functions that extract the common
  17. functionalities and use them in their current places. I think
  18. the end result would become less obvious when it's been looked over
  19. again some time later. And the basic structure of switches would still exist unless there
  20. is another layer that maps view tags to actions.
  21.  
  22. Another way would be to have a declarative description of
  23. the situation and it would generate this code that would be again
  24. easy to read but probably hard to maintain as-it-is.
  25. */
  26.  
  27. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  28. {
  29. switch(tableView.tag) {
  30. case kLatestActionsViewTag:
  31. return [[Battle sharedBattle].latestActions count];
  32. case kTopPlayersViewTag:
  33. return [[Battle sharedBattle].topPlayers count];
  34. default:
  35. return 0;
  36. }
  37. }
  38.  
  39. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  40. {
  41. UITableViewCell *cell;
  42. Battle *battle = [Battle sharedBattle];
  43.  
  44. switch(tableView.tag) {
  45. case kLatestActionsViewTag: {
  46. cell = [tableView dequeueReusableCellWithIdentifier:@"BattleLatestActionCell"];
  47. Action *current = [battle.latestActions objectAtIndex:indexPath.row];
  48. cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", current.player, current.type];
  49. cell.detailTextLabel.text = [NSDate relativeTimeStringSinceDate:current.time];
  50. break;
  51. }
  52. case kTopPlayersViewTag: {
  53. cell = [tableView dequeueReusableCellWithIdentifier:@"BattleTopPlayerCell"];
  54. BasePlayer *player = [battle.topPlayers objectAtIndex:indexPath.row];
  55. cell.textLabel.text = [NSString stringWithFormat:@"%d. %@", player.position, player.name];
  56. cell.detailTextLabel.text = player.team;
  57. break;
  58. }
  59. }
  60.  
  61. return cell;
  62. }
  63.  
  64. - (void)battleModel:(Battle*)battle didUpdateField:(BattleModelField)field
  65. {
  66. switch (field) {
  67. case kBattleModelFieldCurrentPlayer:
  68. {
  69. UILabel *label = (UILabel*)[self.view viewWithTag:656];
  70. label.text = [NSString stringWithFormat:@"%@ – %d pistettä", battle.currentPlayer.name, battle.currentPlayer.points];
  71. [self.indicator stopAnimating];
  72. break;
  73. }
  74. case kBattleModelFieldLatestActions:
  75. {
  76. UITableView* latest = (UITableView*)[self.view viewWithTag:kLatestActionsViewTag];
  77. [latest reloadData];
  78. break;
  79. }
  80. case kBattleModelFieldTopPlayers:
  81. {
  82. UITableView* topPlayers = (UITableView*)[self.view viewWithTag:kTopPlayersViewTag];
  83. [topPlayers reloadData];
  84. break;
  85. }
  86. default:
  87. break;
  88. }
  89. }
  90.  
  91. - (IBAction)segmentChanged:(UISegmentedControl *)sender {
  92. switch(sender.selectedSegmentIndex) {
  93. case 0:
  94. break;
  95. case kLatestActionsViewTag:
  96. [[self.view viewWithTag:kLatestActionsViewTag] setHidden:NO];
  97. [[self.view viewWithTag:kTopPlayersViewTag] setHidden:YES];
  98. break;
  99. case kTopPlayersViewTag:
  100. [[self.view viewWithTag:kLatestActionsViewTag] setHidden:YES];
  101. [[self.view viewWithTag:kTopPlayersViewTag] setHidden:NO];
  102. break;
  103. }
  104. }
Add Comment
Please, Sign In to add comment