Skip to content

Commit 6350a7a

Browse files
committed
Merge branch 'draft'
2 parents 94879eb + 0c45717 commit 6350a7a

14 files changed

Lines changed: 645 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
@@ -72,6 +72,15 @@ public String getSavedDir() {
7272
return dir + Constants.SAVED_DIR;
7373
}
7474

75+
/**
76+
* @brief Returns folder path for saved projects
77+
* @return Folder path
78+
*/
79+
public String getDraftDir() {
80+
return dir + Constants.DRAFT_DIR;
81+
}
82+
83+
7584
/**
7685
* @brief Returns folder path for unzipped apks
7786
* @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
@@ -646,13 +646,15 @@ private String saveProject() {
646646
if (oldFileName != null) {
647647
File tempFile = new File(oldFileName);
648648
tempFile.delete();
649+
oldFileName = null;
649650
}
650651
String saveFileName = title + " by " + author + ".buildmlearn";
651652
saveFileName = saveFileName.replaceAll(" ", "-");
652653

653654

654655
FileUtils.saveXmlFile(toolkit.getSavedDir(), saveFileName, doc);
655656

657+
oldFileName = toolkit.getSavedDir() + saveFileName;
656658

657659
return toolkit.getSavedDir() + saveFileName;
658660
} catch (ParserConfigurationException e) {
@@ -662,6 +664,96 @@ private String saveProject() {
662664
return null;
663665
}
664666

667+
@Override
668+
public void onBackPressed() {
669+
super.onBackPressed();
670+
if (saveDraft()!=null)
671+
Toast.makeText(getApplicationContext(),"Saved in Draft!",Toast.LENGTH_SHORT).show();
672+
673+
}
674+
675+
/**
676+
* Converts the current TemplateInterface object into a xml file. Xml file is saved in DRAFT
677+
* Directory (defined in constants). File name is of the format: draft<0-X>.buildmlearn
678+
*
679+
* @return Absolute path of the saved file. Null if there is some error.
680+
* @brief Saves the current project into a .buildmlearn file.
681+
*/
682+
protected String saveDraft() {
683+
684+
String author = ((EditText) findViewById(R.id.author_name)).getText().toString();
685+
String title = ((EditText) findViewById(R.id.template_title)).getText().toString();
686+
687+
688+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
689+
DocumentBuilder docBuilder;
690+
try {
691+
692+
docBuilder = docFactory.newDocumentBuilder();
693+
Document doc = docBuilder.newDocument();
694+
Element rootElement = doc.createElement("buildmlearn_application");
695+
Attr attr = doc.createAttribute("type");
696+
attr.setValue(getResources().getString(template.getType()));
697+
rootElement.setAttributeNode(attr);
698+
699+
Element authorElement = doc.createElement("author");
700+
rootElement.appendChild(authorElement);
701+
702+
Element nameElement = doc.createElement("name");
703+
nameElement.appendChild(doc.createTextNode(author));
704+
705+
authorElement.appendChild(nameElement);
706+
707+
Element titleElement = doc.createElement("title");
708+
titleElement.appendChild(doc.createTextNode(title));
709+
rootElement.appendChild(titleElement);
710+
711+
doc.appendChild(rootElement);
712+
Element dataElement = doc.createElement("data");
713+
rootElement.appendChild(dataElement);
714+
if (selectedTemplate.getItems(doc).size() == 0) {
715+
Toast.makeText(this, "Unable to perform action: No Data", Toast.LENGTH_SHORT).show();
716+
return null;
717+
}
718+
for (Element item : selectedTemplate.getItems(doc)) {
719+
dataElement.appendChild(item);
720+
}
721+
722+
int draftFileIndex = 0;
723+
File draftDir = new File(toolkit.getDraftDir());
724+
String probableFileName = "draft"+draftFileIndex+ ".buildmlearn";
725+
File probableFile = new File(draftDir, probableFileName);
726+
727+
while(probableFile.exists()) {
728+
draftFileIndex++;
729+
probableFileName = "draft"+draftFileIndex+ ".buildmlearn";
730+
probableFile = new File(draftDir, probableFileName);
731+
}
732+
733+
//Create temporary File, if that file content matches with OldContent
734+
// Then Don't make Draft
735+
File tempFile = new File(toolkit.getDraftDir(), ".temp");
736+
File oldFile = null;
737+
if (oldFileName!=null)
738+
oldFile = new File(oldFileName);
739+
740+
FileUtils.saveXmlFile(toolkit.getDraftDir(), ".temp", doc);
741+
if (oldFile == null || !FileUtils.equalContent(tempFile,oldFile)) {
742+
tempFile.renameTo(probableFile);
743+
return toolkit.getDraftDir() + probableFileName;
744+
} else {
745+
File newFile = new File(toolkit.getDraftDir(), ".temp");
746+
newFile.delete();
747+
}
748+
return null;
749+
750+
} catch (ParserConfigurationException e) {
751+
e.printStackTrace();
752+
}
753+
return null;
754+
}
755+
756+
665757
/**
666758
* @brief Start the simulator activity
667759
* <p/>
@@ -720,6 +812,11 @@ private void parseSavedFile(String path) {
720812
if (templateId == 5) {
721813
populateMetaView(selectedTemplate.loadProjectMetaEditor(this, doc));
722814
}
815+
File draftDir = new File(toolkit.getDraftDir());
816+
if (fXmlFile.getParentFile().compareTo(draftDir) ==0 ) {
817+
//If Draft File
818+
fXmlFile.delete();
819+
}
723820
setUpActionBar();
724821
updateHeaderDetails(name, title);
725822

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

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)