Skip to content

Commit

Permalink
Merge pull request juleswhite#7 from WoodyElf/master
Browse files Browse the repository at this point in the history
Added code in MainActivity.java and ContactOps.java to handle impleme…
  • Loading branch information
douglascraigschmidt committed Jun 29, 2015
2 parents c145eaa + 97510a1 commit a00785f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public boolean chooseOpsOption(MenuItem item) {
switch (item.getItemId()) {
case R.id.simpleImpl:
getOps().setContactsOpsImplType
(ContactsOps.ContactsOpsImplType.SIMPLE);
(ContactsOps.ContactsOpsImplType.SIMPLE);
Utils.showToast(this,
"ContactsOpsImplSimple selected");
break;

case R.id.asyncImpl:
getOps().setContactsOpsImplType
(ContactsOps.ContactsOpsImplType.ASYNC);
(ContactsOps.ContactsOpsImplType.ASYNC);
Utils.showToast(this,
"ContactsOpsImplAsync selected");
break;
Expand All @@ -114,6 +114,18 @@ public boolean chooseOpsOption(MenuItem item) {
"ContactsOpsImplLoaderManager selected");
break;
}

// The calls to setContactsOpsImplType above will both set
// the new implementation type as well as construct a new
// instance of that implementation. This requires initializing
// the implementation weak reference to the this Activity,
// which can be accomplished by generating a fake configuration
// change event. Also, since the ContactOps implementation was
// just constructed and is not being restored, we need to pass
// in true for the "firstTimeIn" in parameter.
//
getOps().onConfiguration(this, true);

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public enum ContactsOpsImplType {
* Stores the type of the ContactsOpsImpl (i.e., SIMPLE, ASYNC, or
* LOADER_MANAGER).
*/
private ContactsOpsImplType mImplType =
ContactsOpsImplType.SIMPLE;
private ContactsOpsImplType mImplType = null;

/**
* The root of the Implementor hierarchy.
Expand All @@ -51,18 +50,7 @@ public enum ContactsOpsImplType {
* class to work properly.
*/
public ContactsOps() {
// Select the appropriate type of ContactsOpsImpl.
switch(mImplType) {
case SIMPLE:
mImpl = new ContactsOpsImplSimple();
break;
case ASYNC:
mImpl = new ContactsOpsImplAsync();
break;
case LOADER_MANAGER:
mImpl = new ContactsOpsImplLoaderManager();
break;
}
setContactsOpsImplType(ContactsOpsImplType.SIMPLE);
}

/**
Expand Down Expand Up @@ -114,6 +102,20 @@ public void deleteContacts() {
* LOADER_MANAGER).
*/
public void setContactsOpsImplType(ContactsOpsImplType implType) {
mImplType = implType;
// Set and construct the appropriate type of ContactsOpsImpl.
if (implType != mImplType) {
mImplType = implType;
switch(mImplType) {
case SIMPLE:
mImpl = new ContactsOpsImplSimple();
break;
case ASYNC:
mImpl = new ContactsOpsImplAsync();
break;
case LOADER_MANAGER:
mImpl = new ContactsOpsImplLoaderManager();
break;
}
}
}
}

0 comments on commit a00785f

Please sign in to comment.