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

Никишина Маргарита #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/state/news/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { Observable } from '../../utils/observable';
import { IArticle, INewsState } from './types';

export class NewsState extends Observable implements INewsState {
private articles: IArticle[] = [];

public getArticles() {
return [];
return this.articles;
}

public setArticles(articles: IArticle[]) {
throw new Error('Not implemented');
this.articles = articles;
this.notifyObservers();
}
}
7 changes: 5 additions & 2 deletions src/state/weather/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { IMeasurement, IWeatherState } from './types';
import { Observable } from '../../utils/observable';

export class WeatherState extends Observable implements IWeatherState {
private measurements: IMeasurement[] = [];

public getMeasurements() {
return [];
return this.measurements;
}

public setMeasurements(measurements: IMeasurement[]) {
throw new Error('Not implemented');
this.measurements = measurements;
this.notifyObservers();
}
}
9 changes: 6 additions & 3 deletions src/utils/observable/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { IObservable, IObserver } from './types';

export class Observable implements IObservable {
private observers: IObserver[] = [];
public addObserver(observer: IObserver) {
throw new Error('Not implemented');
this.observers.push(observer);
}

public deleteObserver(observer: IObserver) {
throw new Error('Not implemented');
this.observers = this.observers.filter(element => element !== observer);
}

public notifyObservers() {
throw new Error('Not implemented');
this.observers.forEach(element => {
element.update(this);
});
}
}
25 changes: 23 additions & 2 deletions src/views/desktop.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { IObservable, IObserver } from '../utils/observable/types';
import { IView } from './types';
import { NewsState } from '../state/news';
import { WeatherState } from '../state/weather';
import { IArticle } from '../state/news/types';
import { IMeasurement } from '../state/weather/types';

export class DesktopView implements IObserver, IView {
private articles: IArticle[] = [];
private weathers: IMeasurement[] = [];
public update(observable: IObservable) {
throw new Error('Not implemented');
if (observable instanceof NewsState) {
this.articles = observable.getArticles().slice(-3);
}
if (observable instanceof WeatherState) {
this.weathers = observable.getMeasurements().slice(-2);
}
this.render();
}

public render() {
throw new Error('Not implemented');
let answer = '<div class="desktop">\n';
this.articles.forEach(element => {
answer = `${answer}[${element.time}] ${element.category} - ${element.title}\n`;
});
this.weathers.forEach(element => {
answer = `${answer}[${element.time}] ${element.temperature} C, ${element.pressure} P, ${
element.humidity
} U\n`;
});
console.log(`${answer}</div>`);
}
}
29 changes: 27 additions & 2 deletions src/views/mobile.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
import { IObservable, IObserver } from '../utils/observable/types';
import { IView } from './types';
import { NewsState } from '../state/news';
import { WeatherState } from '../state/weather';
import { IArticle } from '../state/news/types';
import { IMeasurement } from '../state/weather/types';

export class MobileView implements IObserver, IView {
private articles: IArticle[] = [];
private weathers: IMeasurement[] = [];
public update(observable: IObservable) {
throw new Error('Not implemented');
if (observable instanceof NewsState) {
this.articles = observable.getArticles().slice(-1);
}
if (observable instanceof WeatherState) {
this.weathers = observable.getMeasurements().slice(-1);
}
this.render();
}

public render() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Методы render и update очень похожи на реализацию из DesktopView... может реализовать их однажды, но наследоваться с разными параметрами?

throw new Error('Not implemented');
let answer = '<div class="mobile">\n';
this.articles.forEach(element => {
if (element !== undefined) {
answer = `${answer}[${element.time}] ${element.category} - ${element.title}\n`;
}
});
this.weathers.forEach(element => {
if (element !== undefined) {
answer = `${answer}[${element.time}] ${element.temperature} C, ${
element.pressure
} P, ${element.humidity} U\n`;
}
});
console.log(`${answer}</div>`);
}
}