Dynamically adding myLauncher items -- is this a good way to do it?

57 views
Skip to first unread message

mpemburn

unread,
Jun 3, 2012, 8:54:11 PM6/3/12
to myLauncher
Hi All,

I've been having fun working with myLauncher the past couple of days.
My app has several categories of things (events, lists and videos)
that I want the user to be able to add to and remove from the
myLauncher pages as they wish. I call these things "push pin". The
code is split between the main myLauncher screen (the
DetailViewController of an iPad Split View app) and the "ListManager"
class that handles the lists of these entities. Here's how I'm doing
it:

* First, I had to expose some of the methods of MyLauncherView:

-(void)saveToUserDefaults:(id)object key:(NSString *)key;
-(void)savePages;
-(void)organizePages;

* Next, I subclassed ListManager to MyLauncherViewController. For
some reason, it would not access the the launcherView directly so I
added an instance of MyLauncherView:

MyLauncherView *launcherView;
.
.
.
launcherView = [[MyLauncherView alloc] init];

* When the user selects the "push pin" from the the list view, it
calls the following in ListManager:

- (void) setPushpin
{

NSMutableArray *currentItems = [self savedLauncherItems];
NSMutableArray *pageOne;
MyLauncherItem *newPushpin = [[MyLauncherItem alloc]
initWithTitle: @"Diane Selwyn"
iPhoneImage:
@""
iPadImage:
@"pinned_list"
target:
@"ItemViewController"
targetTitle :
@"Diane Selwyn"
deletable :
YES];
//*** See if there is already an array of myLauncher items.
// If not . . .
if ([currentItems count] == 0) {
//*** Create a new page and add the new item to it
pageOne = [[NSMutableArray alloc] init];
[pageOne addObject: newPushpin];
//*** Add page one to the collection of pages
[currentItems addObject: pageOne];
} else {
//*** Get the array for the existing first page
pageOne = [currentItems objectAtIndex: 0];
//*** If the page isn't full, stick the new item at the end
if ([pageOne count] < [launcherView maxItemsPerPage]) {
[pageOne addObject: newPushpin];
} else {
//*** Otherwise, stick the new item at the beginning
[pageOne insertObject: newPushpin atIndex: 0];
//*** . . . and let the launcherView organize things.
[launcherView organizePages];
}
}
[launcherView setPages: currentItems];

[launcherView savePages];

[popover dismissPopoverAnimated: YES];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"PushpinAddedNotification" object: nil];
}

* Meanwhile, back at the DetailViewController, we've been listening
for a "PushpinAddedNotification":

[[NSNotificationCenter defaultCenter] addObserver:self selector:
@selector(refreshLauncher) name: @"PushpinAddedNotification" object:
nil];

* When we get the notification the myLauncher layout is refreshed:

- (void) refreshLauncher
{
[self.launcherView setPages:[self savedLauncherItems]];
[self.masterPopoverController dismissPopoverAnimated:YES];
}

I'd love to hear feedback and suggestions. Perhaps there's a cleaner
way to do this that as a relative noob, I might have missed.

Mark




Jarada

unread,
Jul 1, 2012, 10:37:36 AM7/1/12
to mylau...@googlegroups.com
Hi Mark,

Apologies for the slow reply, the past month or so has been hectic.

That seems as good a selection of code as any. MyLauncher is good for hacking into and getting it to run the way you would like it to run.

That being said, it seems a good way of adding an item to a MyLauncherView is needed. I already have an issue of this on Github, so I will work to add this into the codebase.

Cheers
Reply all
Reply to author
Forward
0 new messages