Skip to content

Commit ab39f30

Browse files
committed
feat(view): add ink color view basic settings and state
1 parent 50093fc commit ab39f30

File tree

12 files changed

+121
-26
lines changed

12 files changed

+121
-26
lines changed

src/app/components/bucket/bucket.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<inkapp-magic-title [title]="bucketData.name" (changed)="onTitleChange($event)">
22
#{{index}}
33
</inkapp-magic-title>
4-
<div class="d-flex flex-wrap">
4+
<div class="d-flex flex-wrap" [ngClass]="'ink-view-'+(settings|async)?.value">
55
<ng-container *ngFor="let color of ink|async">
66
<inkapp-ink [inkData]="color" [bucketId]="bucketData._id"></inkapp-ink>
77
</ng-container>

src/app/components/bucket/bucket.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit, Input } from '@angular/core';
22
import { Store } from '@ngxs/store';
3-
import { InkBucket, Ink, InkBucketMeta, InkColorMeta } from '../../models';
3+
import { InkBucket, Ink, InkBucketMeta, InkColorMeta, InkAppView, InkAppSettings } from '../../models';
44
import { Observable } from 'rxjs';
55
import { filter, tap, map } from 'rxjs/operators';
66
import { AddInkColor, LoadInkColorsInBucket } from '../../store/actions/ink.action';
@@ -17,8 +17,11 @@ export class BucketComponent implements OnInit {
1717
@Input() index: number;
1818
@Input() bucketData: InkBucketMeta;
1919
ink: Observable<Ink>;
20+
settings: Observable<InkAppSettings>;
21+
view: InkAppView;
2022
constructor(private _store: Store, private _bucketService: BucketService, private _inkColorService: InkColorService) {
2123
this.ink = this._store.select(s => s.ink).pipe(map(x => x.filter(y => y.bucketId === this.bucketData._id)));
24+
this.settings = this._store.select(s => s.settings).pipe(map(s => s.filter(x => x.key === 'view')[0]));
2225
}
2326

2427
ngOnInit() {

src/app/ink.component.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Component, OnInit, NgZone } from '@angular/core';
22
import { Store } from '@ngxs/store';
3-
import { ChangeView } from './store/actions/settings.action';
3+
import { ChangeView, LoadSettings } from './store/actions/settings.action';
44
import { InkAppView, InkDb } from './models';
55
import { DBService } from './services/db.service';
66
import { tap } from 'rxjs/operators';
77
import { RxCollection, RxDatabase } from 'rxdb';
8+
import { LocalDatabase } from './services/localdb.service';
9+
import { SettingsService } from './services/settings.service';
810

911
@Component({
1012
selector: 'inkapp-root',
@@ -14,10 +16,12 @@ import { RxCollection, RxDatabase } from 'rxdb';
1416
`
1517
})
1618
export class InkApp implements OnInit {
17-
constructor(private store: Store, private db: DBService) {
19+
constructor(private _store: Store, private _settingsService: SettingsService) {
1820
// console.log('cons', this.db.conn);
1921
}
2022
ngOnInit() {
21-
this.store.dispatch(new ChangeView(InkAppView.ROUND));
23+
this._settingsService.setDefaultSettings().then((doc: any) => {
24+
if (doc) { this._store.dispatch(new LoadSettings(doc)); }
25+
});
2226
}
2327
}

src/app/models/ink.model.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export interface InkColorMeta {
2828
}
2929
export interface InkSettingsMeta {
3030
key: string;
31-
_id?: string;
3231
value: string;
32+
_id?: string;
3333
}
3434

3535
export type InkBucket = InkBucketMeta[];
@@ -41,9 +41,11 @@ export enum InkAppView {
4141
RECT,
4242
STRIP
4343
}
44-
export interface InkAppSettings {
45-
view: InkAppView;
46-
sortBy: string;
44+
export type InkAppSettings = InkAppSettingsItem[];
45+
46+
export interface InkAppSettingsItem {
47+
key: string;
48+
value: string;
4749
}
4850

4951
/* doc methods */

src/app/pages/home/home.component.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Store } from '@ngxs/store';
44
import { LoadBoard } from '../../store/actions/board.actions';
55
import { filter, map } from 'rxjs/operators';
66
import { InkBoard, InkBoardMeta } from '../../models';
7+
import { LocalDatabase } from '../../services/localdb.service';
78

89
@Component({
910
selector: 'inkapp-home-page',
@@ -12,7 +13,7 @@ import { InkBoard, InkBoardMeta } from '../../models';
1213
})
1314
export class HomePage implements OnInit {
1415
boards: InkBoardMeta[];
15-
constructor(private _boardService: BoardService, private _store: Store) {}
16+
constructor(private _boardService: BoardService, private _store: Store, private _localDatabase: LocalDatabase) {}
1617

1718
async ngOnInit() {
1819
this._store.select(s => s.board).subscribe(b => {
@@ -21,5 +22,13 @@ export class HomePage implements OnInit {
2122
const boards = await this._boardService.getBoards();
2223
boards.map(b => ({ _id: b._id, name: b.name, description: b.description, createdAt: b.createdAt }));
2324
this._store.dispatch(new LoadBoard(boards));
25+
26+
const db = await this._localDatabase.getDatabase();
27+
db.settings
28+
.find()
29+
.exec()
30+
.then(col => {
31+
console.log('got settings collection,', col);
32+
});
2433
}
2534
}

src/app/pages/settings/settings.component.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
<div class="col-md-8 mx-auto">
33
<h3 class="ink-page-title mb-5">
44
<i class="ink-icon icon-settings"></i> Settings</h3>
5+
<inkapp-settings-item>
6+
<div class="description">
7+
<h5>Colors view</h5>
8+
<p>Change the look of an ink color</p>
9+
</div>
10+
<div class="action">
11+
<select name="" class="custom-select" (change)="onViewChange($event.target.value)">
12+
<option value="default">Default</option>
13+
<option value="thin">Thin</option>
14+
</select>
15+
</div>
16+
</inkapp-settings-item>
517
<inkapp-settings-item>
618
<div class="description">
719
<h5>Export database</h5>
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import { Component, OnInit } from '@angular/core';
22
import { LocalDatabase } from '../../services/localdb.service';
3+
import { SettingsService } from '../../services/settings.service';
4+
import { Store } from '@ngxs/store';
5+
import { UpdateSettings, LoadSettings } from '../../store/actions/settings.action';
36

47
@Component({
58
selector: 'inkapp-settings-page',
69
templateUrl: './settings.component.html',
710
styles: []
811
})
912
export class SettingsPage implements OnInit {
10-
constructor(private _localDatabase: LocalDatabase) {}
13+
constructor(
14+
private _store: Store,
15+
private _localDatabase: LocalDatabase,
16+
private _settingsService: SettingsService
17+
) {}
1118

12-
ngOnInit() {}
19+
ngOnInit() {
20+
this._settingsService.getAll().then(doc => {
21+
this._store.dispatch(new LoadSettings(doc));
22+
});
23+
}
1324
deleteDatabase() {
1425
this._localDatabase.deleteDatabase();
1526
}
27+
28+
onViewChange(view) {
29+
this._settingsService.update('view', view).then(doc => {
30+
this._store.dispatch(new UpdateSettings('view', view));
31+
});
32+
}
1633
}

src/app/schema/settings.schema.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ export const SettingsSchema = {
55
type: 'object',
66
uniqueItems: true,
77
properties: {
8-
view: {
8+
key: {
9+
type: 'string'
10+
},
11+
value: {
912
type: 'string'
1013
}
1114
},
12-
required: []
15+
required: ['key', 'value']
1316
};

src/app/services/settings.service.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Injectable } from '@angular/core';
2+
import { LocalDatabase } from './localdb.service';
3+
4+
@Injectable({
5+
providedIn: 'root'
6+
})
7+
export class SettingsService {
8+
constructor(private _localDatabase: LocalDatabase) {}
9+
async setDefaultSettings() {
10+
const db = await this._localDatabase.getDatabase();
11+
const view = await db.settings.find({ key: 'view' }).exec();
12+
if (view.length > 1) {
13+
return db.settings.find().exec();
14+
}
15+
return db.settings.insert({ key: 'view', value: 'default' }).catch(error => {
16+
console.log('Error while settings default settings', error);
17+
});
18+
}
19+
async update(key, value) {
20+
const db = await this._localDatabase.getDatabase();
21+
return db.settings
22+
.findOne({ key })
23+
.update({ $set: { value } })
24+
.catch(error => {
25+
console.error('Error while updating settings!', error);
26+
});
27+
}
28+
29+
async getAll() {
30+
const db = await this._localDatabase.getDatabase();
31+
return db.settings.find().exec();
32+
}
33+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { InkAppView } from '../../models';
1+
import { InkAppView, InkAppSettings } from '../../models';
22

3-
export class ChangeView {
4-
static readonly type = '[Settings] change view';
5-
constructor(public view: InkAppView) {}
3+
export class LoadSettings {
4+
static readonly type = '[Settings] load all settings';
5+
constructor(public settings: InkAppSettings) {}
6+
}
7+
8+
export class UpdateSettings {
9+
static readonly type = '[Settings] update';
10+
constructor(public key: string, public value: string) {}
611
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { State, Action, StateContext } from '@ngxs/store';
22
import { InkAppSettings } from '../../models';
3-
import { ChangeView } from '../actions/settings.action';
3+
import { UpdateSettings, LoadSettings } from '../actions/settings.action';
44

55
@State<Partial<InkAppSettings>>({
66
name: 'settings',
7-
defaults: {}
7+
defaults: []
88
})
99
export class SettingsState {
10-
@Action(ChangeView)
11-
changeView(ctx: StateContext<InkAppSettings>, action: ChangeView) {
10+
@Action(UpdateSettings)
11+
updateSettings(ctx: StateContext<InkAppSettings>, action: UpdateSettings) {
1212
const state = ctx.getState();
13-
ctx.setState({
14-
...state,
15-
view: action.view
13+
const newState = state.map(s => {
14+
if (s[action.key]) {
15+
s[action.key] = action.value;
16+
}
17+
return s;
1618
});
19+
ctx.setState([...newState]);
20+
}
21+
@Action(LoadSettings)
22+
load(ctx: StateContext<InkAppSettings>, action: LoadSettings) {
23+
ctx.setState(action.settings);
1724
}
1825
}

src/styles/vendors/_bootstrap.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $grid-breakpoints: $ink--grid-breakpoints;
4848
//@import '~bootstrap/scss/dropdown';
4949
//@import '~bootstrap/scss/button-group';
5050
@import '~bootstrap/scss/input-group';
51-
//@import '~bootstrap/scss/custom-forms';
51+
@import '~bootstrap/scss/custom-forms';
5252
@import '~bootstrap/scss/nav';
5353
@import '~bootstrap/scss/navbar';
5454
@import '~bootstrap/scss/card';

0 commit comments

Comments
 (0)