Skip to content

Commit

Permalink
Merge pull request #7 from LDprg/angular
Browse files Browse the repository at this point in the history
Angular
  • Loading branch information
LDprg committed Jan 20, 2023
2 parents b1876ee + 2b8e410 commit e69f2da
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/app/learn/learn.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class LearnPage implements OnInit {
search() {
this.count = Math.abs(this.count);

let query = ' ';
let query = '';
if (this.query.length > 0) query = this.query;

this.apiService.searchSets(query, this.count).then((res) => {
Expand Down
46 changes: 40 additions & 6 deletions src/app/learning/learning.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<ion-label>Answer:</ion-label>
<ion-input #correction (keyup)="enterAnswer()" [(ngModel)]="correctionText"
placeholder="Your Text"></ion-input>


<ion-button (click)="changeWrong()" color="warning" expand="full">I was right!</ion-button>
</ion-item>
</ion-col>
</ion-row>
Expand All @@ -96,21 +99,52 @@
<ion-row>
<ion-col>
<ion-item *ngFor="let p of data; let i = index">
<ion-label [color]="p.correct ? 'success' : p.wrong ? 'danger' : 'warning'">
{{p.first}} - {{p.second}}
<ion-label [color]="p.correct ? 'success' : p.wrong ? 'danger' : p.changed ? 'warning' : 'dark'"
class="ion-text-center">
<!-- {{p.first}} - {{p.second}}-->
<ion-row>
<ion-col>
{{p.first}}
</ion-col>
<ion-col>
-
</ion-col>
<ion-col>
{{p.second}}
</ion-col>
<ion-col>
<ion-col class="ion-no-padding" sizeMd="2" sizeXl="2" sizeXs="4">
<ion-button (click)="setStar(p._id)"
[color]="getStar(p._id) ? 'warning' : 'medium'"
expand="block"
fill="clear">
<ion-icon ios="star-outline" md="star-sharp"
slot="icon-only"></ion-icon>
</ion-button>
</ion-col>
</ion-col>
</ion-row>
</ion-label>
</ion-item>
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-button [routerLink]="'/set/' + id" expand="full" fill="clear" routerDirection="root">Back
to Set Page
<ion-button [routerLink]="'/set/' + id" expand="full" fill="clear" routerDirection="root">
Back to Set Page
</ion-button>
</ion-col>
<ion-col>
<ion-button (click)="again()" [routerLink]="'/learning/' + id + '/false'" expand="full"
fill="clear" routerDirection="root">
Again with all
</ion-button>
</ion-col>
<ion-col>
<ion-button (click)="again()" [routerLink]="'/learning/' + id + '/' + starred" expand="full"
fill="clear" routerDirection="root">Again
<ion-button (click)="again()" [routerLink]="'/learning/' + id + '/true'" expand="full"
fill="clear" routerDirection="root">
Again with
<ion-icon ios="star-outline" md="star-sharp" slot="end"></ion-icon>
</ion-button>
</ion-col>
</ion-row>
Expand Down
75 changes: 69 additions & 6 deletions src/app/learning/learning.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class LearningPage implements OnInit, ViewWillEnter {
public status: Status = Status.Ask;

public set: any;
public userStat: any = null;
public data: any;

public answerText: any;
Expand All @@ -36,7 +37,6 @@ export class LearningPage implements OnInit, ViewWillEnter {
}

ionViewWillEnter() {

this.apiService.getSet(this.id).then((res) => {
this.set = res;

Expand Down Expand Up @@ -65,6 +65,10 @@ export class LearningPage implements OnInit, ViewWillEnter {
}
});

this.apiService.getUserStats(this.id).then((stat) => {
this.userStat = stat;
});

this.correctCount = 0;
this.index = 0;
this.status = Status.Ask;
Expand All @@ -80,25 +84,43 @@ export class LearningPage implements OnInit, ViewWillEnter {
}

getAnswer() {
if (this.data !== undefined) {
return this.data[this.index].second;
} else {
if (this.data === undefined) {
return "";
}

return this.answerFormat(this.data[this.index].second);
}

answerFormat(text: string) {
let res = text;

res = res.replace(/[(\[{][^(\[{}\])]*[}\])]/g, ' '); // remove content in brackets
res = res.replace(/[;()\[\]{}\\/]/g, ' '); // remove special characters
res = res.replace(/(?<=[^\d]|([\d][,.][\d]))[.,]/g, ' '); // remove dots
res = res.replace(/ {2,}/g, ' '); // remove multiple spaces
res = res.trim(); // remove leading and trailing spaces

// console.log(text);
// console.log(res);
return res;
}

getAnswerText() {
let text = '';

if (this.status == Status.Ask) {
if (this.answerText === undefined) {
return "";
}
return this.answerText;
text = this.answerText;
} else {
if (this.correctionText === undefined) {
return "";
}
return this.correctionText;
text = this.correctionText;
}

return this.answerFormat(text);
}

setFocus() {
Expand Down Expand Up @@ -154,6 +176,16 @@ export class LearningPage implements OnInit, ViewWillEnter {
this.setFocus();
}

changeWrong() {
this.status = Status.Ask;
this.correctCount++;
this.data[this.index].changed = true;
this.apiService.updateUserStats(this.id, this.data[this.index]._id, "changed");
this.correctionText = "";
this.nextItem();
this.setFocus();
}

nextItem() {
this.answerText = "";

Expand All @@ -164,6 +196,37 @@ export class LearningPage implements OnInit, ViewWillEnter {
}
}

getStar(id: string) {
if (this.userStat != null && this.userStat.data != null) {
for (let i = 0; i < this.userStat.data.length; i++) {
if (this.userStat.data[i].cardid == id) {
if (this.userStat.data[i].stared != null) {
return this.userStat.data[i].stared;
} else {
return false;
}
}
}
}
return false;
}

setStar(id: string) {
this.apiService.updateUserStared(this.id, id, !this.getStar(id)).then((res) => {
if (this.userStat != null && this.userStat.data != null) {
for (let i = 0; i < this.userStat.data.length; i++) {
if (this.userStat.data[i].cardid == id) {
this.userStat.data[i].stared = !this.userStat.data[i].stared;
}
}
}

this.apiService.getUserStats(this.id).then((stat) => {
this.userStat = stat;
});
});
}

again() {
this.status = Status.Ask;
this.answerText = "";
Expand Down
26 changes: 25 additions & 1 deletion src/app/set/set.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ <h4>User: {{set.userid}}</h4>
</ion-card-header>
</ion-card>
</ion-row>
<ion-row class="flex-container">
<ion-card class="flex-item flex-item-100 flex-container">
<ion-item class="flex-item flex-item-75">
<ion-item lines="none">
<ion-text color="medium">
{{set.data.length}} Cards
</ion-text>
</ion-item>
</ion-item>

<ion-toolbar class="flex-item flex-item-25">
<ion-item>
<ion-label>Sort:&nbsp;</ion-label>
<ion-select interface="popover" [(ngModel)]="sortMode" (ionChange)="setSet(orginal)">
<ion-select-option value="norm">Normal</ion-select-option>
<ion-select-option value="statAsc">Stat. Asc.</ion-select-option>
<ion-select-option value="statDesc">Stat. Desc.</ion-select-option>
<ion-select-option value="alphaAsc">Alpha. Asc.</ion-select-option>
<ion-select-option value="alphaDesc">Alpha. Desc.</ion-select-option>
</ion-select>
</ion-item>
</ion-toolbar>
</ion-card>
</ion-row>
<ion-row>
<ion-col>
<div *ngIf="set.data.length > 0 then notEmpty else empty"></div>
Expand Down Expand Up @@ -113,7 +137,7 @@ <h4>User: {{set.userid}}</h4>
<ion-icon color="warning" ios="help-outline"
md="help-sharp"></ion-icon>
&nbsp;
{{getStat(s._id, 'skip')}}
{{getStat(s._id, 'changed')}}
</ion-label>
</ion-item>
</ion-col>
Expand Down
58 changes: 49 additions & 9 deletions src/app/set/set.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,89 @@ export class SetPage implements OnInit, ViewWillEnter {
public id!: string;

public set: any = null;

public orginal: any = null;
public userStat: any = null;
public editMode = false;
public editId = -1;

public sortMode :string= "norm";

constructor(private alertController: AlertController, private modalController: ModalController, private activatedRoute: ActivatedRoute, private apiService: ApiService) {
}

ngOnInit() {
this.id = this.activatedRoute.snapshot.paramMap.get('id') as string;
}

async sort (newItem : any) {
if (this.sortMode == "alphaAsc") {
newItem.data = await newItem.data.sort((a: any, b: any) => {
return a.first.localeCompare(b.first);
});
} else if (this.sortMode == "alphaDesc") {
newItem.data = await newItem.data.sort((a: any, b: any) => {
return b.first.localeCompare(a.first);
});
} else if (this.userStat != null && this.userStat.data != null) {
if (this.sortMode == "statAsc") {
newItem.data = await newItem.data.sort((a: any, b: any) => {
return (this.getStat(b._id, 'success') + 1) / (this.getStat(b._id, 'wrong') + 1) -
(this.getStat(a._id, 'success') + 1) / (this.getStat(a._id, 'wrong') + 1);
});
} else if (this.sortMode == "statDesc") {
newItem.data = await newItem.data.sort((a: any, b: any) => {
return (this.getStat(b._id, 'wrong') + 1) / (this.getStat(b._id, 'success') + 1) -
(this.getStat(a._id, 'wrong') + 1) / (this.getStat(a._id, 'success') + 1);
});
}
}

return await newItem;
}

ionViewWillEnter() {
this.apiService.getSet(this.id).then((set) => {
this.set = set;
this.setSet(set);

this.apiService.getUserStats(this.id).then((stat) => {
this.userStat = stat;
console.log(stat);
});
});
}

setSet(data: any){
this.orginal = data;
this.sort(data).then((newItem) => {
this.set = newItem;
});
}

status() {
return JSON.stringify(this.set);
}

canBeLearned() {
return this.set.data.length <= 0;
if (this.set != null && this.set?.data != null) {
return this.set.data.length <= 0;
}

return false;
}

getLines() {
return this.editMode ? "full" : "none";
}

newCard() {
this.set.data.push({
this.orginal.data.push({
first: "",
second: "",
});

this.apiService.updateSet(this.set.id, this.set).then((res) => {
this.apiService.updateSet(this.orginal.id, this.orginal).then((res) => {
this.apiService.getSet(this.id).then((set) => {
this.set = set;
this.setSet(set);
});
});
}
Expand All @@ -68,7 +108,7 @@ export class SetPage implements OnInit, ViewWillEnter {
if (!this.editMode) {
this.apiService.updateSet(this.set.id, this.set).then((res) => {
this.apiService.getSet(this.id).then((set) => {
this.set = set;
this.setSet(set);
});
});
}
Expand All @@ -90,7 +130,7 @@ export class SetPage implements OnInit, ViewWillEnter {
this.set.data.splice(i, 1);
this.apiService.updateSet(this.set.id, this.set).then((res) => {
this.apiService.getSet(this.id).then((set) => {
this.set = set;
this.setSet(set);
});
});
}
Expand Down Expand Up @@ -169,7 +209,7 @@ export class SetPage implements OnInit, ViewWillEnter {

this.apiService.updateSet(this.set.id, this.set).then((res) => {
this.apiService.getSet(this.id).then((set) => {
this.set = set;
this.setSet(set);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/sets/sets.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export class SetsPage implements OnInit, ViewWillEnter {
}
],
buttons: [
'Cancel',
{
text: 'Create',
handler: (input) => {
Expand All @@ -50,6 +49,7 @@ export class SetsPage implements OnInit, ViewWillEnter {
});
},
},
'Cancel',
],
}).then((alert) => {
alert.present();
Expand Down
Loading

0 comments on commit e69f2da

Please sign in to comment.