Add available faces to the search page

This commit is contained in:
Vishnu Mohandas 2020-04-05 17:52:38 +05:30
parent 95d41d2de4
commit 2089135720
6 changed files with 85 additions and 10 deletions

2
lib/core/constants.dart Normal file
View file

@ -0,0 +1,2 @@
const String ENDPOINT = "http://192.168.0.106:8080";
const String USER = "umbu"; // TODO: Fix me

View file

@ -0,0 +1,24 @@
import 'package:dio/dio.dart';
import 'package:logger/logger.dart';
import 'package:myapp/core/constants.dart' as Constants;
import 'models/face.dart';
class FaceSearchManager {
final _logger = Logger();
final _dio = Dio();
Future<List<Face>> getFaces() {
return _dio
.get(Constants.ENDPOINT + "/faces",
queryParameters: {"user": Constants.USER})
.then((response) => (response.data["faces"] as List)
.map((face) => new Face.fromJson(face))
.toList())
.catchError(_onError);
}
void _onError(error) {
_logger.e(error);
}
}

View file

@ -79,7 +79,6 @@ class HomeWidget extends StatelessWidget {
child: TextField(
readOnly: true,
onTap: () {
logger.i("Tapped");
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {

8
lib/models/face.dart Normal file
View file

@ -0,0 +1,8 @@
class Face {
final int faceID;
final String thumbnailURL;
Face.fromJson(Map<String, dynamic> json)
: faceID = json["faceID"],
thumbnailURL = json["thumbnailURL"];
}

View file

@ -10,12 +10,11 @@ import 'package:photo_manager/photo_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:myapp/models/photo.dart';
import 'package:myapp/core/constants.dart' as Constants;
class PhotoSyncManager {
final _logger = Logger();
final _dio = Dio();
final _endpoint = "http://192.168.0.106:8080";
final _user = "umbu";
final List<AssetEntity> _assets;
static final _lastSyncTimestampKey = "last_sync_timestamp_0";
static final _lastDBUpdateTimestampKey = "last_db_update_timestamp";
@ -113,7 +112,7 @@ class PhotoSyncManager {
} else {
var localPath = path + basename(photo.url);
await _dio
.download(_endpoint + photo.url, localPath)
.download(Constants.ENDPOINT + photo.url, localPath)
.catchError(_onError);
photo.localPath = localPath;
Photo.setThumbnail(photo);
@ -125,10 +124,11 @@ class PhotoSyncManager {
}
Future<List<Photo>> _getDiff(int lastSyncTimestamp) async {
Response response = await _dio.get(_endpoint + "/diff", queryParameters: {
"user": _user,
"lastSyncTimestamp": lastSyncTimestamp
}).catchError(_onError);
Response response = await _dio.get(Constants.ENDPOINT + "/diff",
queryParameters: {
"user": Constants.USER,
"lastSyncTimestamp": lastSyncTimestamp
}).catchError(_onError);
_logger.i(response.toString());
if (response != null) {
return (response.data["diff"] as List)
@ -142,10 +142,10 @@ class PhotoSyncManager {
Future<Photo> _uploadFile(String path, String hash) async {
var formData = FormData.fromMap({
"file": await MultipartFile.fromFile(path, filename: basename(path)),
"user": _user,
"user": Constants.USER,
});
var response = await _dio
.post(_endpoint + "/upload", data: formData)
.post(Constants.ENDPOINT + "/upload", data: formData)
.catchError(_onError);
_logger.i(response.toString());
var photo = Photo.fromJson(response.data);

View file

@ -1,6 +1,10 @@
import 'package:flutter/material.dart';
import 'package:myapp/face_search_manager.dart';
import 'package:myapp/models/face.dart';
import 'package:myapp/core/constants.dart' as Constants;
class SearchPage extends StatelessWidget {
final FaceSearchManager _faceSearchManager = FaceSearchManager();
@override
Widget build(BuildContext context) {
return Scaffold(
@ -30,6 +34,44 @@ class SearchPage extends StatelessWidget {
)
],
),
body: Container(
child: _getSearchSuggestions(),
),
);
}
Widget _getSearchSuggestions() {
return FutureBuilder<List<Face>>(
future: _faceSearchManager.getFaces(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(
height: 60,
margin: EdgeInsets.only(top: 4, left: 4),
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return _buildItem(context, snapshot.data[index]);
}),
);
} else {
return Text("Loading...");
}
},
);
}
Widget _buildItem(BuildContext context, Face face) {
return new Container(
width: 60.0,
height: 60.0,
margin: const EdgeInsets.only(right: 8),
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.contain,
image:
new NetworkImage(Constants.ENDPOINT + face.thumbnailURL))));
}
}