PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/three20/samples/TTCatalog/Classes/MockDataSource.m

http://github.com/hac76/three20Macosx
Objective C | 577 lines | 491 code | 63 blank | 23 comment | 22 complexity | b0a2aae3eac6bcc077f408e9f9651bdb MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. #import "MockDataSource.h"
  2. @interface MockAddressBook ()
  3. - (void) loadNames;
  4. @end
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6. @implementation MockAddressBook
  7. @synthesize names = _names, fakeSearchDuration = _fakeSearchDuration, fakeLoadingDuration = _fakeLoadingDuration;
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. // class public
  10. + (NSMutableArray*)fakeNames {
  11. return [NSMutableArray arrayWithObjects:
  12. @"Hector Lewis",
  13. @"Juanita Fredrick",
  14. @"Richard Raymond",
  15. @"Marcia Myer",
  16. @"Shannon Mahoney",
  17. @"James Steiner",
  18. @"Daniel Lloyd",
  19. @"Fredrick Hutchins",
  20. @"Tracey Smith",
  21. @"Brandon Rutherford",
  22. @"Megan Lopez",
  23. @"Jean Trujillo",
  24. @"Franklin Diamond",
  25. @"Mildred Jacobsen",
  26. @"Sandra Adams",
  27. @"Debra Pugliese",
  28. @"Cynthia Hall",
  29. @"Joshua Hicks",
  30. @"Lorenzo Evatt",
  31. @"Erica Dozier",
  32. @"Barbara Lazarus",
  33. @"Joye Hocker",
  34. @"Henry Arana",
  35. @"Glen Cabrales",
  36. @"Mai Valdez",
  37. @"Travis Book",
  38. @"John Ibanez",
  39. @"Barbara White",
  40. @"Cassandra Smith",
  41. @"Virginia Costilla",
  42. @"Rachel Baker",
  43. @"Mildred Foley",
  44. @"Todd Nevels",
  45. @"William Boes",
  46. @"Claire Harrell",
  47. @"Charlotte Espinoza",
  48. @"Gerald Miller",
  49. @"Lucille Lankford",
  50. @"Lloyd Fritz",
  51. @"Charlie Cabrera",
  52. @"Shane Vogt",
  53. @"Cynthia Simon",
  54. @"Clayton Land",
  55. @"Roy Stahl",
  56. @"Helen Peralta",
  57. @"Reinaldo Self",
  58. @"Thomas Carter",
  59. @"Anthony Hebert",
  60. @"Timothy Abernathy",
  61. @"Rob Magruder",
  62. @"Ira Kaplan",
  63. @"Dixie Gurney",
  64. @"Darrin Pritchard",
  65. @"Patrick Brooks",
  66. @"Norman Sanchez",
  67. @"Vickie Robbins",
  68. @"Santiago Chiles",
  69. @"Douglas Bearden",
  70. @"Cora Jones",
  71. @"Donald Kennedy",
  72. @"Henry Nelson",
  73. @"Rosa Judge",
  74. @"Andrew Khan",
  75. @"Lauretta Rose",
  76. @"Mildred Nance",
  77. @"Antoinette Delarosa",
  78. @"Stephanie Johnson",
  79. @"Geoffrey Perry",
  80. @"Sally Houston",
  81. @"Pamela Pellegrin",
  82. @"Nereida Faul",
  83. @"Nichole Moore",
  84. @"David Thompson",
  85. @"Rob Burt",
  86. @"Mary Gelb",
  87. @"Glenda Stgeorge",
  88. @"Lydia Freeman",
  89. @"Otto Brown",
  90. @"Erica Cooke",
  91. @"Evelyn Stephens",
  92. @"Vanessa Ayers",
  93. @"Jeffrey Kirk",
  94. @"Christine Stradford",
  95. @"John Murphy",
  96. @"Paul Pederson",
  97. @"Genevieve Barrett",
  98. @"Stanley Kelly",
  99. @"Marie Noel",
  100. @"Mike Mathis",
  101. @"Albert Gary",
  102. @"Alice Thomas",
  103. @"Anna Bond",
  104. @"Bobby Gaines",
  105. @"Helen Ellis",
  106. @"Bobbie Thayer",
  107. @"Dorothy Totten",
  108. @"Laura Wegener",
  109. @"Pam Hackett",
  110. @"Arlene Blount",
  111. @"James Clark",
  112. @"Richard Harris",
  113. @"Joseph Cain",
  114. @"Stacy Jones",
  115. @"Bonnie Gonzalez",
  116. @"Maria Bailey",
  117. @"Francis Caldwell",
  118. @"Anthony Gale",
  119. @"John Zackery",
  120. @"Patricia Taylor",
  121. @"Kimberly Jarrett",
  122. @"Carol Dennie",
  123. @"Betty Zager",
  124. @"Ellen Godines",
  125. @"Edward Adams",
  126. @"Ricky Salamanca",
  127. @"Elizabeth Ruvalcaba",
  128. @"Veronica Esposito",
  129. @"Russel Owen",
  130. @"Harry Plascencia",
  131. @"Thomas Dewalt",
  132. @"Robert Eldred",
  133. @"Frank Buerger",
  134. @"Phillip James",
  135. @"James Beverly",
  136. @"Michael Mcallister",
  137. @"George Nichols",
  138. @"Richard Larson",
  139. @"Patricia Ramirez",
  140. @"Rob Govan",
  141. @"Charles Johnston",
  142. @"David Rogers",
  143. @"Homer Allen",
  144. @"Carolyn Green",
  145. @"Velma Beery",
  146. @"Ida Garcia",
  147. @"Jasmine Creighton",
  148. @"Ozie Templin",
  149. @"Julia Hudson",
  150. @"David Cortez",
  151. @"Tina Henderson",
  152. @"Janette Bray",
  153. @"Michael Hamilton",
  154. @"Andrew Bennett",
  155. @"Margarita Lehmann",
  156. @"Stephanie Whitehead",
  157. @"Mary Saladino",
  158. @"Nicholas Alaniz",
  159. @"John Escobedo",
  160. @"Macie Workman",
  161. @"Michelle Thomas",
  162. @"Robert Carvalho",
  163. @"Allen Johnson",
  164. @"Will Norris",
  165. @"Matthew Mabrey",
  166. @"Vicki Howard",
  167. @"Annie Campbell",
  168. @"Stephen Anderson",
  169. @"Leah Scott",
  170. @"Dominic Winters",
  171. @"Catherine Rondeau",
  172. @"Amanda Hall",
  173. @"Michael Hucks",
  174. @"Truman Vidal",
  175. @"Jennifer Worley",
  176. @"Jack Fiore",
  177. @"Douglas Irby",
  178. @"Ana Bonilla",
  179. @"Jacob Lankford",
  180. @"Robert Garcia",
  181. @"Helene Arispe",
  182. @"Ola Barnhill",
  183. @"Rosario Delaney",
  184. @"Robert Estes",
  185. @"Karen Carter",
  186. @"Hazel Thompson",
  187. @"Rebecca Mack",
  188. @"Lashawn Rice",
  189. @"Harry Davis",
  190. @"Matilda Woodbury",
  191. @"Celia Barnhart",
  192. @"Ava Murray",
  193. @"John Williams",
  194. @"Debbie Watts",
  195. @"Karen Borel",
  196. @"Neta Yingst",
  197. @"Roy Carter",
  198. @"Leon Roberts",
  199. @"Evette Jones",
  200. @"Dianna Adams",
  201. @"Brian Reed",
  202. @"Joyce Ritter",
  203. @"Stephen Diaz",
  204. @"Elizabeth Shealey",
  205. @"Barbara Hatch",
  206. @"Doug Harrison",
  207. @"Sharon Castro",
  208. @"Juan Frakes",
  209. @"Gary Berry",
  210. @"Helen Zamora",
  211. @"Sarah Woodard",
  212. @"Phillip Hebert",
  213. @"Weldon Goldsmith",
  214. @"Lisa Segura",
  215. @"Adrienne Pedroza",
  216. @"Alfred Ransom",
  217. @"Everett Rambo",
  218. @"John Phillips",
  219. @"Carol Mccabe",
  220. @"James Martinez",
  221. @"Trang Strickland",
  222. @"Linda Brown",
  223. @"Shayla Hodgson",
  224. @"Magdalena Moore",
  225. @"Gwendolyn Meyers",
  226. @"Stephen Williams",
  227. @"Pamela Radford",
  228. @"Jeffrey Hooker",
  229. @"David Catania",
  230. @"Marvin Miller",
  231. @"Laura Murray",
  232. @"Dorothy James",
  233. @"Susannah Gibson",
  234. @"Melissa Owens",
  235. @"Deborah Horrell",
  236. @"Franklin Williams",
  237. @"Scott Brown",
  238. @"Adam Gautreaux",
  239. @"Joseph Byers",
  240. @"Deborah Barker",
  241. @"Linda Virgin",
  242. @"Patricia Mitchell",
  243. @"John Donovan",
  244. @"Robert Hamill",
  245. @"Jamel Mcmillen",
  246. @"Patty Guzzi",
  247. @"Mildred Leon",
  248. @"Benjamin Mapes",
  249. @"Allen Cave",
  250. @"Patrick Sims",
  251. @"Patrick Mercado",
  252. @"Joyce Orozco",
  253. @"Gloria Bell",
  254. @"Gerard Kelley",
  255. @"Molly Grant",
  256. @"Patrick Perez",
  257. @"John Corey",
  258. @"Ryan Clemmer",
  259. @"James Davis",
  260. @"Kevin Scarborough",
  261. @"Frankie Montagna",
  262. @"Terry Joseph",
  263. @"Ira Boyle",
  264. @"Kathryn Hildebrandt",
  265. @"James Larson",
  266. @"Shelia Easter",
  267. @"Gabriel Dorsett",
  268. @"Irene Mcbroom",
  269. @"Robert Hardin",
  270. @"Sue Grossi",
  271. @"Rebecca Stock",
  272. @"Ruby Kiefer",
  273. @"John Sandoval",
  274. @"Peter King",
  275. @"Joseph Fleming",
  276. @"Joan Swenson",
  277. @"Conrad Savory",
  278. @"Grant Kowalewski",
  279. @"Shirley Garcia",
  280. @"Dorris West",
  281. @"Jane Tran",
  282. @"Meaghan Lam",
  283. @"Jorge White",
  284. @"Catherine Whittaker",
  285. @"Katherine Hope",
  286. @"Maria Underwood",
  287. @"Rose Ray",
  288. @"Samatha Sedlacek",
  289. @"Jerri Sampson",
  290. @"Loren Bourdeau",
  291. @"William Wilson",
  292. @"Stephen Canady",
  293. @"Elaine Ruth",
  294. @"Marcus Martin",
  295. @"Aileen Kemp",
  296. @"Kevin Maupin",
  297. @"Martha Vanwinkle",
  298. @"Kelly Munk",
  299. @"Lisa Rosato",
  300. @"Marcella Jett",
  301. @"Barbara Murphy",
  302. @"Edward Sherrod",
  303. @"Jane Cushing",
  304. @"Clara Sims",
  305. @"Velma Moreno",
  306. @"Eric Noguera",
  307. @"Shirley Williams",
  308. @"Gina Millard",
  309. @"Edward Deputy",
  310. @"Jennifer Myers",
  311. @"Mary Spurgeon",
  312. nil];
  313. }
  314. ///////////////////////////////////////////////////////////////////////////////////////////////////
  315. // private
  316. - (void)fakeSearch:(NSString*)text {
  317. self.names = [NSMutableArray array];
  318. if (text.length) {
  319. text = [text lowercaseString];
  320. for (NSString* name in _allNames) {
  321. if ([[name lowercaseString] rangeOfString:text].location == 0) {
  322. [_names addObject:name];
  323. }
  324. }
  325. }
  326. [_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
  327. }
  328. - (void)fakeSearchReady:(NSTimer*)timer {
  329. _fakeSearchTimer = nil;
  330. NSString* text = timer.userInfo;
  331. [self fakeSearch:text];
  332. }
  333. ///////////////////////////////////////////////////////////////////////////////////////////////////
  334. // NSObject
  335. - (id)initWithNames:(NSArray*)names {
  336. if (self = [super init]) {
  337. _delegates = nil;
  338. _allNames = [names copy];
  339. _names = nil;
  340. _fakeSearchTimer = nil;
  341. _fakeSearchDuration = 0;
  342. }
  343. return self;
  344. }
  345. - (void)dealloc {
  346. TT_INVALIDATE_TIMER(_fakeSearchTimer);
  347. TT_INVALIDATE_TIMER(_fakeLoadingTimer)
  348. TT_RELEASE_SAFELY(_delegates);
  349. TT_RELEASE_SAFELY(_allNames);
  350. TT_RELEASE_SAFELY(_names);
  351. [super dealloc];
  352. }
  353. ///////////////////////////////////////////////////////////////////////////////////////////////////
  354. // TTModel
  355. - (NSMutableArray*)delegates {
  356. if (!_delegates) {
  357. _delegates = TTCreateNonRetainingArray();
  358. }
  359. return _delegates;
  360. }
  361. - (BOOL)isLoadingMore {
  362. return NO;
  363. }
  364. - (BOOL)isOutdated {
  365. return NO;
  366. }
  367. - (BOOL)isLoaded {
  368. return !!_names;
  369. }
  370. - (BOOL)isLoading {
  371. return !!_fakeSearchTimer || !!_fakeLoadingTimer;
  372. }
  373. - (BOOL)isEmpty {
  374. return !_names.count;
  375. }
  376. - (void) fakeLoadingReady {
  377. _fakeLoadingTimer = nil;
  378. [self loadNames];
  379. [_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
  380. }
  381. - (void)load:(TTURLRequestCachePolicy)cachePolicy more:(BOOL)more {
  382. [_delegates perform:@selector(modelDidStartLoad:) withObject:self];
  383. if (_fakeLoadingDuration) {
  384. TT_INVALIDATE_TIMER(_fakeLoadingTimer);
  385. _fakeLoadingTimer = [NSTimer scheduledTimerWithTimeInterval:_fakeLoadingDuration target:self
  386. selector:@selector(fakeLoadingReady) userInfo:nil repeats:NO];
  387. [_delegates perform:@selector(modelDidStartLoad:) withObject:self];
  388. } else {
  389. [self loadNames];
  390. [_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
  391. }
  392. }
  393. - (void)invalidate:(BOOL)erase {
  394. }
  395. - (void)cancel {
  396. if (_fakeSearchTimer) {
  397. TT_INVALIDATE_TIMER(_fakeSearchTimer);
  398. [_delegates perform:@selector(modelDidCancelLoad:) withObject:self];
  399. } else if(_fakeLoadingTimer) {
  400. TT_INVALIDATE_TIMER(_fakeLoadingTimer);
  401. [_delegates perform:@selector(modelDidCancelLoad:) withObject:self];
  402. }
  403. }
  404. ///////////////////////////////////////////////////////////////////////////////////////////////////
  405. // public
  406. - (void)loadNames {
  407. TT_RELEASE_SAFELY(_names);
  408. _names = [_allNames mutableCopy];
  409. }
  410. - (void)search:(NSString*)text {
  411. [self cancel];
  412. TT_RELEASE_SAFELY(_names);
  413. if (text.length) {
  414. if (_fakeSearchDuration) {
  415. TT_INVALIDATE_TIMER(_fakeSearchTimer);
  416. _fakeSearchTimer = [NSTimer scheduledTimerWithTimeInterval:_fakeSearchDuration target:self
  417. selector:@selector(fakeSearchReady:) userInfo:text repeats:NO];
  418. [_delegates perform:@selector(modelDidStartLoad:) withObject:self];
  419. } else {
  420. [self fakeSearch:text];
  421. [_delegates perform:@selector(modelDidFinishLoad:) withObject:self];
  422. }
  423. } else {
  424. [_delegates perform:@selector(modelDidChange:) withObject:self];
  425. }
  426. }
  427. @end
  428. ///////////////////////////////////////////////////////////////////////////////////////////////////
  429. @implementation MockDataSource
  430. @synthesize addressBook = _addressBook;
  431. ///////////////////////////////////////////////////////////////////////////////////////////////////
  432. // NSObject
  433. - (id)init {
  434. if (self = [super init]) {
  435. _addressBook = [[MockAddressBook alloc] initWithNames:[MockAddressBook fakeNames]];
  436. self.model = _addressBook;
  437. }
  438. return self;
  439. }
  440. - (void)dealloc {
  441. TT_RELEASE_SAFELY(_addressBook);
  442. [super dealloc];
  443. }
  444. ///////////////////////////////////////////////////////////////////////////////////////////////////
  445. // UITableViewDataSource
  446. - (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView {
  447. return [TTTableViewDataSource lettersForSectionsWithSearch:YES summary:NO];
  448. }
  449. ///////////////////////////////////////////////////////////////////////////////////////////////////
  450. // TTTableViewDataSource
  451. - (void)tableViewDidLoadModel:(UITableView*)tableView {
  452. self.items = [NSMutableArray array];
  453. self.sections = [NSMutableArray array];
  454. NSMutableDictionary* groups = [NSMutableDictionary dictionary];
  455. for (NSString* name in _addressBook.names) {
  456. NSString* letter = [NSString stringWithFormat:@"%C", [name characterAtIndex:0]];
  457. NSMutableArray* section = [groups objectForKey:letter];
  458. if (!section) {
  459. section = [NSMutableArray array];
  460. [groups setObject:section forKey:letter];
  461. }
  462. TTTableItem* item = [TTTableTextItem itemWithText:name URL:nil];
  463. [section addObject:item];
  464. }
  465. NSArray* letters = [groups.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  466. for (NSString* letter in letters) {
  467. NSArray* items = [groups objectForKey:letter];
  468. [_sections addObject:letter];
  469. [_items addObject:items];
  470. }
  471. }
  472. - (id<TTModel>)model {
  473. return _addressBook;
  474. }
  475. @end
  476. ///////////////////////////////////////////////////////////////////////////////////////////////////
  477. @implementation MockSearchDataSource
  478. @synthesize addressBook = _addressBook;
  479. ///////////////////////////////////////////////////////////////////////////////////////////////////
  480. // NSObject
  481. - (id)initWithDuration:(NSTimeInterval)duration {
  482. if (self = [super init]) {
  483. _addressBook = [[MockAddressBook alloc] initWithNames:[MockAddressBook fakeNames]];
  484. _addressBook.fakeSearchDuration = duration;
  485. self.model = _addressBook;
  486. }
  487. return self;
  488. }
  489. - (id)init {
  490. return [self initWithDuration:0];
  491. }
  492. - (void)dealloc {
  493. TT_RELEASE_SAFELY(_addressBook);
  494. [super dealloc];
  495. }
  496. ///////////////////////////////////////////////////////////////////////////////////////////////////
  497. // TTTableViewDataSource
  498. - (void)tableViewDidLoadModel:(UITableView*)tableView {
  499. self.items = [NSMutableArray array];
  500. for (NSString* name in _addressBook.names) {
  501. TTTableItem* item = [TTTableTextItem itemWithText:name URL:@"http://google.com"];
  502. [_items addObject:item];
  503. }
  504. }
  505. - (void)search:(NSString*)text {
  506. [_addressBook search:text];
  507. }
  508. - (NSString*)titleForLoading:(BOOL)reloading {
  509. return @"Searching...";
  510. }
  511. - (NSString*)titleForNoData {
  512. return @"No names found";
  513. }
  514. @end