Fakultas Ilmu Komputer UI

cookies_interface.dart 2.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
import 'dart:convert';
import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:ppl_disabilitas/config/strings.dart';

class CookiesInterface {
  Future<bool> checkCookieFileAvailability({String fileName}) async {
    Directory dir;
    await getApplicationDocumentsDirectory().then((Directory directory) {
      dir = directory;
    });
13
    File cookieFile = File("${dir.path}/$fileName.json");
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    bool cookiesExist = cookieFile.existsSync();

    return cookiesExist;
  }

  Future<File> createSignInCookie({
      Map<String, dynamic> responseHeaders}) async {
    try {
      String setCookie;
      String csrfToken;
      String sessionId;
      String userKey;
      List<String> cookiesList;
      Directory dir;

      await getApplicationDocumentsDirectory().then((Directory directory) {
        dir = directory;
      });
32
      File cookieFile = File("${dir.path}/usercookies.json");
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      cookieFile.createSync();

      setCookie = responseHeaders["set-cookie"];
      if (setCookie != null) {
        csrfToken = setCookie.split(";")[0].split("=")[1];
        sessionId = setCookie.split(";")[4].split(",")[1].split("=")[1];
        userKey = key;
      }
      cookiesList = <String>[
        csrfToken,
        sessionId,
        userKey,
      ];
      cookieFile.writeAsStringSync(json.encode(cookiesList));
      return cookieFile;
    } on Exception catch (e) {
      print(e.toString());
50
      rethrow;
51
52
53
54
55
    }
  }

  Future<File> createSearchHistoryCookie({
    Map<String, dynamic> recentSearch}) async {
56
    print("recent searrch $recentSearch");
57
    Directory dir;
58
    List currentSearchHistory;
59
60
61
62
    try {
      await getApplicationDocumentsDirectory().then((Directory directory) {
        dir = directory;
      });
63
      File cookieFile = File('${dir.path}/searchhistory.json');
64
      cookieFile.createSync();
65
66
67
68
69
70
      await checkCookieFileAvailability(fileName: "searchhistory").then((available) async {
        if (available) {
          await getCookieFile(fileName: "searchhistory").then((cookie) {
            if (cookie == null) {
              currentSearchHistory = [];
            } else {
71
              currentSearchHistory = cookie == "" ? [] : json.decode(cookie);
72
            }
73
            currentSearchHistory.insert(0, recentSearch);
74
75
76
77
          });
        } else {
          currentSearchHistory = [];
        }
78
        await cookieFile.writeAsString(json.encode(currentSearchHistory));
79
      });
80
81
82
      return cookieFile;
    } on Exception catch (e) {
      print(e.toString());
83
      rethrow;
84
85
86
    }
  }

87
  Future<dynamic> getCookieFile({String fileName}) async {
88
89
90
91
    Directory dir;
    await getApplicationDocumentsDirectory().then((Directory directory) {
      dir = directory;
    });
92
    File file = File("${dir.path}/$fileName.json");
93
94
95
    dynamic res;
    try {
      res = file.readAsStringSync();
Fakhira Devina's avatar
Fakhira Devina committed
96
    } on Exception {
97
98
      res = [];
    }
99
    return res;
100
101
  }
}