Skip to content

Commit 1afeb2c

Browse files
author
Anirudh S
committed
Drafts: Single file + support for replies
- Using a single file for saving draft details - Ticket replies have draft-support too - Attachment images are also saved to draft
1 parent 4901012 commit 1afeb2c

File tree

9 files changed

+201
-111
lines changed

9 files changed

+201
-111
lines changed

helpstack/res/layout/hs_fragment_issue_detail.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
android:layout_alignParentRight="true"
5252
android:background="@color/hs_transparent_color"
5353
android:contentDescription="@string/hs_sendbutton_title"
54-
android:src="@drawable/hs_send_img"
54+
android:src="@drawable/hs_send_now_img"
5555
/>
5656

5757
</RelativeLayout>

helpstack/res/menu/hs_issue_menu.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
tools:context="com.tenmiles.helpstack.NewIssueActivity" >
55

6+
<item
7+
android:id="@+id/clearItem"
8+
android:title="@string/hs_clear"
9+
android:visible="true"
10+
/>
11+
612
<item
713
android:id="@+id/doneItem"
814
android:title="@string/hs_done"

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
115115
mExpandableListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
116116

117117
gearSource = new HSSource(getActivity());
118+
119+
this.replyEditTextView.setText(gearSource.getDraftReplyMessage());
120+
121+
if (gearSource.getDraftReplyAttachments() != null && gearSource.getDraftReplyAttachments().length > 0) {
122+
this.selectedAttachment = gearSource.getDraftReplyAttachments()[0];
123+
resetAttachmentImage();
124+
}
118125

119126
mAdapter.setOnChildItemClickListener(listChildClickListener);
120127

@@ -155,7 +162,21 @@ public void onSaveInstanceState(Bundle outState) {
155162
outState.putSerializable("selectedAttachment", selectedAttachment);
156163
outState.putSerializable("replyEditTextView", replyEditTextView.getText().toString());
157164
}
158-
165+
166+
@Override
167+
public void onPause() {
168+
super.onPause();
169+
170+
HSAttachment[] attachmentArray = null;
171+
172+
if (selectedAttachment != null) {
173+
attachmentArray = new HSAttachment[1];
174+
attachmentArray[0] = selectedAttachment;
175+
}
176+
177+
gearSource.saveReplyDetailsInDraft(replyEditTextView.getText().toString(), attachmentArray);
178+
}
179+
159180
@Override
160181
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
161182

@@ -302,6 +323,8 @@ public void onClick(View v) {
302323

303324
@Override
304325
public void onSuccess(Object successObject) {
326+
clearFormData();
327+
305328
sendButton.setEnabled(true);
306329
sendButton.setAlpha((float)1.0);
307330
HSTicketUpdate update = (HSTicketUpdate) successObject;
@@ -336,8 +359,13 @@ public void onErrorResponse(VolleyError error) {
336359
});
337360
}
338361
};
339-
340-
private void expandAll() {
362+
363+
private void clearFormData() {
364+
replyEditTextView.setText("");
365+
gearSource.clearReplyDraft();
366+
}
367+
368+
private void expandAll() {
341369
int count = mAdapter.getGroupCount();
342370
for (int i = 0; i < count; i++) {
343371
mExpandableListView.expandGroup(i);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
124124
this.subjectField.setText(gearSource.getDraftSubject());
125125
this.messageField.setText(gearSource.getDraftMessage());
126126

127+
if (gearSource.getDraftAttachments() != null && gearSource.getDraftAttachments().length > 0) {
128+
this.selectedAttachment = gearSource.getDraftAttachments()[0];
129+
resetAttachmentImage();
130+
}
131+
127132
if (!HSHelpStack.getInstance(getActivity()).getShowCredits()) {
128133
rootView.findViewById(R.id.footerTextLabel).setVisibility(View.GONE);
129134
rootView.findViewById(R.id.footerDivider).setVisibility(View.GONE);
@@ -174,6 +179,9 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
174179
// Inflate the menu; this adds items to the action bar if it is present.
175180
inflater.inflate(R.menu.hs_issue_menu, menu);
176181

182+
MenuItem clearMenu = menu.findItem(R.id.clearItem);
183+
MenuItemCompat.setShowAsAction(clearMenu, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
184+
177185
MenuItem doneMenu = menu.findItem(R.id.doneItem);
178186

179187
if (gearSource.isNewUser()) {
@@ -234,6 +242,9 @@ public void onErrorResponse(VolleyError error) {
234242

235243
return true;
236244
}
245+
else if(id == R.id.clearItem) {
246+
clearFormData();
247+
}
237248

238249
return super.onOptionsItemSelected(item);
239250
}
@@ -263,10 +274,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
263274
selectedAttachment = HSAttachment.createAttachment(selectedImage.toString(), display_name, mime_type);
264275

265276
resetAttachmentImage();
277+
break;
266278
}
267279
case REQUEST_CODE_NEW_TICKET:
268280
if (resultCode == HSActivityManager.resultCode_sucess) {
269281
HSActivityManager.sendSuccessSignal(getActivity(), intent);
282+
break;
270283
}
271284
}
272285
}
@@ -332,6 +345,10 @@ private void resetAttachmentImage() {
332345
private void clearFormData() {
333346
this.subjectField.setText("");
334347
this.messageField.setText("");
348+
this.selectedAttachment = null;
349+
350+
resetAttachmentImage();
351+
335352
gearSource.clearTicketDraft();
336353
}
337354

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
9393

9494
gearSource = new HSSource(getActivity());
9595

96-
HSUser user = gearSource.getDraftUser();
97-
if (user != null) {
98-
this.firstNameField.setText(user.getFirstName());
99-
this.lastNameField.setText(user.getLastName());
100-
this.emailField.setText(user.getEmail());
96+
HSUser draftUser = gearSource.getDraftUser();
97+
if (draftUser != null) {
98+
this.firstNameField.setText(draftUser.getFirstName());
99+
this.lastNameField.setText(draftUser.getLastName());
100+
this.emailField.setText(draftUser.getEmail());
101101
}
102102

103103
return rootView;

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

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@
3939
import com.tenmiles.helpstack.activities.HSActivityManager;
4040
import com.tenmiles.helpstack.fragments.HSFragmentParent;
4141
import com.tenmiles.helpstack.model.HSAttachment;
42-
import com.tenmiles.helpstack.model.HSTicketDraft;
42+
import com.tenmiles.helpstack.model.HSDraft;
4343
import com.tenmiles.helpstack.model.HSCachedTicket;
4444
import com.tenmiles.helpstack.model.HSCachedUser;
4545
import com.tenmiles.helpstack.model.HSKBItem;
4646
import com.tenmiles.helpstack.model.HSTicket;
4747
import com.tenmiles.helpstack.model.HSTicketUpdate;
4848
import com.tenmiles.helpstack.model.HSUploadAttachment;
4949
import com.tenmiles.helpstack.model.HSUser;
50-
import com.tenmiles.helpstack.model.HSUserDraft;
5150

5251
import org.xmlpull.v1.XmlPullParserException;
5352

@@ -67,8 +66,7 @@ public class HSSource {
6766
private static final String HELPSTACK_DIRECTORY = "helpstack";
6867
private static final String HELPSTACK_TICKETS_FILE_NAME = "tickets";
6968
private static final String HELPSTACK_TICKETS_USER_DATA = "user_credential";
70-
private static final String HELPSTACK_TICKET_DRAFT = "ticket_draft";
71-
private static final String HELPSTACK_USER_DRAFT = "user_draft";
69+
private static final String HELPSTACK_DRAFT = "draft";
7270

7371
private HSGear gear;
7472
private Context mContext;
@@ -77,9 +75,7 @@ public class HSSource {
7775
private HSCachedTicket cachedTicket;
7876
private HSCachedUser cachedUser;
7977

80-
private HSTicketDraft ticketDraft;
81-
private HSUserDraft userDraft;
82-
private HSUser draftUser;
78+
private HSDraft draftObject;
8379

8480
public HSSource(Context context) {
8581
this.mContext = context;
@@ -89,13 +85,10 @@ public HSSource(Context context) {
8985
cachedTicket = new HSCachedTicket();
9086
cachedUser = new HSCachedUser();
9187

92-
draftUser = new HSUser();
93-
9488
// read the ticket data from cache and maintain here
9589
doReadTicketsFromCache();
9690
doReadUserFromCache();
97-
doReadTicketDraftFromCache();
98-
doReadUserDraftFromCache();
91+
doReadDraftFromCache();
9992
}
10093

10194
public void requestKBArticle(String cancelTag, HSKBItem section, OnFetchedArraySuccessListener success, ErrorListener errorListener ) {
@@ -215,29 +208,43 @@ public HSUser getUser() {
215208
}
216209

217210
public String getDraftSubject() {
218-
if(ticketDraft != null) {
219-
return ticketDraft.getSubject();
211+
if(draftObject != null) {
212+
return draftObject.getSubject();
220213
}
221214
return null;
222215
}
223216

224217
public String getDraftMessage() {
225-
if(ticketDraft != null) {
226-
return ticketDraft.getMessage();
218+
if(draftObject != null) {
219+
return draftObject.getMessage();
220+
}
221+
return null;
222+
}
223+
224+
public HSUser getDraftUser() {
225+
if(draftObject != null) {
226+
return draftObject.getDraftUser();
227227
}
228228
return null;
229229
}
230230

231231
public HSAttachment[] getDraftAttachments() {
232-
if(ticketDraft != null) {
233-
return ticketDraft.getAttachments();
232+
if(draftObject != null) {
233+
return draftObject.getAttachments();
234234
}
235235
return null;
236236
}
237237

238-
public HSUser getDraftUser() {
239-
if(userDraft != null) {
240-
return userDraft.getUser();
238+
public String getDraftReplyMessage() {
239+
if(draftObject != null) {
240+
return draftObject.getDraftReplyMessage();
241+
}
242+
return null;
243+
}
244+
245+
public HSAttachment[] getDraftReplyAttachments() {
246+
if(draftObject != null) {
247+
return draftObject.getDraftReplyAttachments();
241248
}
242249
return null;
243250
}
@@ -250,6 +257,10 @@ public void saveUserDetailsInDraft(HSUser user) {
250257
doSaveUserDraftForGearInCache(user);
251258
}
252259

260+
public void saveReplyDetailsInDraft(String message, HSAttachment[] attachmentsArray) {
261+
doSaveReplyDraftForGearInCache(message, attachmentsArray);
262+
}
263+
253264
public boolean haveImplementedTicketFetching() {
254265
return gear.haveImplementedTicketFetching();
255266
}
@@ -432,49 +443,51 @@ protected void doReadUserFromCache() {
432443
}
433444
}
434445

435-
protected void doReadTicketDraftFromCache() {
436-
File draftFile = new File(getProjectDirectory(), HELPSTACK_TICKET_DRAFT);
446+
protected void doReadDraftFromCache() {
447+
File draftFile = new File(getProjectDirectory(), HELPSTACK_DRAFT);
437448

438449
String json = readJsonFromFile(draftFile);
439450

440451
if (json != null) {
441452
Gson gson = new Gson();
442-
ticketDraft = gson.fromJson(json, HSTicketDraft.class);
453+
draftObject = gson.fromJson(json, HSDraft.class);
443454
}
444455
}
445456

446-
protected void doReadUserDraftFromCache() {
447-
File draftFile = new File(getProjectDirectory(), HELPSTACK_USER_DRAFT);
457+
protected void doSaveTicketDraftForGearInCache(String subject, String message, HSAttachment[] attachmentsArray) {
458+
if(draftObject == null) {
459+
draftObject = new HSDraft();
460+
}
448461

449-
String json = readJsonFromFile(draftFile);
462+
draftObject.setDraftSubject(subject);
463+
draftObject.setDraftMessage(message);
464+
draftObject.setDraftAttachments(attachmentsArray);
450465

451-
if (json != null) {
452-
Gson gson = new Gson();
453-
userDraft = gson.fromJson(json, HSUserDraft.class);
454-
}
466+
writeDraftIntoFile();
455467
}
456468

457-
protected void doSaveTicketDraftForGearInCache(String subject, String message, HSAttachment[] attachmentsArray) {
458-
Gson gson = new Gson();
469+
protected void doSaveUserDraftForGearInCache(HSUser user) {
470+
draftObject.setDraftUSer(user);
471+
writeDraftIntoFile();
472+
}
459473

460-
HSTicketDraft draft = new HSTicketDraft(subject, message, attachmentsArray);
461-
String draftJson = gson.toJson(draft);
462-
File draftFile = new File(getProjectDirectory(), HELPSTACK_TICKET_DRAFT);
474+
protected void doSaveReplyDraftForGearInCache(String message, HSAttachment[] attachmentsArray) {
475+
draftObject.setDraftReplyMessage(message);
476+
draftObject.setDraftReplyAttachments(attachmentsArray);
463477

464-
writeJsonIntoFile(draftFile, draftJson);
478+
writeDraftIntoFile();
465479
}
466480

467-
protected void doSaveUserDraftForGearInCache(HSUser user) {
468-
Gson gson = new Gson();
469481

470-
HSUserDraft draft = new HSUserDraft(user);
471-
String draftJson = gson.toJson(draft);
472-
File draftFile = new File(getProjectDirectory(), HELPSTACK_USER_DRAFT);
482+
private void writeDraftIntoFile() {
483+
Gson gson = new Gson();
484+
String draftJson = gson.toJson(draftObject);
485+
File draftFile = new File(getProjectDirectory(), HELPSTACK_DRAFT);
473486

474487
writeJsonIntoFile(draftFile, draftJson);
475488
}
476-
477-
protected File getProjectDirectory() {
489+
490+
protected File getProjectDirectory() {
478491

479492
File projDir = new File(mContext.getFilesDir(), HELPSTACK_DIRECTORY);
480493
if (!projDir.exists())
@@ -487,6 +500,10 @@ public void clearTicketDraft() {
487500
saveTicketDetailsInDraft("", "", null);
488501
}
489502

503+
public void clearReplyDraft() {
504+
saveReplyDetailsInDraft("", null);
505+
}
506+
490507
private class NewTicketSuccessWrapper implements OnNewTicketFetchedSuccessListener
491508
{
492509

0 commit comments

Comments
 (0)