<commit_before>import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/drive/v3.dart';
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:swassistant/utils/driver/query.dart';

class Driver{
  String wd = "appDataFolder";
  DriveApi? api;
  GoogleSignIn? gsi;

  //ready returns whether the driver is ready to use. If the driver is not ready, it tries to initialize it.
  Future<bool> ready([String? wdFolder]) async {
    if(gsi != null && gsi!.currentUser != null && api != null){
      api = DriveApi((await gsi!.authenticatedClient())!);
      return true;
    }
    try{
      gsi ??= GoogleSignIn(scopes: [DriveApi.driveAppdataScope]);
      if(gsi!.currentUser == null || !(await gsi!.isSignedIn())){
        await gsi!.signInSilently();
        if(gsi!.currentUser == null) gsi!.signIn();
      }
      if(gsi!.currentUser == null) return false;
      api = DriveApi((await gsi!.authenticatedClient())!);
    } catch(e) {
      return false;
    }
    if(wdFolder != null) await setWD(wdFolder);
    return true;
  }

  //readySync is similar to ready, except does NOT try to initialize the driver. Use ready when possible.
  bool readySync() {
    return gsi != null && gsi!.currentUser != null && api != null;
  }

  Future<bool> setWD(String folder) async {
    if(!await ready()) return false;
    if(folder == "" || folder == "/"){
      wd = "appDataFolder";
      return true;
    }
    var foldId = await getIDFromRoot(folder, mimeType: DriveQueryBuilder.folderMime, createIfMissing: true);
    if (foldId == null) return false;
    wd = foldId;<commit_msg>Fix Google Drive syncing when first enabling it.<commit_after>import 'package:google_sign_in/google_sign_in.dart';
import 'package:googleapis/drive/v3.dart';
import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';
import 'package:swassistant/utils/driver/query.dart';

class Driver{
  String wd = "appDataFolder";
  DriveApi? api;
  GoogleSignIn? gsi;

  //ready returns whether the driver is ready to use. If the driver is not ready, it tries to initialize it.
  Future<bool> ready([String? wdFolder]) async {
    if(gsi != null && gsi!.currentUser != null && api != null){
      api = DriveApi((await gsi!.authenticatedClient())!);
      return true;
    }
    try{
      gsi ??= GoogleSignIn(scopes: [DriveApi.driveAppdataScope]);
      if(gsi!.currentUser == null || !(await gsi!.isSignedIn())){
        await gsi!.signInSilently();
        if(gsi!.currentUser == null) await gsi!.signIn();
      }
      if(gsi!.currentUser == null) return false;
      api = DriveApi((await gsi!.authenticatedClient())!);
    } catch(e) {
      return false;
    }
    if(wdFolder != null) await setWD(wdFolder);
    return true;
  }

  //readySync is similar to ready, except does NOT try to initialize the driver. Use ready when possible.
  bool readySync() {
    return gsi != null && gsi!.currentUser != null && api != null;
  }

  Future<bool> setWD(String folder) async {
    if(!await ready()) return false;
    if(folder == "" || folder == "/"){
      wd = "appDataFolder";
      return true;
    }
    var foldId = await getIDFromRoot(folder, mimeType: DriveQueryBuilder.folderMime, createIfMissing: true);
    if (foldId == null) return false;
    wd = foldId;
{'fraction_non_alphanumeric': 0.1337466784765279, 'fraction_numerical': 0.000590493061706525, 'mean_word_length': 3.6158038147138964, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '788791', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1074, 'n_words': 279}