Skip to content

Commit

Permalink
feat: use preference settings for analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
limwa committed Jan 12, 2024
1 parent 20257cb commit 34dda98
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions uni/lib/controller/local_storage/preferences_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class PreferencesController {
static const String _filteredExamsTypes = 'filtered_exam_types';
static final List<String> _defaultFilteredExamTypes = Exam.displayedTypes;

static final _statsToggleStreamController =
StreamController<bool>.broadcast();
static final onStatsToggle = _statsToggleStreamController.stream;

/// Returns the last time the data with given key was updated.
static DateTime? getLastDataClassUpdateTime(String dataKey) {
final lastUpdateTime = prefs.getString(dataKey + _lastUpdateTimeKeySuffix);
Expand Down Expand Up @@ -290,5 +294,6 @@ class PreferencesController {
required bool value,
}) async {
await prefs.setBool(_usageStatsToggleKey, value);
_statsToggleStreamController.add(value);
}
}
35 changes: 33 additions & 2 deletions uni/lib/model/providers/plausible/plausible_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import 'dart:async';
import 'package:battery_plus/battery_plus.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/widgets.dart';
import 'package:logger/logger.dart';
import 'package:plausible_analytics/plausible_analytics.dart';
import 'package:provider/provider.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';

class PlausibleProvider extends StatefulWidget {
const PlausibleProvider({
Expand All @@ -24,6 +26,7 @@ class _PlausibleProviderState extends State<PlausibleProvider> {
int _batteryLevel = 0;
bool _isInBatterySaveMode = true;
ConnectivityResult _connectivityResult = ConnectivityResult.mobile;
bool _isUsageStatsEnabled = true;

bool _canUpdateBatteryState = true;

Expand All @@ -37,16 +40,20 @@ class _PlausibleProviderState extends State<PlausibleProvider> {

_startListeners(plausible)
.then((_) => _updateBatteryState())
.then((_) => _updateConnectivityState());
.then((_) => _updateConnectivityState())
.then((_) => _updateUsageStatsState());
}
}

void _updateAnalyticsState() {
final plausible = widget.plausible;
if (plausible != null) {
plausible.enabled = _batteryLevel > 20 &&
plausible.enabled = _isUsageStatsEnabled &&
!_isInBatterySaveMode &&
_batteryLevel > 20 &&
_connectivityResult == ConnectivityResult.wifi;

Logger().d("Plausible enabled: ${plausible.enabled}");
}
}

Expand All @@ -62,20 +69,44 @@ class _PlausibleProviderState extends State<PlausibleProvider> {
_batteryLevel = await battery.batteryLevel;
_isInBatterySaveMode = await battery.isInBatterySaveMode;

Logger().d("Battery level: $_batteryLevel");
Logger().d("Is in battery save mode: $_isInBatterySaveMode");

_updateAnalyticsState();
}

Future<void> _updateConnectivityState() async {
final connectivity = Connectivity();
_connectivityResult = await connectivity.checkConnectivity();

Logger().d("Connectivity result: $_connectivityResult");

_updateAnalyticsState();
}

Future<void> _updateUsageStatsState() async {
_isUsageStatsEnabled = PreferencesController.getUsageStatsToggle();

Logger().d("Usage stats enabled: $_isUsageStatsEnabled");

_updateAnalyticsState();
}

Future<void> _startListeners(Plausible plausible) async {
final connectivity = Connectivity();
connectivity.onConnectivityChanged.listen((result) {
_connectivityResult = result;

Logger().d("Connectivity result: $_connectivityResult");

_updateAnalyticsState();
});

PreferencesController.onStatsToggle.listen((event) {
_isUsageStatsEnabled = event;

Logger().d("Usage stats enabled: $_isUsageStatsEnabled");

_updateAnalyticsState();
});
}
Expand Down

0 comments on commit 34dda98

Please sign in to comment.