1
1
package org .buildmlearn .toolkit .activity ;
2
-
3
2
import android .Manifest ;
4
3
import android .app .ProgressDialog ;
5
4
import android .content .Context ;
15
14
import android .os .Handler ;
16
15
import android .os .Message ;
17
16
import android .preference .PreferenceManager ;
18
- import android .support .design .widget .Snackbar ;
19
17
import android .support .v4 .app .ActivityCompat ;
20
18
import android .support .v4 .content .ContextCompat ;
21
19
import android .support .v7 .app .ActionBar ;
@@ -87,10 +85,12 @@ public void handleMessage(Message message) {
87
85
private int selectedPosition = -1 ;
88
86
private boolean showTemplateSelectedMenu ;
89
87
private View selectedView ;
88
+ private EditText titleEditText ;
90
89
private ToolkitApplication toolkit ;
91
90
private String oldFileName ;
92
91
private ProgressDialog mApkGenerationDialog ;
93
92
93
+
94
94
/**
95
95
* {@inheritDoc}
96
96
*/
@@ -306,7 +306,8 @@ private void setUpTemplateEditor() {
306
306
setUpActionBar ();
307
307
} catch (IllegalAccessException e ) {
308
308
e .printStackTrace ();
309
- } catch (InstantiationException e ) {
309
+ }
310
+ catch (InstantiationException e ) {
310
311
e .printStackTrace ();
311
312
}
312
313
}
@@ -366,19 +367,25 @@ public boolean onOptionsItemSelected(MenuItem item) {
366
367
367
368
switch (id ) {
368
369
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 () {
376
380
@ Override
377
381
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 ();
380
386
}
381
- }).show ();
387
+ });
388
+
382
389
break ;
383
390
case R .id .action_edit :
384
391
selectedTemplate .editItem (this , selectedPosition );
@@ -397,6 +404,10 @@ public void onClick(DialogInterface dialog, int id) {
397
404
398
405
case R .id .share_project :
399
406
savedFilePath = saveProject ();
407
+ if (("File already exists" .equals (savedFilePath ))){
408
+ return ;
409
+ }
410
+
400
411
if (savedFilePath == null || savedFilePath .length () == 0 ) {
401
412
return ;
402
413
}
@@ -412,6 +423,9 @@ public void onClick(DialogInterface dialog, int id) {
412
423
case R .id .share_apk :
413
424
414
425
savedFilePath = saveProject ();
426
+ if (("File already exists" .equals (savedFilePath ))){
427
+ return ;
428
+ }
415
429
if (savedFilePath == null || savedFilePath .length () == 0 ) {
416
430
return ;
417
431
}
@@ -474,6 +488,9 @@ public void onFail(Exception e) {
474
488
break ;
475
489
case R .id .save_apk :
476
490
savedFilePath = saveProject ();
491
+ if (("File already exists" .equals (savedFilePath ))){
492
+ return ;
493
+ }
477
494
if (savedFilePath == null || savedFilePath .length () == 0 ) {
478
495
return ;
479
496
}
@@ -607,7 +624,7 @@ private void restoreColorScheme() {
607
624
private String saveProject () {
608
625
609
626
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 );
611
628
assert findViewById (R .id .author_name ) != null ;
612
629
assert ((EditText ) findViewById (R .id .author_name )) != null ;
613
630
String author = ((EditText ) findViewById (R .id .author_name )).getText ().toString ();
@@ -664,10 +681,17 @@ private String saveProject() {
664
681
saveFileName = saveFileName .replaceAll (" " , "-" );
665
682
666
683
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
+
671
695
} catch (ParserConfigurationException e ) {
672
696
e .printStackTrace ();
673
697
}
@@ -771,16 +795,22 @@ private String saveDraft() {
771
795
* @brief Start the simulator activity
772
796
* <p/>
773
797
* 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.
774
799
*/
775
800
private void startSimulator () {
776
- String filePath = saveProject ();
777
- if (filePath == null || filePath .equals ("" )) {
801
+ String message = saveProject ();
802
+ if (message == null || message .equals ("" )) {
778
803
Toast .makeText (this , "Build unsuccessful" , Toast .LENGTH_SHORT ).show ();
779
804
return ;
780
805
}
806
+ else if ("File already exists" .equals (message ))
807
+ {
808
+ titleEditText .setError ("Template Already exists" );
809
+ return ;
810
+ }
781
811
Intent simulatorIntent = new Intent (getApplicationContext (), Simulator .class );
782
812
simulatorIntent .putExtra (Constants .TEMPLATE_ID , templateId );
783
- simulatorIntent .putExtra (Constants .SIMULATOR_FILE_PATH , filePath );
813
+ simulatorIntent .putExtra (Constants .SIMULATOR_FILE_PATH , message );
784
814
startActivity (simulatorIntent );
785
815
786
816
}
@@ -849,7 +879,7 @@ private void parseSavedFile(String path) {
849
879
*/
850
880
private void updateHeaderDetails (String name , String title ) {
851
881
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 );
853
883
assert authorEditText != null ;
854
884
authorEditText .setText (name );
855
885
assert titleEditText != null ;
@@ -882,3 +912,4 @@ protected void onStop() {
882
912
}
883
913
884
914
}
915
+
0 commit comments