Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 4 #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# surf_flutter_summer_school_24

Шаблонный репозиторий для Surf Flutter Summer School '24
![Screenshot_20240724_223550](https://github.com/user-attachments/assets/fa94c98e-a9d6-411a-9a06-b9aaa0d099ff)
![Screenshot_20240724_223810](https://github.com/user-attachments/assets/5a15f69d-4c85-420f-91f8-6defd15e5310)
![Screenshot_20240724_223903](https://github.com/user-attachments/assets/609be02a-7446-43c5-b17c-c344bc3f26ec)
![Screenshot_20240724_223949](https://github.com/user-attachments/assets/da60db58-081d-4d58-b473-c19fa2904b64)
![Screenshot_20240724_224044](https://github.com/user-attachments/assets/49cc926d-6b77-404a-8b93-fcb94aeb4898)
![Screenshot_20240724_224117](https://github.com/user-attachments/assets/d52cc536-1ffa-4585-8b55-bca0daeaa60c)
![Screenshot_20240724_224314](https://github.com/user-attachments/assets/6f00962d-2a56-4c8c-b696-20c0c2901f2a)
![Screenshot_20240724_224235](https://github.com/user-attachments/assets/e71b2ff2-3bb9-4f86-a861-e9bd69bfec5c)
![Screenshot_20240724_224210](https://github.com/user-attachments/assets/4c673ea4-a26d-4f1b-af6d-24fa7c085487)
Binary file added fonts/Magnolia.ttf
Binary file not shown.
20 changes: 0 additions & 20 deletions lib/main.dart

This file was deleted.

47 changes: 47 additions & 0 deletions lib/src/feature/photos/data/dowload_image_to_cload.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:convert';
import 'dart:io';

import 'package:http/http.dart' as http;

import '../domain/models/photo_entity.dart';

Future<List<PhotoEntity>> downloadImageFromYandexCloud() async {
const token = 'y0_AgAAAABZVuavAADLWwAAAAELdHsRAAAWFyiqDOJE_LPjdaheqhXn63NSWA';

final uri = Uri.https(
'cloud-api.yandex.net',
'v1/disk/resources',
{
"path": '/',
},
);

final response = await http.get(
uri,
headers: {
HttpHeaders.authorizationHeader: 'OAuth $token',
},
);

if (response.statusCode == 200) {
final body = response.body;
final json = jsonDecode(body) as Map<String, dynamic>;
final items = json['_embedded']['items'] as List<dynamic>;

return items.where((item) {
final url = item['file'] as String?;
return url != null && url.isNotEmpty;
}).map((item) {
final id = item['name'] as String;
final url = item['file'] as String;
final createAt = DateTime.tryParse(item['created']) ?? null;
return PhotoEntity(
id: id,
url: url,
createAt: createAt,
);
}).toList();
} else {
throw Exception('Ошибка получения фотографий с Яндекс.Диска');
}
}
16 changes: 16 additions & 0 deletions lib/src/feature/photos/data/photo_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import '../domain/i_photo_repository.dart';
import '../domain/models/photo_entity.dart';


import 'dowload_image_to_cload.dart';

class PhotoRepository implements IPhotoRepository {

@override
Future<List<PhotoEntity>> getPhotos() async {
final photos = await downloadImageFromYandexCloud();
return photos;
}

}

36 changes: 36 additions & 0 deletions lib/src/feature/photos/data/upload_image_to_cload.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;

Future<void> uploadImageToYandexCloud(String imagePath) async {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Эта штука тоже должна быть в репозитории

const token = 'y0_AgAAAABZVuavAADLWwAAAAELdHsRAAAWFyiqDOJE_LPjdaheqhXn63NSWA';

final uri = Uri.https(
'cloud-api.yandex.net',
'v1/disk/resources/upload',
{
"path": imagePath.split('/').last,
},
);

final response = await http.get(
uri,
headers: {
HttpHeaders.authorizationHeader: 'OAuth $token',
},
);

final body = response.body;
final json = jsonDecode(body);
json as Map<String, dynamic>;
final linkToUpload = json['href'] as String;

final dio = Dio();
final file = File(imagePath);
final formData = FormData.fromMap({
'file': await MultipartFile.fromFile(file.path),
});
await dio.put(linkToUpload, data: formData);
}
5 changes: 5 additions & 0 deletions lib/src/feature/photos/domain/i_photo_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'models/photo_entity.dart';

abstract interface class IPhotoRepository {
Future<List<PhotoEntity>> getPhotos();
}
20 changes: 20 additions & 0 deletions lib/src/feature/photos/domain/models/photo_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';

final class PhotoEntity extends Equatable {
final String id;
final String url;
final DateTime? createAt;

const PhotoEntity({
required this.id,
required this.url,
this.createAt,
});

@override
List<Object?> get props => [
id,
url,
createAt,
];
}
22 changes: 22 additions & 0 deletions lib/src/feature/theme/data/theme_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:surf_flutter_summer_school_24/src/storage/theme/theme_storage.dart';
import 'package:flutter/material.dart';

class ThemeRepository {
final ThemeStorage _themeStorage;

ThemeRepository({
required ThemeStorage themeStorage,
}) : _themeStorage = themeStorage;

Future<void> setThemeMode(
ThemeMode newThemeMode,
) async {
await _themeStorage.saveThemeMode(
mode: newThemeMode,
);
}

ThemeMode? getThemeMode() {
return _themeStorage.getThemeMode();
}
}
27 changes: 27 additions & 0 deletions lib/src/feature/theme/di/theme_inherited.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import '../domain/theme_controller.dart';

class ThemeInherited extends InheritedWidget {
const ThemeInherited({
super.key,
required this.themeController,
required super.child,
});

final ThemeController themeController;

static ThemeController? maybeOf(BuildContext context) {
return context
.dependOnInheritedWidgetOfExactType<ThemeInherited>()
?.themeController;
}

static ThemeController of(BuildContext context) {
final ThemeController? result = maybeOf(context);
assert(result != null, 'No MyThemeInherited found in context');
return result!;
}

@override
bool updateShouldNotify(ThemeInherited oldWidget) => false;
}
30 changes: 30 additions & 0 deletions lib/src/feature/theme/domain/theme_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:surf_flutter_summer_school_24/src/feature/theme/data/theme_repository.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class ThemeController {
final ThemeRepository _themeRepository;

ThemeController({
required ThemeRepository themeRepository,
}) : _themeRepository = themeRepository;

late final ValueNotifier<ThemeMode> _themeMode = ValueNotifier<ThemeMode>(
_themeRepository.getThemeMode() ?? ThemeMode.system,
);

ValueListenable<ThemeMode> get themeMode => _themeMode;

Future<void> setThemeMode(ThemeMode newThemeMode) async {
if (newThemeMode == _themeMode.value) return;
await _themeRepository.setThemeMode(newThemeMode);
_themeMode.value = newThemeMode;
}

Future<void> switchThemeMode() async {
final newThemeMode =
_themeMode.value != ThemeMode.light ? ThemeMode.light : ThemeMode.dark;
await _themeRepository.setThemeMode(newThemeMode);
_themeMode.value = newThemeMode;
}
}
37 changes: 37 additions & 0 deletions lib/src/feature/theme/ui/theme_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:surf_flutter_summer_school_24/src/feature/theme/di/theme_inherited.dart';
import 'package:flutter/material.dart';

typedef ThemeWidgetBuilder = Widget Function(
BuildContext context,
ThemeMode themeMode,
);

class ThemeBuilder extends StatefulWidget {
const ThemeBuilder({
required this.builder,
super.key,
});

final ThemeWidgetBuilder builder;

@override
State<ThemeBuilder> createState() => _ThemeBuilderState();
}

class _ThemeBuilderState extends State<ThemeBuilder> {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<ThemeMode>(
valueListenable: ThemeInherited.of(context).themeMode,
builder: (
builderContext,
themeMode,
_,
) =>
widget.builder(
builderContext,
themeMode,
),
);
}
}
50 changes: 50 additions & 0 deletions lib/src/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:surf_flutter_summer_school_24/src/feature/theme/data/theme_repository.dart';
import 'package:surf_flutter_summer_school_24/src/feature/theme/di/theme_inherited.dart';
import 'package:surf_flutter_summer_school_24/src/feature/theme/domain/theme_controller.dart';
import 'package:surf_flutter_summer_school_24/src/feature/theme/ui/theme_builder.dart';
import 'package:surf_flutter_summer_school_24/src/pages/gallery/gallery_widget.dart';
import 'package:surf_flutter_summer_school_24/src/storage/theme/theme_storage.dart';
import 'package:surf_flutter_summer_school_24/src/uikit/theme/theme_data.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
final themeStorage = ThemeStorage(
prefs: prefs,
);
final themeRepository = ThemeRepository(
themeStorage: themeStorage,
);
final themeController = ThemeController(
themeRepository: themeRepository,
);
runApp(MainApp(
themeController: themeController,
));
}

class MainApp extends StatelessWidget {
final ThemeController themeController;

const MainApp({super.key, required this.themeController,});

@override
Widget build(BuildContext context) {
return ThemeInherited(
themeController: themeController,
child: ThemeBuilder(
builder: (_, themeMode) {
return MaterialApp(
theme: AppThemeData.lightTheme,
darkTheme: AppThemeData.darkTheme,
themeMode: themeMode,
home: const GalleryWidget()
);
},
)
);
}
}

17 changes: 17 additions & 0 deletions lib/src/pages/gallery/gallery_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:elementary/elementary.dart';
import 'package:flutter/material.dart';

import '../../feature/photos/domain/i_photo_repository.dart';
import '../../feature/photos/domain/models/photo_entity.dart';

class GalleryModel extends ElementaryModel {
final IPhotoRepository photoRepository;
final ValueNotifier<List<PhotoEntity>> images = ValueNotifier([]);

GalleryModel(this.photoRepository);

Future<void> fetchPhotos() async {
final photos = await photoRepository.getPhotos();
images.value = photos;
}
}
Loading