Skip to content

Commit 7a9c26e

Browse files
committed
Improved logic of app to disable unnecessary object creation
1 parent 243072e commit 7a9c26e

File tree

7 files changed

+63
-46
lines changed

7 files changed

+63
-46
lines changed

helpstack/src/com/tenmiles/helpstack/activities/HSActivityManager.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import android.app.Activity;
2626
import android.content.Context;
2727
import android.content.Intent;
28-
import android.os.Bundle;
2928

3029
import com.google.gson.Gson;
3130
import com.tenmiles.helpstack.fragments.HSFragmentParent;
32-
import com.tenmiles.helpstack.fragments.NewIssueFragment;
3331
import com.tenmiles.helpstack.model.HSAttachment;
3432
import com.tenmiles.helpstack.model.HSKBItem;
3533
import com.tenmiles.helpstack.model.HSTicket;

helpstack/src/com/tenmiles/helpstack/fragments/HomeFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
111111
setHasOptionsMenu(true);
112112

113113
// Initialize gear
114-
gearSource = new HSSource(getActivity());
114+
gearSource = HSSource.getInstance(getActivity());
115115

116116
// handle orientation
117117
if (savedInstanceState == null) {

helpstack/src/com/tenmiles/helpstack/fragments/IssueDetailFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
114114
mExpandableListView.setAdapter(mAdapter);
115115
mExpandableListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
116116

117-
gearSource = new HSSource(getActivity());
117+
gearSource = HSSource.getInstance(getActivity());
118118

119119
this.replyEditTextView.setText(gearSource.getDraftReplyMessage());
120120

helpstack/src/com/tenmiles/helpstack/fragments/NewIssueFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
119119
}
120120
}
121121

122-
gearSource = new HSSource(getActivity());
122+
gearSource = HSSource.getInstance(getActivity());
123123

124124
this.subjectField.setText(gearSource.getDraftSubject());
125125
this.messageField.setText(gearSource.getDraftMessage());

helpstack/src/com/tenmiles/helpstack/fragments/NewUserFragment.java

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
8282
this.lastNameField = (EditText) rootView.findViewById(R.id.lastname);
8383
this.emailField = (EditText) rootView.findViewById(R.id.email);
8484

85-
Bundle args = savedInstanceState;
86-
if (args == null) {
87-
args = getArguments();
88-
}
8985

90-
if (args != null) {
91-
subject = args.getString(EXTRAS_SUBJECT, null);
92-
message = args.getString(EXTRAS_MESSAGE, null);
93-
if (args.containsKey(EXTRAS_ATTACHMENT)) {
94-
String json = args.getString(EXTRAS_ATTACHMENT);
95-
Gson gson = new Gson();
96-
attachmentArray = gson.fromJson(json, HSAttachment[].class);
97-
}
98-
99-
String first_name = args.getString(EXTRAS_FIRST_NAME, null);
100-
if (first_name != null) firstNameField.setText(first_name);
101-
String last_name = args.getString(EXTRAS_LAST_NAME, null);
102-
if (last_name != null) lastNameField.setText(last_name);
103-
String email = args.getString(EXTRAS_EMAIL, null);
104-
if (email != null) emailField.setText(email);
105-
}
106-
107-
gearSource = new HSSource(getActivity());
108-
109-
HSUser draftUser = gearSource.getDraftUser();
110-
if (draftUser != null) {
111-
this.firstNameField.setText(draftUser.getFirstName());
112-
this.lastNameField.setText(draftUser.getLastName());
113-
this.emailField.setText(draftUser.getEmail());
114-
}
11586

11687
return rootView;
11788
}
@@ -128,8 +99,6 @@ public void onSaveInstanceState(Bundle outState) {
12899
Gson gson = new Gson();
129100
outState.putSerializable(EXTRAS_ATTACHMENT, gson.toJson(attachmentArray));
130101
}
131-
132-
133102
}
134103

135104
@Override
@@ -148,14 +117,36 @@ public void onPause() {
148117
public void onActivityCreated(Bundle savedInstanceState) {
149118
super.onActivityCreated(savedInstanceState);
150119

151-
if (savedInstanceState == null) {
152-
153-
}
154-
else {
155-
this.firstNameField.setText(savedInstanceState.getString("first_name"));
156-
this.lastNameField.setText(savedInstanceState.getString("last_name"));
157-
this.emailField.setText(savedInstanceState.getString("email"));
158-
}
120+
Bundle args = savedInstanceState;
121+
if (args == null) {
122+
args = getArguments();
123+
}
124+
125+
if (args != null) {
126+
subject = args.getString(EXTRAS_SUBJECT, null);
127+
message = args.getString(EXTRAS_MESSAGE, null);
128+
if (args.containsKey(EXTRAS_ATTACHMENT)) {
129+
String json = args.getString(EXTRAS_ATTACHMENT);
130+
Gson gson = new Gson();
131+
attachmentArray = gson.fromJson(json, HSAttachment[].class);
132+
}
133+
134+
String first_name = args.getString(EXTRAS_FIRST_NAME, null);
135+
if (first_name != null) firstNameField.setText(first_name);
136+
String last_name = args.getString(EXTRAS_LAST_NAME, null);
137+
if (last_name != null) lastNameField.setText(last_name);
138+
String email = args.getString(EXTRAS_EMAIL, null);
139+
if (email != null) emailField.setText(email);
140+
}
141+
142+
gearSource = HSSource.getInstance(getActivity());
143+
144+
HSUser draftUser = gearSource.getDraftUser();
145+
if (draftUser != null) {
146+
this.firstNameField.setText(draftUser.getFirstName());
147+
this.lastNameField.setText(draftUser.getLastName());
148+
this.emailField.setText(draftUser.getEmail());
149+
}
159150
}
160151

161152
@Override

helpstack/src/com/tenmiles/helpstack/fragments/SectionFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
9595
mSearchFragment.setOnReportAnIssueClickListener(reportAnIssueLisener);
9696
setHasOptionsMenu(true);
9797

98-
gearSource = new HSSource (getActivity());
98+
gearSource = HSSource.getInstance(getActivity());
9999

100100
if (savedInstanceState == null) {
101101
initializeView();

helpstack/src/com/tenmiles/helpstack/logic/HSSource.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ public class HSSource {
6868
private static final String HELPSTACK_TICKETS_USER_DATA = "user_credential";
6969
private static final String HELPSTACK_DRAFT = "draft";
7070

71+
private static HSSource singletonInstance = null;
72+
73+
/**
74+
*
75+
* @param context
76+
* @return singleton instance of this class.
77+
*/
78+
public static HSSource getInstance(Context context) {
79+
if (singletonInstance == null) {
80+
synchronized (HSSource.class) { // 1
81+
if (singletonInstance == null) // 2
82+
{
83+
Log.d(TAG, "New Instance");
84+
singletonInstance = new HSSource(
85+
context.getApplicationContext()); // 3
86+
}
87+
88+
// Can be called even before a gear is set
89+
if (singletonInstance.gear == null) {
90+
singletonInstance.setGear(HSHelpStack.getInstance(context).getGear());
91+
}
92+
}
93+
}
94+
return singletonInstance;
95+
}
96+
7197
private HSGear gear;
7298
private Context mContext;
7399
private RequestQueue mRequestQueue;
@@ -77,7 +103,7 @@ public class HSSource {
77103

78104
private HSDraft draftObject;
79105

80-
public HSSource(Context context) {
106+
private HSSource(Context context) {
81107
this.mContext = context;
82108
setGear(HSHelpStack.getInstance(context).getGear());
83109
mRequestQueue = HSHelpStack.getInstance(context).getRequestQueue();
@@ -90,6 +116,8 @@ public HSSource(Context context) {
90116
doReadUserFromCache();
91117
doReadDraftFromCache();
92118
}
119+
120+
93121

94122
public void requestKBArticle(String cancelTag, HSKBItem section, OnFetchedArraySuccessListener success, ErrorListener errorListener ) {
95123

0 commit comments

Comments
 (0)