Skip to content

Commit 0c45717

Browse files
committed
Implemented "My Drafts" #113
1 parent 80f5238 commit 0c45717

10 files changed

Lines changed: 642 additions & 1 deletion

File tree

source-code/app/src/main/java/org/buildmlearn/toolkit/ToolkitApplication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public String getSavedDir() {
6767
return dir + Constants.SAVED_DIR;
6868
}
6969

70+
/**
71+
* @brief Returns folder path for saved projects
72+
* @return Folder path
73+
*/
74+
public String getDraftDir() {
75+
return dir + Constants.DRAFT_DIR;
76+
}
77+
78+
7079
/**
7180
* @brief Returns folder path for unzipped apks
7281
* @return Folder path

source-code/app/src/main/java/org/buildmlearn/toolkit/activity/TemplateEditor.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,13 +537,15 @@ protected String saveProject() {
537537
if (oldFileName != null) {
538538
File tempFile = new File(oldFileName);
539539
tempFile.delete();
540+
oldFileName = null;
540541
}
541542
String saveFileName = title + " by " + author + ".buildmlearn";
542543
saveFileName = saveFileName.replaceAll(" ", "-");
543544

544545

545546
FileUtils.saveXmlFile(toolkit.getSavedDir(), saveFileName, doc);
546547

548+
oldFileName = toolkit.getSavedDir() + saveFileName;
547549

548550
return toolkit.getSavedDir() + saveFileName;
549551
} catch (ParserConfigurationException e) {
@@ -553,6 +555,96 @@ protected String saveProject() {
553555
return null;
554556
}
555557

558+
@Override
559+
public void onBackPressed() {
560+
super.onBackPressed();
561+
if (saveDraft()!=null)
562+
Toast.makeText(getApplicationContext(),"Saved in Draft!",Toast.LENGTH_SHORT).show();
563+
564+
}
565+
566+
/**
567+
* Converts the current TemplateInterface object into a xml file. Xml file is saved in DRAFT
568+
* Directory (defined in constants). File name is of the format: draft<0-X>.buildmlearn
569+
*
570+
* @return Absolute path of the saved file. Null if there is some error.
571+
* @brief Saves the current project into a .buildmlearn file.
572+
*/
573+
protected String saveDraft() {
574+
575+
String author = ((EditText) findViewById(R.id.author_name)).getText().toString();
576+
String title = ((EditText) findViewById(R.id.template_title)).getText().toString();
577+
578+
579+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
580+
DocumentBuilder docBuilder;
581+
try {
582+
583+
docBuilder = docFactory.newDocumentBuilder();
584+
Document doc = docBuilder.newDocument();
585+
Element rootElement = doc.createElement("buildmlearn_application");
586+
Attr attr = doc.createAttribute("type");
587+
attr.setValue(getResources().getString(template.getType()));
588+
rootElement.setAttributeNode(attr);
589+
590+
Element authorElement = doc.createElement("author");
591+
rootElement.appendChild(authorElement);
592+
593+
Element nameElement = doc.createElement("name");
594+
nameElement.appendChild(doc.createTextNode(author));
595+
596+
authorElement.appendChild(nameElement);
597+
598+
Element titleElement = doc.createElement("title");
599+
titleElement.appendChild(doc.createTextNode(title));
600+
rootElement.appendChild(titleElement);
601+
602+
doc.appendChild(rootElement);
603+
Element dataElement = doc.createElement("data");
604+
rootElement.appendChild(dataElement);
605+
if (selectedTemplate.getItems(doc).size() == 0) {
606+
Toast.makeText(this, "Unable to perform action: No Data", Toast.LENGTH_SHORT).show();
607+
return null;
608+
}
609+
for (Element item : selectedTemplate.getItems(doc)) {
610+
dataElement.appendChild(item);
611+
}
612+
613+
int draftFileIndex = 0;
614+
File draftDir = new File(toolkit.getDraftDir());
615+
String probableFileName = "draft"+draftFileIndex+ ".buildmlearn";
616+
File probableFile = new File(draftDir, probableFileName);
617+
618+
while(probableFile.exists()) {
619+
draftFileIndex++;
620+
probableFileName = "draft"+draftFileIndex+ ".buildmlearn";
621+
probableFile = new File(draftDir, probableFileName);
622+
}
623+
624+
//Create temporary File, if that file content matches with OldContent
625+
// Then Don't make Draft
626+
File tempFile = new File(toolkit.getDraftDir(), ".temp");
627+
File oldFile = null;
628+
if (oldFileName!=null)
629+
oldFile = new File(oldFileName);
630+
631+
FileUtils.saveXmlFile(toolkit.getDraftDir(), ".temp", doc);
632+
if (oldFile == null || !FileUtils.equalContent(tempFile,oldFile)) {
633+
tempFile.renameTo(probableFile);
634+
return toolkit.getDraftDir() + probableFileName;
635+
} else {
636+
File newFile = new File(toolkit.getDraftDir(), ".temp");
637+
newFile.delete();
638+
}
639+
return null;
640+
641+
} catch (ParserConfigurationException e) {
642+
e.printStackTrace();
643+
}
644+
return null;
645+
}
646+
647+
556648
/**
557649
* @brief Start the simulator activity
558650
* <p/>
@@ -608,6 +700,11 @@ protected void parseSavedFile(String path) {
608700
Object templateObject = templateClass.newInstance();
609701
selectedTemplate = (TemplateInterface) templateObject;
610702
populateListView(selectedTemplate.loadProjectTemplateEditor(this, items));
703+
File draftDir = new File(toolkit.getDraftDir());
704+
if (fXmlFile.getParentFile().compareTo(draftDir) ==0 ) {
705+
//If Draft File
706+
fXmlFile.delete();
707+
}
611708
setUpActionBar();
612709
updateHeaderDetails(name, title);
613710

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.buildmlearn.toolkit.adapter;
2+
3+
import android.content.Context;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.BaseAdapter;
8+
9+
import org.buildmlearn.toolkit.R;
10+
import org.buildmlearn.toolkit.model.SavedProject;
11+
import org.buildmlearn.toolkit.views.TextViewPlus;
12+
13+
import java.util.ArrayList;
14+
import java.util.Locale;
15+
16+
/**
17+
* Created by scopeinfinity on 10/3/16.
18+
*/
19+
public class DraftProjectAdapter extends BaseAdapter {
20+
21+
private Context mContext;
22+
private ArrayList<SavedProject> data;
23+
24+
public DraftProjectAdapter(Context mContext, ArrayList<SavedProject> data) {
25+
this.mContext = mContext;
26+
this.data = data;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
@Override
33+
public int getCount() {
34+
return data.size();
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
@Override
41+
public SavedProject getItem(int i) {
42+
return data.get(i);
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
@Override
49+
public long getItemId(int i) {
50+
return i;
51+
}
52+
53+
/**
54+
* {@inheritDoc}
55+
*/
56+
@Override
57+
public View getView(int position, View convertView, ViewGroup parent) {
58+
LayoutInflater mInflater;
59+
mInflater = LayoutInflater.from(mContext);
60+
DraftHolder holder;
61+
if (convertView == null) {
62+
convertView = mInflater.inflate(R.layout.item_load_project, parent, false);
63+
holder = new DraftHolder();
64+
holder.draftTitle = (TextViewPlus) convertView.findViewById(R.id.title);
65+
holder.draftIcon = (TextViewPlus) convertView.findViewById(R.id.icon);
66+
holder.draftSubtitle = (TextViewPlus) convertView.findViewById(R.id.subtitle);
67+
} else {
68+
holder = (DraftHolder) convertView.getTag();
69+
}
70+
71+
SavedProject projectData = getItem(position);
72+
holder.draftSubtitle.setText("Last Modified: " + projectData.getTime() );
73+
holder.draftTitle.setText("Drafted on "+projectData.getDate());
74+
holder.draftIcon.setText("D");
75+
convertView.setTag(holder);
76+
return convertView;
77+
}
78+
79+
public class DraftHolder {
80+
TextViewPlus draftTitle;
81+
TextViewPlus draftIcon;
82+
TextViewPlus draftSubtitle;
83+
}
84+
}

source-code/app/src/main/java/org/buildmlearn/toolkit/constant/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public final static String UNZIP = BUILD_M_LEARN_PATH + "unzipped/";
1313
public final static String APK_DIR = BUILD_M_LEARN_PATH + "apk/";
1414
public final static String SAVED_DIR = BUILD_M_LEARN_PATH + "saved/";
15+
public final static String DRAFT_DIR = BUILD_M_LEARN_PATH + "draft/";
1516
public final static String TEMPLATE_ID = "TEMPLATE_ID";
1617
public final static String TEMPLATE_OBJECT = "TEMPLATE_OBJECT";
1718
public final static String SIMULATOR_FILE_PATH = "SIMULATOR_FILE_PATH";

0 commit comments

Comments
 (0)