Skip to content

Commit

Permalink
Move wuid generator
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed Nov 13, 2023
1 parent b270c18 commit fe3e6d6
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/src/_wiredash_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export 'package:wiredash/src/core/wiredash_model_provider.dart';
export 'package:wiredash/src/metadata/all_meta_data.dart';
export 'package:wiredash/src/metadata/build_info/app_info.dart';
export 'package:wiredash/src/metadata/build_info/build_info.dart';
export 'package:wiredash/src/metadata/build_info/uid_generator.dart';
export 'package:wiredash/src/core/wuid_generator.dart';
export 'package:wiredash/src/metadata/device_info/device_info.dart';
export 'package:wiredash/src/metadata/device_info/device_info_generator.dart';
export 'package:wiredash/src/utils/object_util.dart';
Expand Down
10 changes: 5 additions & 5 deletions lib/src/core/services/services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WiredashServices extends ChangeNotifier {

FeedbackSubmitter get feedbackSubmitter => _locator.watch();

UidGenerator get uidGenerator => _locator.watch();
WuidGenerator get wuidGenerator => _locator.watch();

Wiredash get wiredashWidget => _locator.watch();

Expand Down Expand Up @@ -110,7 +110,7 @@ void _setupServices(WiredashServices sl) {
child: SizedBox(),
),
);
sl.inject<UidGenerator>((_) => UidGenerator());
sl.inject<WuidGenerator>((_) => WuidGenerator());
sl.inject<ProjectCredentialValidator>(
(_) => const ProjectCredentialValidator(),
);
Expand All @@ -122,7 +122,7 @@ void _setupServices(WiredashServices sl) {
);
sl.inject<PsTrigger>((_) {
return PsTrigger(
deviceIdGenerator: sl.uidGenerator,
wuidGenerator: sl.wuidGenerator,
appTelemetry: sl.appTelemetry,
wiredashTelemetry: sl.wiredashTelemetry,
);
Expand Down Expand Up @@ -190,7 +190,7 @@ void _setupServices(WiredashServices sl) {
sharedPreferencesProvider: SharedPreferences.getInstance,
dirPathProvider: () async =>
(await getApplicationDocumentsDirectory()).path,
idGenerator: sl.uidGenerator,
wuidGenerator: sl.wuidGenerator,
);
final retryingFeedbackSubmitter =
RetryingFeedbackSubmitter(fileSystem, storage, sl.api);
Expand Down Expand Up @@ -229,7 +229,7 @@ void _setupServices(WiredashServices sl) {
'ping',
PingJob(
apiProvider: () => sl.api,
uidGenerator: () => sl.uidGenerator,
wuidGenerator: () => sl.wuidGenerator,
metaDataCollector: () => sl.metaDataCollector,
sharedPreferencesProvider: SharedPreferences.getInstance,
),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/core/sync/ping_job.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import 'package:wiredash/src/metadata/meta_data_collector.dart';
class PingJob extends Job {
final WiredashApi Function() apiProvider;
final Future<SharedPreferences> Function() sharedPreferencesProvider;
final UidGenerator Function() uidGenerator;
final WuidGenerator Function() wuidGenerator;
final MetaDataCollector Function() metaDataCollector;

PingJob({
required this.apiProvider,
required this.sharedPreferencesProvider,
required this.uidGenerator,
required this.wuidGenerator,
required this.metaDataCollector,
});

Expand Down Expand Up @@ -56,7 +56,7 @@ class PingJob extends Job {
await metaDataCollector().collectSessionMetaData(null);

final body = PingRequestBody(
analyticsId: await uidGenerator().appUsageId(),
analyticsId: await wuidGenerator().appUsageId(),
buildCommit:
sessionMetaData.buildCommit ?? fixedData.buildInfo.buildCommit,
appVersion:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import 'package:nanoid2/nanoid2.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// Generates a unique ids for client identification
class UidGenerator {
/// Wiredash Unique Identifier Generator
class WuidGenerator {
static const _prefsDeviceID = '_wiredashDeviceID';
static const _prefsAppUsageID = '_wiredashAppUsageID';

Expand All @@ -14,7 +14,7 @@ class UidGenerator {
/// the deviceId fallback should generate in finite time
static const _sharedPrefsTimeout = Duration(seconds: 2);

UidGenerator();
WuidGenerator();

/// Feedbacks that are saved locally (offline) until they are sent to the server
String localFeedbackId() {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/feedback/data/pending_feedback_item_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class PendingFeedbackItemStorage {
required FileSystem fileSystem,
required Future<SharedPreferences> Function() sharedPreferencesProvider,
required Future<String> Function() dirPathProvider,
required UidGenerator idGenerator,
required WuidGenerator wuidGenerator,
}) : _fs = fileSystem,
_sharedPreferences = sharedPreferencesProvider,
_getScreenshotStorageDirectoryPath = dirPathProvider,
_idGenerator = idGenerator;
_wuidGenerator = wuidGenerator;

final FileSystem _fs;
final Future<SharedPreferences> Function() _sharedPreferences;
final Future<String> Function() _getScreenshotStorageDirectoryPath;
final UidGenerator _idGenerator;
final WuidGenerator _wuidGenerator;

static const _feedbackItemsKey = 'io.wiredash.pending_feedback_items';

Expand Down Expand Up @@ -86,7 +86,7 @@ class PendingFeedbackItemStorage {
}
// save file to disk
final screenshotsDir = await _getScreenshotStorageDirectoryPath();
final uniqueFileName = _idGenerator.screenshotFilename();
final uniqueFileName = _wuidGenerator.screenshotFilename();
final filePath = _fs.path
.normalize(_fs.path.join(screenshotsDir, '$uniqueFileName.png'));
final data = attachment.file.binaryData(_fs)!;
Expand All @@ -101,7 +101,7 @@ class PendingFeedbackItemStorage {
);

final pendingItem = PendingFeedbackItem(
id: _idGenerator.localFeedbackId(),
id: _wuidGenerator.localFeedbackId(),
feedbackItem: serializable,
);

Expand Down
2 changes: 1 addition & 1 deletion lib/src/feedback/feedback_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class FeedbackModel extends ChangeNotifier2 {
}

Future<FeedbackItem> createFeedback() async {
final deviceId = await _services.uidGenerator.submitId();
final deviceId = await _services.wuidGenerator.submitId();

final fixedMetadata =
await _services.metaDataCollector.collectFixedMetaData();
Expand Down
2 changes: 1 addition & 1 deletion lib/src/promoterscore/ps_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PsModel extends ChangeNotifier2 {
Object? _submissionError;

Future<void> updatePromoterScoreRecord({bool silentFail = true}) async {
final deviceId = await _services.uidGenerator.submitId();
final deviceId = await _services.wuidGenerator.submitId();
final fixedMetadata =
await _services.metaDataCollector.collectFixedMetaData();
final sessionMetadata =
Expand Down
6 changes: 3 additions & 3 deletions lib/src/promoterscore/ps_trigger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import 'package:wiredash/src/core/telemetry/wiredash_telemetry.dart';
/// Decides when it is time to show the promoter score survey
class PsTrigger {
PsTrigger({
required this.deviceIdGenerator,
required this.wuidGenerator,
required this.appTelemetry,
required this.wiredashTelemetry,
});

final UidGenerator deviceIdGenerator;
final WuidGenerator wuidGenerator;
final AppTelemetry appTelemetry;
final WiredashTelemetry wiredashTelemetry;

Expand Down Expand Up @@ -93,7 +93,7 @@ class PsTrigger {
if (lastSurvey == null) {
// Using the device id to randomly distribute the next survey time within
// frequency. This results in the same date for every call of this method
final String deviceId = await deviceIdGenerator.submitId();
final String deviceId = await wuidGenerator.submitId();
final random = Random(deviceId.hashCode);
final shiftTimeInS = (random.nextDouble() * frequency.inSeconds).toInt();
final DateTime? firstAppStart = await appTelemetry.firstAppStart();
Expand Down
8 changes: 4 additions & 4 deletions test/feedback/data/pending_feedback_item_storage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ void main() {
group('PendingFeedbackItemStorage', () {
late FileSystem fileSystem;
late InMemorySharedPreferences prefs;
late UidGenerator idGenerator;
late WuidGenerator wuidGenerator;
late PendingFeedbackItemStorage storage;

setUp(() {
fileSystem = MemoryFileSystem.test();
prefs = InMemorySharedPreferences();
idGenerator = IncrementalIdGenerator();
wuidGenerator = IncrementalIdGenerator();
storage = PendingFeedbackItemStorage(
fileSystem: fileSystem,
sharedPreferencesProvider: () async => prefs,
dirPathProvider: () async => '.',
idGenerator: idGenerator,
wuidGenerator: wuidGenerator,
);
});

Expand Down Expand Up @@ -402,7 +402,7 @@ class InMemorySharedPreferences extends Fake implements SharedPreferences {
}

/// Creates string IDs that increment
class IncrementalIdGenerator implements UidGenerator {
class IncrementalIdGenerator implements WuidGenerator {
var _nextInt = 0;

String next() {
Expand Down
2 changes: 1 addition & 1 deletion test/feedback/data/retrying_feedback_submitter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void main() {
idGenerator = IncrementalIdGenerator();
storage = PendingFeedbackItemStorage(
fileSystem: fileSystem,
idGenerator: idGenerator,
wuidGenerator: idGenerator,
dirPathProvider: () async => '.',
sharedPreferencesProvider: () async => preferences,
);
Expand Down
26 changes: 13 additions & 13 deletions test/ps/ps_trigger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:wiredash/src/_ps.dart';
import 'package:wiredash/src/core/telemetry/app_telemetry.dart';
import 'package:wiredash/src/core/telemetry/wiredash_telemetry.dart';
import 'package:wiredash/src/metadata/build_info/uid_generator.dart';
import 'package:wiredash/src/core/wuid_generator.dart';

void main() {
setUp(() {
Expand All @@ -27,7 +27,7 @@ void main() {
final trigger = PsTrigger(
appTelemetry: appTelemetry,
wiredashTelemetry: wiredashTelemetry,
deviceIdGenerator: FakeDeviceIdGenerator('qwer'),
wuidGenerator: FakeWuidGenerator('qwer'),
);
final options = PsOptions(
frequency: frequency,
Expand Down Expand Up @@ -105,7 +105,7 @@ void main() {
test('first interval is randomly distributed, based on deviceId', () async {
final DateTime now = DateTime.utc(2020);
await withClock(Clock(() => now), () async {
final deviceIdGenerator = FakeDeviceIdGenerator('');
final deviceIdGenerator = FakeWuidGenerator('');
const frequency = Duration(days: 10);
final wiredashTelemetry =
PersistentWiredashTelemetry(SharedPreferences.getInstance);
Expand All @@ -115,19 +115,19 @@ void main() {
final trigger = PsTrigger(
appTelemetry: appTelemetry,
wiredashTelemetry: wiredashTelemetry,
deviceIdGenerator: deviceIdGenerator,
wuidGenerator: deviceIdGenerator,
);
const options = PsOptions(frequency: frequency);

deviceIdGenerator.mockedDeviceId = 'one';
deviceIdGenerator.mockedSubmitId = 'one';
final date1 = await trigger.earliestNextPromoterScoreSurveyDate(options);
expect(date1, DateTime.utc(2020, 1, 6, 21, 43, 8));

deviceIdGenerator.mockedDeviceId = 'two';
deviceIdGenerator.mockedSubmitId = 'two';
final date2 = await trigger.earliestNextPromoterScoreSurveyDate(options);
expect(date2, DateTime.utc(2020, 1, 4, 5, 45, 30));

deviceIdGenerator.mockedDeviceId = 'three';
deviceIdGenerator.mockedSubmitId = 'three';
final date3 = await trigger.earliestNextPromoterScoreSurveyDate(options);
expect(date3, DateTime.utc(2020, 1, 10, 6, 50, 44));

Expand Down Expand Up @@ -157,7 +157,7 @@ void main() {
final trigger = PsTrigger(
appTelemetry: appTelemetry,
wiredashTelemetry: wiredashTelemetry,
deviceIdGenerator: FakeDeviceIdGenerator('qwer'),
wuidGenerator: FakeWuidGenerator('qwer'),
);

now = now.add(const Duration(days: 100));
Expand Down Expand Up @@ -188,21 +188,21 @@ void main() {
appTelemetry: appTelemetry,
wiredashTelemetry:
PersistentWiredashTelemetry(SharedPreferences.getInstance),
deviceIdGenerator: FakeDeviceIdGenerator('qwer'),
wuidGenerator: FakeWuidGenerator('qwer'),
);

expect(await trigger.shouldShowPromoterSurvey(options: options), isTrue);
});
});
}

class FakeDeviceIdGenerator with Fake implements UidGenerator {
String mockedDeviceId;
class FakeWuidGenerator with Fake implements WuidGenerator {
String mockedSubmitId;

FakeDeviceIdGenerator(this.mockedDeviceId);
FakeWuidGenerator(this.mockedSubmitId);

@override
Future<String> submitId() async {
return mockedDeviceId;
return mockedSubmitId;
}
}
6 changes: 3 additions & 3 deletions test/sync/ping_job_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
apiProvider: () => api,
sharedPreferencesProvider: prefsProvider,
metaDataCollector: () => FakeMetaDataCollector(),
uidGenerator: () => IncrementalIdGenerator(),
wuidGenerator: () => IncrementalIdGenerator(),
);

setUp(() {
Expand Down Expand Up @@ -102,7 +102,7 @@ void main() {
apiProvider: () => api,
sharedPreferencesProvider: prefsProvider,
metaDataCollector: () => FakeMetaDataCollector(),
uidGenerator: () => IncrementalIdGenerator(),
wuidGenerator: () => IncrementalIdGenerator(),
);
pingJob.execute();
async.flushTimers();
Expand All @@ -124,7 +124,7 @@ void main() {
apiProvider: () => api,
sharedPreferencesProvider: prefsProvider,
metaDataCollector: () => FakeMetaDataCollector(),
uidGenerator: () => IncrementalIdGenerator(),
wuidGenerator: () => IncrementalIdGenerator(),
);
pingJob.execute();
async.flushTimers();
Expand Down

0 comments on commit fe3e6d6

Please sign in to comment.