Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosume committed Dec 6, 2019
1 parent 46f7f0e commit 0d8d19f
Show file tree
Hide file tree
Showing 22 changed files with 371 additions and 128 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
PODS:
- Flutter (1.0.0)
- path_provider (0.0.1):
- Flutter
- shared_preferences (0.0.1):
- Flutter

DEPENDENCIES:
- Flutter (from `.symlinks/flutter/ios`)
- path_provider (from `.symlinks/plugins/path_provider/ios`)
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)

EXTERNAL SOURCES:
Flutter:
:path: ".symlinks/flutter/ios"
path_provider:
:path: ".symlinks/plugins/path_provider/ios"
shared_preferences:
:path: ".symlinks/plugins/shared_preferences/ios"

SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
path_provider: fb74bd0465e96b594bb3b5088ee4a4e7bb1f2a9d
shared_preferences: 430726339841afefe5142b9c1f50cb6bd7793e01

PODFILE CHECKSUM: b6a0a141693093b304368d08511b46cf3d1d0ac5
Expand Down
6 changes: 3 additions & 3 deletions lib/apis/Api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Api {
static final client = new http.Client();
static String _baseUrl = 'https://gin-drive.herokuapp.com';

static Future<Map<String, String>> _getHeaders() async {
static Future<Map<String, String>> getHeaders() async {
String _token = await userRepository.getToken();
if (_token != null) {
return {'Authorization': 'Bearer ' + _token};
Expand All @@ -14,12 +14,12 @@ class Api {
}

static Future<http.Response> post(path, {body}) async {
var headers = await _getHeaders();
var headers = await getHeaders();
return client.post(_baseUrl + path, body: body, headers: headers);
}

static Future<http.Response> get(path) async {
var headers = await _getHeaders();
var headers = await getHeaders();
return client.get(_baseUrl + path, headers: headers);
}
}
17 changes: 17 additions & 0 deletions lib/apis/FileApiProvider.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter_app/apis/Api.dart';
import 'package:path_provider/path_provider.dart';

class FileApiProvider {
Dio _dio = new Dio();

Future<List<dynamic>> load() {
return Api.get('/files').then((res) {
if (res.statusCode == 200) {
Expand All @@ -11,6 +16,18 @@ class FileApiProvider {
throw new Exception('Fail');
});
}

Future<String> download(String id, String originalName) async {
String dir = (await getApplicationDocumentsDirectory()).path;
String file = '$dir/$originalName';
print(file);
return _dio
.download('https://gin-drive.herokuapp.com/files/download/' + id, file,
options: Options(headers: await Api.getHeaders()))
.then((res) {
return file;
});
}
}

FileApiProvider fileApiProvider = FileApiProvider();
15 changes: 13 additions & 2 deletions lib/apis/UserApiProvider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:convert';

import 'package:flutter_app/apis/Api.dart';
import 'package:flutter_app/models/UserModel.dart';
import 'package:http/http.dart' as http;

class UserApiProvider {
Future<String> login(String username, String password) async {
Expand All @@ -11,7 +10,19 @@ class UserApiProvider {
if (response.statusCode == 200) {
return json.decode(response.body)['data'];
}
throw new Exception('Login failure');
throw new Exception('Username or password is invalid');
});
}

Future<bool> register(String username, String password) async {
return Api.post('/users/register',
body: {'username': username, 'password': password}).then((res) {
Map<String, dynamic> body = json.decode(res.body);
print(body);
if (res.statusCode == 200) {
return true;
}
throw new Exception(body['data']);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/blocs/AuthenticationBloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AuthenticationBloc
}
}

if (event is OnLogin) {
if (event is OnLoginPage) {
yield AuthenticationUnauthenticated();
}

Expand All @@ -48,7 +48,7 @@ class AuthenticationBloc
yield AuthenticationUnauthenticated();
}

if (event is OnRegister) {
if (event is OnRegisterPage) {
yield AuthenticationRegister();
}

Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions lib/blocs/LoginBloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
yield LoginLoading();

try {

authenticationBloc.add(OnAuthenticationLoading());
final token = await userRepository.authenticate(
username: event.username,
password: event.password,
Expand Down
23 changes: 23 additions & 0 deletions lib/blocs/RegisterBloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:bloc/bloc.dart';
import 'package:flutter_app/blocs/events/RegisterEvent.dart';
import 'package:flutter_app/blocs/states/RegisterState.dart';
import 'package:flutter_app/resources/UserRepository.dart';

class RegisterBloc extends Bloc<RegisterEvent, RegisterState> {
@override
RegisterState get initialState => OnRegisterState();

@override
Stream<RegisterState> mapEventToState(RegisterEvent event) async* {
if (event is OnRegister && event.username.isNotEmpty) {
yield OnRegisteringState();
await Future.delayed(Duration(seconds: 1));
try {
await userRepository.register(event.username, event.password);
yield OnRegisterSuccessfulState();
} catch (e) {
yield OnRegisterFailureState(e.message);
}
}
}
}
2 changes: 1 addition & 1 deletion lib/blocs/builders/FileBuilder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_app/blocs/events/FileBloc.dart';
import 'package:flutter_app/blocs/FileBloc.dart';
import 'package:flutter_app/blocs/events/FileEvent.dart';
import 'package:flutter_app/blocs/states/FileState.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down
19 changes: 19 additions & 0 deletions lib/blocs/builders/RegisterBuilder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter_app/blocs/RegisterBloc.dart';
import 'package:flutter_app/blocs/events/RegisterEvent.dart';
import 'package:flutter_app/blocs/states/RegisterState.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

registerCreator(child) {
return BlocProvider(
create: (context) {
return RegisterBloc()..add(OnRegister());
},
child: child,
);
}

registerBuilder(builder) {
return BlocBuilder<RegisterBloc, RegisterState>(
builder: builder,
);
}
4 changes: 2 additions & 2 deletions lib/blocs/events/AuthenticationEvent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class OnLoggedIn extends AuthenticationEvent {

class OnLoggedOut extends AuthenticationEvent {}

class OnRegister extends AuthenticationEvent {}
class OnRegisterPage extends AuthenticationEvent {}

class OnLogin extends AuthenticationEvent {}
class OnLoginPage extends AuthenticationEvent {}

class OnAuthenticationLoading extends AuthenticationEvent {}
20 changes: 20 additions & 0 deletions lib/blocs/events/RegisterEvent.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';

abstract class RegisterEvent extends Equatable {
@override
List<Object> get props => [];
}

class OnRegistering extends RegisterEvent {}

class OnRegister extends RegisterEvent {
final String username;
final String password;

OnRegister({this.username = '', this.password = ''});

@override
List<Object> get props => [username, password];
}

class OnRegisterFailure extends RegisterEvent {}
20 changes: 20 additions & 0 deletions lib/blocs/states/RegisterState.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';

abstract class RegisterState extends Equatable {
@override
List<Object> get props => [];
}

class OnRegisterState extends RegisterState {}

class OnRegisteringState extends RegisterState {}

class OnRegisterFailureState extends RegisterState {
final String message;

OnRegisterFailureState(this.message);

List<Object> get props => [message];
}

class OnRegisterSuccessfulState extends RegisterState {}
74 changes: 58 additions & 16 deletions lib/components/FileList.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/apis/FileApiProvider.dart';
import 'package:flutter_app/blocs/FileBloc.dart';
import 'package:flutter_app/blocs/builders/FileBuilder.dart';
import 'package:flutter_app/blocs/events/FileEvent.dart';
import 'package:flutter_app/blocs/states/FileState.dart';
import 'package:flutter_app/components/LoadingIndicator.dart';
import 'package:flutter_app/models/FileModel.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';

class FileList extends StatefulWidget {
Expand All @@ -17,22 +21,55 @@ class _State extends State<FileList> {
buildList() {}

Widget buildItem(FileModel model) {
return ListTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(model.metadata.originalname),
Text(
model.length.toString() + ' bit',
style: TextStyle(fontSize: 14.0),
)
],
),
subtitle: Text(
DateFormat("HH:mm:ss dd-MM-yyyy")
.format(DateTime.parse(model.uploadDate).add(Duration(hours: 7))),
style: TextStyle(fontSize: 12.0),
return Dismissible(
child: ListTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(model.metadata.originalname),
Text(
model.length.toString() + ' bit',
style: TextStyle(fontSize: 14.0),
)
],
),
subtitle: Text(
DateFormat("HH:mm:ss dd-MM-yyyy")
.format(DateTime.parse(model.uploadDate).add(Duration(hours: 7))),
style: TextStyle(fontSize: 12.0),
),
),
key: Key(model.id),
confirmDismiss: (DismissDirection dismissDirection) async {
print(dismissDirection);
if (dismissDirection == DismissDirection.startToEnd) {
fileApiProvider
.download(model.id, model.metadata.originalname)
.then((path) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(
'Your file is saved at: $path',
style: Theme.of(context).textTheme.button,
),
backgroundColor: Colors.blue,
),
);
}).catchError((er) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text(
'An error occurred: ${er.message}',
style: Theme.of(context).textTheme.button,
),
backgroundColor: Colors.red,
),
);
});
}
return false;
},
onDismissed: (DismissDirection dismissDirection) {},
);
}

Expand All @@ -50,7 +87,12 @@ class _State extends State<FileList> {
return buildItem(item);
}).toList();
print(wids);
return ListView(children: wids);
return RefreshIndicator(
onRefresh: () async {
print('reload');
BlocProvider.of<FileBloc>(context).add(OnLoadFile());
},
child: ListView(children: wids));
}
return LoadingIndicator();
}));
Expand Down
Loading

0 comments on commit 0d8d19f

Please sign in to comment.