BlackListUtils.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. import 'dart:async';
  3. // 黑名单工具类,用于在本地操作黑名单
  4. class BlackListUtils {
  5. static final String SP_BLACK_LIST = "blackList";
  6. // 将对象数组转化为整型数组
  7. static List<int?> convert(List objList) {
  8. if (objList == null || objList.isEmpty) {
  9. return <int>[];
  10. }
  11. List<int?> intList = [];
  12. for (var obj in objList) {
  13. intList.add(obj['authorid']);
  14. }
  15. return intList;
  16. }
  17. // 字符串转化为整型数组
  18. static List<int?>? _str2intList(String? str) {
  19. if (str != null && str.length > 0) {
  20. List<String> list = str.split(",");
  21. if (list != null && list.isNotEmpty) {
  22. List<int?> intList = [];
  23. for (String s in list) {
  24. intList.add(int.parse(s));
  25. }
  26. return intList;
  27. }
  28. }
  29. return null;
  30. }
  31. // 整型数组转化为字符串
  32. static String? _intList2Str(List<int?> list) {
  33. if (list == null || list.isEmpty) {
  34. return null;
  35. }
  36. StringBuffer sb = StringBuffer();
  37. for (int? id in list) {
  38. sb.write("$id,");
  39. }
  40. String result = sb.toString();
  41. return result.substring(0, result.length - 1);
  42. }
  43. // 保存黑名单的id
  44. static Future<String?> saveBlackListIds(List<int?> list) async {
  45. String? str = _intList2Str(list);
  46. if (str != null) {
  47. SharedPreferences sp = await SharedPreferences.getInstance();
  48. sp.setString(SP_BLACK_LIST, str);
  49. } else {
  50. SharedPreferences sp = await SharedPreferences.getInstance();
  51. sp.setString(SP_BLACK_LIST, "");
  52. }
  53. return str;
  54. }
  55. // 获取本地保存的黑名单id数据
  56. static Future<List<int?>?> getBlackListIds() async {
  57. SharedPreferences sp = await SharedPreferences.getInstance();
  58. String? str = sp.getString(SP_BLACK_LIST);
  59. if (str != null && str.length > 0) {
  60. return _str2intList(str);
  61. }
  62. return null;
  63. }
  64. // 向黑名单中添加一个id
  65. static Future<List<int?>?> addBlackId(int? id) async {
  66. List<int?>? list = await getBlackListIds();
  67. if (list != null && list.isNotEmpty) {
  68. if (!list.contains(id)) {
  69. list.add(id);
  70. String? str = await saveBlackListIds(list);
  71. return _str2intList(str);
  72. } else {
  73. return list;
  74. }
  75. } else {
  76. List<int?> l = [];
  77. l.add(id);
  78. String? str = await saveBlackListIds(l);
  79. return _str2intList(str);
  80. }
  81. }
  82. // 向黑名单中移除一个id
  83. static Future<List<int?>?> removeBlackId(int? id) async {
  84. List<int?>? list = await getBlackListIds();
  85. if (list != null && list.isNotEmpty) {
  86. if (list.contains(id)) {
  87. list.remove(id);
  88. String? str = await saveBlackListIds(list);
  89. return _str2intList(str);
  90. }
  91. }
  92. return list;
  93. }
  94. }