Skip to content

Commit

Permalink
Add member image
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed May 28, 2024
1 parent 408adab commit 2f5bd47
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 146 deletions.
6 changes: 6 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
Expand Down
Binary file removed assets/avatar.jpg
Binary file not shown.
42 changes: 42 additions & 0 deletions lib/controllers/member_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import 'package:application/dtos/anime_dto.dart';
import 'package:application/dtos/episode_mapping_dto.dart';
import 'package:application/dtos/member_dto.dart';
import 'package:application/utils/http_request.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MemberController {
Expand All @@ -16,13 +18,15 @@ class MemberController {
final streamController = StreamController<MemberDto>.broadcast();
String? identifier;
MemberDto? member;
int imageVersion = 0;

Future<void> init({bool afterDelete = false}) async {
if (!afterDelete) {
_sharedPreferences = await SharedPreferences.getInstance();
}

identifier = _sharedPreferences.getString('identifier') ?? await register();
imageVersion = _sharedPreferences.getInt('imageVersion') ?? 0;

try {
await login();
Expand All @@ -33,6 +37,8 @@ class MemberController {
// Move the current identifier to old identifier
final oldIdentifier = identifier;
await _sharedPreferences.remove('identifier');
await _sharedPreferences.remove('imageVersion');

await _sharedPreferences.setString('oldIdentifier', oldIdentifier!);
await init(afterDelete: true);
}
Expand Down Expand Up @@ -86,6 +92,42 @@ class MemberController {
streamController.add(member!);
}

Future<void> changeImage() async {
final result = await FilePicker.platform.pickFiles(
allowMultiple: false,
type: FileType.image,
);

if (result != null) {
final path = result.files.single.path!;

final croppedFile = await ImageCropper().cropImage(
sourcePath: path,
aspectRatioPresets: [CropAspectRatioPreset.square],
uiSettings: [
AndroidUiSettings(),
IOSUiSettings(),
],
);

final response = await HttpRequest().postMultipart(
'/v1/members/image',
member!.token,
croppedFile!.path,
);

if (response.statusCode != 200) {
throw HttpException('Failed to change image ${response.body}');
}

imageVersion++;
await _sharedPreferences.setInt('imageVersion', imageVersion);
Future.delayed(const Duration(seconds: 1), () {
streamController.add(member!);
});
}
}

Future<String> associateEmail(String email) async {
final response = await HttpRequest().post(
'/v1/members/associate-email',
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ class HttpRequest {
.timeout(_timeout);
}

Future<http.Response> postMultipart<Response>(
String endpoint,
String token,
String path,
) async {
final request = http.MultipartRequest(
'POST',
Uri.parse(
Constant.apiUrl + endpoint,
),
);

request.headers.putIfAbsent('Authorization', () => 'Bearer $token');
request.files.add(
await http.MultipartFile.fromPath(
'file',
path,
filename: path.split('/').last,
),
);

return http.Response.fromStream(await request.send()).timeout(_timeout);
}

Future<http.Response> put<Response>(
String endpoint,
String token,
Expand Down
Loading

0 comments on commit 2f5bd47

Please sign in to comment.