Created
April 18, 2021 16:06
-
-
Save polyGeek/907f08418c4022a8fc09fa4c6ad012fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileStorage { | |
Future<bool> doesUserHavePreviousID() async { | |
/// If you need to test your code that runs when an existing | |
/// user IS FOUND then uncomment the lines below | |
/*oldUserID = 167081; | |
Init.gotUserID = true; // static int where userID is stored if found. | |
return true;*/ | |
dynamic directory = await path_provider.getApplicationDocumentsDirectory(); | |
/// This is going to run a try/catch loop through every folder | |
/// starting with '/' until it succeeds, then return the contents. | |
List<dynamic> allContents = loopThroughPath( fullPath: directory.path ); | |
_log( msg: 'allContents = ' + allContents.toString() ); | |
/// Pass the 'allContents' List to this method to recursively | |
/// look for the user.txt file. If it succeeds it will read | |
/// the contents of the file and store that on a static variable | |
/// and return true. If user.txt is not found then return false | |
bool foundID = await loopThroughAllContents( allContents: allContents ); | |
/// This is a hack to fix a case where the user.txt file does exist, | |
/// but was not found. | |
if( foundID == false ) { | |
_log( msg: 'Trying to force Library on the path.' ); | |
try{ | |
allContents = loopThroughPath( | |
fullPath: directory.path.substring( 0, directory.path.indexOf( 'Documents' ) ) + 'Library/' | |
); | |
} catch ( e ) { | |
_log( msg: 'Documents folder not found in Application Documents Directory' ); | |
} | |
foundID = await loopThroughAllContents( allContents: allContents ); | |
} | |
if ( foundID == true ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
List getAllContents( { String p } ) { | |
dynamic file; | |
try{ | |
file = io.Directory( p ); | |
if( file != null ) | |
_log(msg: "file = " + file.toString()); | |
return file.listSync( recursive: true ); | |
} catch( e ) { | |
_log( msg: "Cannot read file.listSync" ); | |
return null; | |
} | |
} | |
List<dynamic> loopThroughPath( { String fullPath } ) { | |
List<String> paths = fullPath.split( '/' ); | |
List<dynamic> allContents; | |
String path = ''; | |
for( String p in paths ) { | |
path += ( p + '/' ); | |
allContents = getAllContents( p: path ); | |
/// Found It! | |
if( allContents != null ) { | |
_log( msg: 'FOUND IT' ); | |
return allContents; | |
} | |
} | |
/// Outside the loop. If allContents still null then quit. | |
if( allContents == null ) { | |
return null; | |
} | |
return null; | |
} | |
Future<bool> loopThroughAllContents( { List<dynamic> allContents } ) async { | |
int loopCount = 0; | |
bool gotUserID = false; | |
for ( var fileOrDir in allContents ) { | |
_log( msg: loopCount.toString() + ' - fileOrDir = ' + fileOrDir.toString() ); | |
loopCount++; | |
if( fileOrDir is io.File ) { | |
if( fileOrDir.path.indexOf( 'userID.txt' ) != -1 ) { | |
_log(msg: 'FOUND userID.txt'); | |
gotUserID = true; | |
/// Set static value on Init. | |
Init.oldUserID = int.parse( await fileOrDir.readAsString() ); | |
/// Don't let userID be null. | |
Init.oldUserID = (Init.oldUserID == null) ? 0 : Init.oldUserID; | |
_log(msg: 'oldUserID = ' + Init.oldUserID.toString()); | |
} | |
if ( gotUserID ) break; | |
} | |
} | |
return gotUserID; | |
} | |
Future<String> readMsg({String filename}) async { | |
try { | |
final file = await _localFile(filename: filename); | |
/// Read the file | |
String contents = await file.readAsString(); | |
return contents; | |
} catch (e) { | |
/// If encountering an error, return 'null'. | |
return null; | |
} | |
} | |
Future<io.File> writeMsg({String filename, String msg}) async { | |
final file = await _localFile(filename: filename); | |
// Write the file | |
return file.writeAsString(msg); | |
} | |
Future<String> get _localPath async { | |
final directory = await path_provider.getApplicationDocumentsDirectory(); | |
String path = directory.path.substring( 0, directory.path.lastIndexOf( '/' ) ); | |
return path + '/'; | |
} | |
Future<io.File> _localFile({String filename}) async { | |
final path = await _localPath; | |
return io.File( '$path/$filename' ); | |
} | |
/// This method does much more in my code, but I removed it to simplfy this example. | |
void _log( { String msg } ) { | |
print( msg ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment