Skip to content

Commit 79e79e4

Browse files
committed
Bug for same named template addition
1 parent 6688dda commit 79e79e4

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

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

+53-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package org.buildmlearn.toolkit.activity;
2-
32
import android.Manifest;
43
import android.app.ProgressDialog;
54
import android.content.Context;
@@ -15,7 +14,6 @@
1514
import android.os.Handler;
1615
import android.os.Message;
1716
import android.preference.PreferenceManager;
18-
import android.support.design.widget.Snackbar;
1917
import android.support.v4.app.ActivityCompat;
2018
import android.support.v4.content.ContextCompat;
2119
import android.support.v7.app.ActionBar;
@@ -87,10 +85,12 @@ public void handleMessage(Message message) {
8785
private int selectedPosition = -1;
8886
private boolean showTemplateSelectedMenu;
8987
private View selectedView;
88+
private EditText titleEditText;
9089
private ToolkitApplication toolkit;
9190
private String oldFileName;
9291
private ProgressDialog mApkGenerationDialog;
9392

93+
9494
/**
9595
* {@inheritDoc}
9696
*/
@@ -306,7 +306,8 @@ private void setUpTemplateEditor() {
306306
setUpActionBar();
307307
} catch (IllegalAccessException e) {
308308
e.printStackTrace();
309-
} catch (InstantiationException e) {
309+
}
310+
catch (InstantiationException e) {
310311
e.printStackTrace();
311312
}
312313
}
@@ -366,19 +367,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
366367

367368
switch (id) {
368369
case R.id.action_delete:
369-
final int restorePosition = selectedPosition;
370-
final Object object = selectedTemplate.deleteItem(TemplateEditor.this, selectedPosition);
371-
selectedPosition = -1;
372-
restoreSelectedView();
373-
Snackbar.make(findViewById(R.id.relative_layout),
374-
R.string.snackbar_deleted_message,Snackbar.LENGTH_LONG)
375-
.setAction(R.string.snackbar_undo, new View.OnClickListener() {
370+
371+
final AlertDialog dialog = new AlertDialog.Builder(this)
372+
.setTitle(R.string.dialog_delete_title)
373+
.setMessage(R.string.dialog_delete_msg)
374+
.setPositiveButton(R.string.dialog_yes, null)
375+
.setNegativeButton(R.string.dialog_no, null)
376+
.create();
377+
dialog.show();
378+
379+
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
376380
@Override
377381
public void onClick(View v) {
378-
selectedTemplate.restoreItem(TemplateEditor.this,restorePosition,object);
379-
Snackbar.make(v,R.string.snackbar_restored_message,Snackbar.LENGTH_LONG).show();
382+
dialog.dismiss();
383+
selectedTemplate.deleteItem(TemplateEditor.this, selectedPosition);
384+
selectedPosition = -1;
385+
restoreSelectedView();
380386
}
381-
}).show();
387+
});
388+
382389
break;
383390
case R.id.action_edit:
384391
selectedTemplate.editItem(this, selectedPosition);
@@ -397,6 +404,10 @@ public void onClick(DialogInterface dialog, int id) {
397404

398405
case R.id.share_project:
399406
savedFilePath = saveProject();
407+
if(("File already exists".equals(savedFilePath))){
408+
return;
409+
}
410+
400411
if (savedFilePath == null || savedFilePath.length() == 0) {
401412
return;
402413
}
@@ -412,6 +423,9 @@ public void onClick(DialogInterface dialog, int id) {
412423
case R.id.share_apk:
413424

414425
savedFilePath = saveProject();
426+
if(("File already exists".equals(savedFilePath))){
427+
return;
428+
}
415429
if (savedFilePath == null || savedFilePath.length() == 0) {
416430
return;
417431
}
@@ -474,6 +488,9 @@ public void onFail(Exception e) {
474488
break;
475489
case R.id.save_apk:
476490
savedFilePath = saveProject();
491+
if(("File already exists".equals(savedFilePath))){
492+
return;
493+
}
477494
if (savedFilePath == null || savedFilePath.length() == 0) {
478495
return;
479496
}
@@ -607,7 +624,7 @@ private void restoreColorScheme() {
607624
private String saveProject() {
608625

609626
EditText authorEditText = (EditText) findViewById(R.id.author_name);
610-
EditText titleEditText = (EditText) findViewById(R.id.template_title);
627+
titleEditText = (EditText) findViewById(R.id.template_title);
611628
assert findViewById(R.id.author_name) != null;
612629
assert ((EditText) findViewById(R.id.author_name)) != null;
613630
String author = ((EditText) findViewById(R.id.author_name)).getText().toString();
@@ -664,10 +681,17 @@ private String saveProject() {
664681
saveFileName = saveFileName.replaceAll(" ", "-");
665682

666683

667-
FileUtils.saveXmlFile(toolkit.getSavedDir(), saveFileName, doc);
668-
oldFileName = toolkit.getSavedDir() + saveFileName;
669-
Toast.makeText(this, "Project Successfully Saved!", Toast.LENGTH_SHORT).show();
670-
return oldFileName;
684+
boolean isSaved=FileUtils.saveXmlFile(toolkit.getSavedDir(), saveFileName, doc);
685+
if(isSaved) {
686+
oldFileName = toolkit.getSavedDir() + saveFileName;
687+
Toast.makeText(this, "Project Successfully Saved!", Toast.LENGTH_SHORT).show();
688+
return oldFileName;
689+
}
690+
else {
691+
titleEditText.setError("File Already exists");
692+
return "File already exists";
693+
}
694+
671695
} catch (ParserConfigurationException e) {
672696
e.printStackTrace();
673697
}
@@ -771,16 +795,22 @@ private String saveDraft() {
771795
* @brief Start the simulator activity
772796
* <p/>
773797
* Start the simulator with the fragment returned by the selected template. Simulator is started as a new activity.
798+
* String message contains file response which will be filepath if successfully saved and otherwise error message.
774799
*/
775800
private void startSimulator() {
776-
String filePath = saveProject();
777-
if (filePath == null || filePath.equals("")) {
801+
String message = saveProject();
802+
if (message == null || message.equals("")) {
778803
Toast.makeText(this, "Build unsuccessful", Toast.LENGTH_SHORT).show();
779804
return;
780805
}
806+
else if("File already exists".equals(message))
807+
{
808+
titleEditText.setError("Template Already exists");
809+
return;
810+
}
781811
Intent simulatorIntent = new Intent(getApplicationContext(), Simulator.class);
782812
simulatorIntent.putExtra(Constants.TEMPLATE_ID, templateId);
783-
simulatorIntent.putExtra(Constants.SIMULATOR_FILE_PATH, filePath);
813+
simulatorIntent.putExtra(Constants.SIMULATOR_FILE_PATH, message);
784814
startActivity(simulatorIntent);
785815

786816
}
@@ -849,7 +879,7 @@ private void parseSavedFile(String path) {
849879
*/
850880
private void updateHeaderDetails(String name, String title) {
851881
EditText authorEditText = (EditText) findViewById(R.id.author_name);
852-
EditText titleEditText = (EditText) findViewById(R.id.template_title);
882+
titleEditText = (EditText) findViewById(R.id.template_title);
853883
assert authorEditText != null;
854884
authorEditText.setText(name);
855885
assert titleEditText != null;
@@ -882,3 +912,4 @@ protected void onStop() {
882912
}
883913

884914
}
915+

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.buildmlearn.toolkit.utilities;
22

33
import android.content.Context;
4+
45
import android.content.res.AssetManager;
56

7+
68
import org.w3c.dom.Document;
79

810
import java.io.BufferedInputStream;
@@ -194,7 +196,7 @@ public static boolean equalContent(File file1, File file2) {
194196
* @return Returns true if successfully converted
195197
* @brief Converts a given Document object to xml format file
196198
*/
197-
public static void saveXmlFile(String destinationFolder, String fileName, Document doc) {
199+
public static boolean saveXmlFile(String destinationFolder, String fileName, Document doc) {
198200

199201
File f = new File(destinationFolder);
200202
if (!f.isDirectory()) {
@@ -203,13 +205,18 @@ public static void saveXmlFile(String destinationFolder, String fileName, Docume
203205
TransformerFactory transformerFactory = TransformerFactory.newInstance();
204206
Transformer transformer;
205207
try {
208+
File newTemplateFile=new File(destinationFolder + fileName);
209+
if(newTemplateFile.exists())
210+
return false;
206211
transformer = transformerFactory.newTransformer();
207212
DOMSource source = new DOMSource(doc);
208-
StreamResult result = new StreamResult(new File(destinationFolder + fileName));
213+
StreamResult result = new StreamResult(newTemplateFile);
209214
transformer.transform(source, result);
215+
210216
} catch (TransformerException e) {
211217
e.printStackTrace();
212218
}
219+
return true;
213220
}
214221

215222
/**
@@ -312,3 +319,4 @@ public static void copy(File src, File dst) throws IOException {
312319
out.close();
313320
}
314321
}
322+

0 commit comments

Comments
 (0)