Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tb 4009 integrate start io ads #579

Draft
wants to merge 5 commits into
base: TB-4009_placeholder_ads
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use cards as input instead of documents, so we can correctly chain in…
…jection use cases
frankpepermans authored and joecks committed Jul 20, 2022
commit 90a6dd323793fd52054fc6e00a5a94aaa3a9e9eb
Original file line number Diff line number Diff line change
@@ -9,32 +9,37 @@ import 'package:xayn_discovery_engine_flutter/discovery_engine.dart';
const int _skipEvery = 5;

@lazySingleton
class AdCardInjectionUseCase extends UseCase<AdCardInjectionData, Set<Card>> {
class AdCardInjectionUseCase extends UseCase<Set<Card>, Set<Card>> {
final FeatureManager featureManager;
final Set<DocumentId> _buffer = <DocumentId>{};

AdCardInjectionUseCase(this.featureManager);

@override
Stream<Set<Card>> transaction(AdCardInjectionData param) async* {
final nextDocuments = param.nextDocuments;
Stream<Set<Card>> transaction(Set<Card> param) async* {
if (!featureManager.areAdsEnabled) yield param;

if (nextDocuments == null) {
yield param.currentCards;
} else {
_buffer.addAll(nextDocuments.map((it) => it.documentId));
final documents = param
.where((it) => it.type == CardType.document)
.map((it) => it.document!)
.toSet();

yield toCards(nextDocuments).toSet();
}
_buffer.addAll(documents.map((it) => it.documentId));

yield toCards(param).toSet();
}

@visibleForTesting
Iterable<Card> toCards(Set<Document> documents) sync* {
for (final document in documents) {
yield Card.document(document);
Iterable<Card> toCards(Set<Card> cards) sync* {
for (final card in cards) {
yield card;

if (shouldShowAdAfter(document.documentId)) {
yield Card.other(CardType.ad, document.documentId.uniqueId);
if (card.type == CardType.document) {
final document = card.requireDocument;

if (shouldShowAdAfter(document.documentId)) {
yield Card.other(CardType.ad, document.documentId.uniqueId);
}
}
}
}
@@ -49,19 +54,3 @@ class AdCardInjectionUseCase extends UseCase<AdCardInjectionData, Set<Card>> {
return !isFirstGroup && isAtSkipLocation;
}
}

@immutable
class AdCardInjectionData {
final Set<Card> currentCards;
final Set<Document>? nextDocuments;

int get currentDocumentsCount =>
currentCards.where((it) => it.document != null).length;

int get nextDocumentsCount => nextDocuments?.length ?? 0;

const AdCardInjectionData({
required this.currentCards,
this.nextDocuments,
});
}
Original file line number Diff line number Diff line change
@@ -22,16 +22,14 @@ class SurveyCardInjectionUseCase

@override
Stream<Set<Card>> transaction(SurveyCardInjectionData param) async* {
final nextDocuments = param.nextDocuments;

if (nextDocuments == null) {
yield param.currentCards;
if (param.nextDocumentsCount == 0) {
yield param.cards;
} else {
if (shouldMarkInjectionPoint(param)) {
nextDocumentSibling = nextDocuments.last;
nextDocumentSibling = param.lastDocument;
}

yield toCards(nextDocuments).toSet();
yield toCards(param.cards).toSet();
}
}

@@ -43,31 +41,38 @@ class SurveyCardInjectionUseCase
data.nextDocumentsCount > 2;

@visibleForTesting
Iterable<Card> toCards(Set<Document> documents) sync* {
for (final document in documents) {
if (document == nextDocumentSibling) {
yield Card.other(CardType.survey, document.documentId.uniqueId);
Iterable<Card> toCards(Set<Card> cards) sync* {
for (final card in cards) {
if (card.type == CardType.document &&
card.requireDocument == nextDocumentSibling) {
yield Card.other(
CardType.survey, card.requireDocument.documentId.uniqueId);
}

yield Card.document(document);
yield card;
}
}
}

@immutable
class SurveyCardInjectionData {
final Set<Card> currentCards;
final Set<Document>? nextDocuments;
final Set<Card> cards;
final Set<Document> _documents;
final SurveyConditionsStatus status;

int get currentDocumentsCount =>
currentCards.where((it) => it.document != null).length;
cards.where((it) => it.document != null).length;

int get nextDocumentsCount => _documents.length;

int get nextDocumentsCount => nextDocuments?.length ?? 0;
Document get lastDocument => _documents.last;

const SurveyCardInjectionData({
required this.currentCards,
this.nextDocuments,
SurveyCardInjectionData({
required this.cards,
SurveyConditionsStatus? status,
}) : status = status ?? SurveyConditionsStatus.notReached;
}) : status = status ?? SurveyConditionsStatus.notReached,
_documents = cards
.where((it) => it.type == CardType.document)
.map((it) => it.document!)
.toSet();
}
Original file line number Diff line number Diff line change
@@ -371,7 +371,9 @@ abstract class BaseDiscoveryManager extends Cubit<DiscoveryState>
if (_cardIndex == null) return null;

final cards = await _injectCustomCardsIfAny(
documents: documents,
cards: documents == null
? state.cards
: documents.map(Card.document).toSet(),
surveyConditionStatus: surveyConditionStatus,
);

@@ -430,23 +432,21 @@ abstract class BaseDiscoveryManager extends Cubit<DiscoveryState>
_documentCurrentViewMode[id] ?? DocumentViewMode.story;

Future<Set<Card>> _injectCustomCardsIfAny({
Set<Document>? documents,
required Set<Card> cards,
SurveyConditionsStatus? surveyConditionStatus,
}) async {
final cards = await surveyCardInjectionUseCase.singleOutput(
var nextCards = cards;

nextCards = await surveyCardInjectionUseCase.singleOutput(
SurveyCardInjectionData(
currentCards: state.cards,
nextDocuments: documents,
cards: nextCards,
status: surveyConditionStatus,
),
);

return adCardInjectionUseCase.singleOutput(
AdCardInjectionData(
currentCards: cards,
nextDocuments: documents,
),
);
nextCards = await adCardInjectionUseCase.singleOutput(nextCards);

return nextCards;
}

/// secondary observation action, check if we should implicitly like the [Document]
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ void main() {
(realInvocation) async => surveyCardInjectionUseCase
.toCards((realInvocation.positionalArguments.first
as SurveyCardInjectionData)
.nextDocuments)
.cards)
.toSet());
when(surveyCardInjectionUseCase.toCards(any)).thenAnswer((realInvocation) =>
(realInvocation.positionalArguments.first as Set<Document>? ?? const {})
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ void main() async {
(realInvocation) async => surveyCardInjectionUseCase
.toCards((realInvocation.positionalArguments.first
as SurveyCardInjectionData)
.nextDocuments)
.cards)
.toSet());
when(surveyCardInjectionUseCase.toCards(any)).thenAnswer((realInvocation) =>
(realInvocation.positionalArguments.first as Set<Document>? ?? const {})