mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 17:42:57 +00:00
refactor: remove search page inheritance from food data list
This commit is contained in:
@@ -12,27 +12,13 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {AfterViewInit, Component, ViewChild} from '@angular/core';
|
import {ChangeDetectionStrategy, Component} from '@angular/core';
|
||||||
import {MapPosition, PositionService} from '../../map/position.service';
|
import {MapPosition, PositionService} from '../../map/position.service';
|
||||||
import {SCSearchFilter, SCSearchSort} from '@openstapps/core';
|
|
||||||
import {SearchPageComponent} from './search-page.component';
|
|
||||||
import {Geolocation} from '@capacitor/geolocation';
|
import {Geolocation} from '@capacitor/geolocation';
|
||||||
|
import {BehaviorSubject, from} from 'rxjs';
|
||||||
/**
|
import {pauseWhen} from '../../../util/pause-when';
|
||||||
* Converts a position into a sort query
|
import {map, retry, startWith, take} from 'rxjs/operators';
|
||||||
*/
|
import {SCSearchFilter, SCSearchSort} from '@openstapps/core';
|
||||||
function asSortQuery(position: MapPosition): SCSearchSort[] {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
type: 'distance',
|
|
||||||
order: 'asc',
|
|
||||||
arguments: {
|
|
||||||
field: 'geo',
|
|
||||||
position: [position.longitude, position.latitude],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Presents a list of places for eating/drinking
|
* Presents a list of places for eating/drinking
|
||||||
@@ -41,9 +27,10 @@ function asSortQuery(position: MapPosition): SCSearchSort[] {
|
|||||||
selector: 'stapps-food-data-list',
|
selector: 'stapps-food-data-list',
|
||||||
templateUrl: 'food-data-list.html',
|
templateUrl: 'food-data-list.html',
|
||||||
styleUrls: ['food-data-list.scss'],
|
styleUrls: ['food-data-list.scss'],
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class FoodDataListComponent implements AfterViewInit {
|
export class FoodDataListComponent {
|
||||||
@ViewChild(SearchPageComponent) searchPage: SearchPageComponent;
|
isNotInView$ = new BehaviorSubject(true);
|
||||||
|
|
||||||
forcedFilter: SCSearchFilter = {
|
forcedFilter: SCSearchFilter = {
|
||||||
arguments: {
|
arguments: {
|
||||||
@@ -82,20 +69,43 @@ export class FoodDataListComponent implements AfterViewInit {
|
|||||||
type: 'boolean',
|
type: 'boolean',
|
||||||
};
|
};
|
||||||
|
|
||||||
sortQuery = this.positionService.getCurrentLocation({enableHighAccuracy: false}).then(asSortQuery);
|
sortQuery = this.positionService
|
||||||
|
.watchCurrentLocation({
|
||||||
|
enableHighAccuracy: false,
|
||||||
|
maximumAge: 1000,
|
||||||
|
})
|
||||||
|
.pipe(
|
||||||
|
pauseWhen(this.isNotInView$),
|
||||||
|
retry({
|
||||||
|
delay: () => from(Geolocation.checkPermissions()),
|
||||||
|
}),
|
||||||
|
map<MapPosition, SCSearchSort[]>(({longitude, latitude}) => [
|
||||||
|
{
|
||||||
|
type: 'distance',
|
||||||
|
order: 'asc',
|
||||||
|
arguments: {
|
||||||
|
field: 'geo',
|
||||||
|
position: [longitude, latitude],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
take(1),
|
||||||
|
startWith<SCSearchSort[]>([
|
||||||
|
{
|
||||||
|
arguments: {field: 'name'},
|
||||||
|
order: 'asc',
|
||||||
|
type: 'ducet',
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
constructor(private readonly positionService: PositionService) {}
|
constructor(private readonly positionService: PositionService) {}
|
||||||
|
|
||||||
async ngAfterViewInit() {
|
async ionViewWillEnter() {
|
||||||
const canAccessLocation = await Geolocation.checkPermissions()
|
this.isNotInView$.next(false);
|
||||||
.then(it => it.coarseLocation === 'granted' || it.location === 'granted')
|
}
|
||||||
.catch(() => false);
|
|
||||||
this.searchPage.showDefaultData = true;
|
ionViewWillLeave() {
|
||||||
this.searchPage.loading = true;
|
this.isNotInView$.next(true);
|
||||||
if (!canAccessLocation) {
|
|
||||||
await this.searchPage.fetchAndUpdateItems();
|
|
||||||
}
|
|
||||||
this.searchPage.sortQuery = await this.sortQuery;
|
|
||||||
await this.searchPage.fetchAndUpdateItems();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<stapps-search-page
|
<stapps-search-page
|
||||||
[forcedFilter]="forcedFilter"
|
[forcedFilter]="forcedFilter"
|
||||||
|
[sortQuery]="sortQuery | async"
|
||||||
[title]="'canteens.title'"
|
[title]="'canteens.title'"
|
||||||
[showNavigation]="false"
|
[showNavigation]="false"
|
||||||
[showDefaultData]="false"
|
[showDefaultData]="true"
|
||||||
>
|
>
|
||||||
</stapps-search-page>
|
</stapps-search-page>
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
:host {
|
||||||
|
display: contents;
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {Component, DestroyRef, inject, Input, OnInit} from '@angular/core';
|
import {Component, DestroyRef, inject, Input, OnChanges, OnInit, SimpleChanges} from '@angular/core';
|
||||||
import {ActivatedRoute, Router} from '@angular/router';
|
import {ActivatedRoute, Router} from '@angular/router';
|
||||||
import {Keyboard} from '@capacitor/keyboard';
|
import {Keyboard} from '@capacitor/keyboard';
|
||||||
import {AlertController} from '@ionic/angular';
|
import {AlertController} from '@ionic/angular';
|
||||||
@@ -45,7 +45,7 @@ import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
|||||||
styleUrls: ['search-page.scss'],
|
styleUrls: ['search-page.scss'],
|
||||||
providers: [ContextMenuService],
|
providers: [ContextMenuService],
|
||||||
})
|
})
|
||||||
export class SearchPageComponent implements OnInit {
|
export class SearchPageComponent implements OnInit, OnChanges {
|
||||||
@Input() title = 'search.title';
|
@Input() title = 'search.title';
|
||||||
|
|
||||||
@Input() placeholder = 'search.search_bar.placeholder';
|
@Input() placeholder = 'search.search_bar.placeholder';
|
||||||
@@ -139,7 +139,7 @@ export class SearchPageComponent implements OnInit {
|
|||||||
/**
|
/**
|
||||||
* Api query sorting
|
* Api query sorting
|
||||||
*/
|
*/
|
||||||
sortQuery: SCSearchSort[] | undefined;
|
@Input() sortQuery: SCSearchSort[] | undefined;
|
||||||
|
|
||||||
destroy$ = inject(DestroyRef);
|
destroy$ = inject(DestroyRef);
|
||||||
|
|
||||||
@@ -157,6 +157,12 @@ export class SearchPageComponent implements OnInit {
|
|||||||
private readonly configProvider: ConfigProvider,
|
private readonly configProvider: ConfigProvider,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
async ngOnChanges(changes: SimpleChanges) {
|
||||||
|
if ('sortQuery' in changes) {
|
||||||
|
await this.fetchAndUpdateItems();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches items with set query configuration
|
* Fetches items with set query configuration
|
||||||
* @param append If true fetched data gets appended to existing, override otherwise (default false)
|
* @param append If true fetched data gets appended to existing, override otherwise (default false)
|
||||||
|
|||||||
Reference in New Issue
Block a user