| @@ -15,10 +15,15 @@ class DBHelper { | |||||
| Future<Database> get db async { | Future<Database> get db async { | ||||
| if (_db == null) _db = await initDb(); | if (_db == null) _db = await initDb(); | ||||
| //backupDatabase | |||||
| String backupPath = join(_db.path.substring(0,_db.path.indexOf("/Android"))+"/Download/UnitDBBackup/", "UnitStocking.db"); | |||||
| io.File dbFile = io.File(_db.path); | |||||
| dbFile.copySync(backupPath); | |||||
| try{ | |||||
| //backupDatabase | |||||
| String backupPath = join(_db.path.substring(0,_db.path.indexOf("/Android"))+"/Download/UnitDBBackup/", "${prefs.getString(keyClass.stock_id)}_UnitStocking.db"); | |||||
| io.File dbFile = io.File(_db.path); | |||||
| dbFile.copySync(backupPath); | |||||
| } | |||||
| catch(e){ | |||||
| print(e); | |||||
| } | |||||
| return _db; | return _db; | ||||
| } | } | ||||
| @@ -30,7 +35,16 @@ class DBHelper { | |||||
| } | } | ||||
| restoreDb()async{ | restoreDb()async{ | ||||
| // do something here | |||||
| await closeDb(); | |||||
| io.Directory documentsDirectory = await getExternalStorageDirectory(); | |||||
| String backupPath = join(documentsDirectory.path.substring(0,documentsDirectory.path.indexOf("/Android"))+"/Download/UnitDBBackup/", "${prefs.getString(keyClass.stock_id)}_UnitStocking.db"); | |||||
| io.File backupdbFile = io.File(backupPath); | |||||
| if(backupdbFile.existsSync()){ | |||||
| String path = join(documentsDirectory.path, "UnitStocking.db"); | |||||
| backupdbFile.copySync(path); | |||||
| return {"STATUS":1,"MSG":"File Back up successfull"}; | |||||
| } | |||||
| return {"STATUS":0,"MSG":"File Backup tidak ditemukan!"}; | |||||
| } | } | ||||
| closeDb() async { | closeDb() async { | ||||
| @@ -14,6 +14,7 @@ class keyClass{ | |||||
| static String stock_id = 'STOCK_TAKING_UNIT_ID'; | static String stock_id = 'STOCK_TAKING_UNIT_ID'; | ||||
| static String client_credential = 'CLIENT_CREDENTIAL'; | static String client_credential = 'CLIENT_CREDENTIAL'; | ||||
| static String rest_data = 'REST_DATA'; | static String rest_data = 'REST_DATA'; | ||||
| static String tgl_start = 'TGL_START'; | |||||
| } | } | ||||
| class columnName{ | class columnName{ | ||||
| static String id = 'ID'; | static String id = 'ID'; | ||||
| @@ -522,6 +522,16 @@ class _HomePageState extends State<HomePage> { | |||||
| ), | ), | ||||
| ), | ), | ||||
| PopupMenuItem( | PopupMenuItem( | ||||
| value:'restore', | |||||
| child: Row( | |||||
| children: <Widget>[ | |||||
| Icon(Icons.sync,color: Colors.black,), | |||||
| SizedBox(width: 10,), | |||||
| Text('Restore Backup') | |||||
| ], | |||||
| ), | |||||
| ), | |||||
| PopupMenuItem( | |||||
| value:'logout', | value:'logout', | ||||
| child: Row( | child: Row( | ||||
| children: <Widget>[ | children: <Widget>[ | ||||
| @@ -660,6 +670,41 @@ class _HomePageState extends State<HomePage> { | |||||
| prefs.setBool(keyClass.logged_in, false); | prefs.setBool(keyClass.logged_in, false); | ||||
| Navigator.pushNamed(context, '/login'); | Navigator.pushNamed(context, '/login'); | ||||
| } | } | ||||
| if(value == 'restore'){ | |||||
| await showDialog(context: context,builder: (context)=>AlertDialog( | |||||
| title: Text('Restore Data ?'), | |||||
| content: Text('Restore data akan menghapus dan mengganti data unit yang ada dengan data unit backup.'), | |||||
| actions: <Widget>[ | |||||
| TextButton( | |||||
| child: Text('Proceed'), | |||||
| onPressed: ()async{ | |||||
| util.showLoading(context); | |||||
| var result = await DBHelper.database.restoreDb(); | |||||
| if(result["MSG"]==1){ | |||||
| await prefs.remove(keyClass.lastDownload); | |||||
| await prefs.remove(keyClass.lastUpload); | |||||
| await prefs.remove(keyClass.targetProccess); | |||||
| await prefs.remove(keyClass.submitProccess); | |||||
| setState(() { | |||||
| lastUpload = ''; | |||||
| lastDownload = ''; | |||||
| timeString = ''; | |||||
| }); | |||||
| valueTab value = await DBHelper.database.getValue(keyClass.tgl_start); | |||||
| if(value != null)await prefs.setString(keyClass.lastDownload, value.value); | |||||
| } | |||||
| util.showFlushbar(context, result["MSG"]); | |||||
| Navigator.pop(context); | |||||
| Navigator.pop(context); | |||||
| }, | |||||
| ), | |||||
| TextButton( | |||||
| child: Text('Cancel'), | |||||
| onPressed: ()=>Navigator.pop(context), | |||||
| ) | |||||
| ], | |||||
| )); | |||||
| } | |||||
| }, | }, | ||||
| ), | ), | ||||
| ], | ], | ||||