Code Monkey home page Code Monkey logo

simpleimagesearchapp's Introduction

Anurag's GitHub stats Hits

simpleimagesearchapp's People

Contributors

lmy6268 avatar

Watchers

 avatar

simpleimagesearchapp's Issues

feat : 클린 아키텍쳐 적용하기

Clean Architecture

도입

  • 좀 더 유지보수가 용이한 코드를 어떻게 만드냐에 대한 고민
  • 갑자기 등장한 것이 아닌, 다른 프레임워크에서도 흔히 사용되는 구조

개념

  • 3가지 레이어로 구분 ( Data, Domain, Presentation )

Presentation Layer

  • View, ViewModel 등이 위치하는 곳
  • 사용자와 직접 Interaction 하는 레이어

[비즈니스 로직 파트] -> 프로젝트 내, Pixabay Api 부분

Domain Layer

  • Use Case, Repository (Interface) , Entity (모델 class)
  • 추상적인 개념의 레이어 ( 일종의 중재자 역할 )

Data Layer

  • 실제 데이터를 공급하는 레이어
  • Repository (Implements)

장점

  • 레이어를 분리함으로서, 코드를 분석하고 찾는데 쉬워진다 -> 유지보수의 용이

feat : 에러 처리하기

에러 처리

도입

  • 현재 앱은 기본적으로는 오류가 발생하지 않는 것처럼 보인다.
  • 인터넷이 연결되어있지 않을 때, 어떻게 처리할 지 고민해보자.

개념

  • 현재 앱은 클린 아키텍쳐로 구성되어있고, Data 레이어에서 발생한 에러를 어떻게 Presentation 레이어로 전달할 수 있는가..?

방법

  • 이럴 땐 보통 Result 라는 클래스를 별도로 생성해서 처리하는 경우가 많다.
  • Result Class
    • 결과를 담는 객체
    • 성공 시 필요한 데이터를, 실패 시 에러 데이터를 담아 전달한다.
    • Sealed Class 느낌으로 구성한다.
  • Enum vs Sealed
    • Enum과 달리 Sealed는 실제 데이터를 담을 수 있음.
  • freezed로 쉽게 만들어볼 수 있음.

Freezed 이용해서 만들어보기

진행

  1. Live Template 만들기
import 'package:freezed_annotation/freezed_annotation.dart';
part '$_NAME$.freezed.dart';

@freezed
abstract class $NAME$ <T> with _$$$NAME$<T>{
    const factory $NAME$.success(T data) = Success;
    const factory $NAME$.error(Exception e) = Error;
} 
  1. dart run build_runner build 를 터미널에 입력해서, Code Generate 진행하기.

StreamController를 이용해, 에러메시지를 SnackBar로 띄우기

  1. HomeUiEvent 클래스를 만들어, 결과값을 가지도록 한다.
  2. 결과값이 가지고 있는 메시지를 Stream을 통해, 외부로 노출시킨다.
  3. Stream을 통해, 메시지를 전달받은 HomeScreen에서 메시지를 스낵바의 형태로 띄워준다.

feat: 로딩처리 구현하기

문제 상황

데이터를 가져오는데 있어, 로딩 중인 것을 사용자에게 알려주어야 하는데, 현재는 그런 화면이 보이지 않는다. 이를 해결해야 한다.

해결과정

[1] isLoading 변수를 만든 후, 해당 변수의 값에 따라 로딩여부를 파악한다.

  1. 외부에서 isLoading 변수의 값을 변경할 수 없도록, _isLoading을 내부에 선언하고, isLoading값은 _isLoading 값을 get만 할 수 있도록 제한하여, 안전성을 확보한다.
  2. 이후 해당 값을 이용하여, 로딩여부를 화면에 표시한다.
  • 단, _isLoading 값 변경 시, 외부의 ChangeNotifierProvider에 자식요소에서 변경사항을 인식할 수 있도록, notifyListeners()를 사용하여, 변경사항을 반영한다.

다음과 같이 할 경우의 문제점

image

=> 위와 같이 값이 변경될 때마다, 매번 notifyListeners() 을 호출해주어야 한다.

[2] 내부의 변경되는 모든 변수의 상태를 모두 담은 별도의 변수를 만들어 관리한다.

  • freezed를 이용하여, 상태를 다루는 클래스를 생성한다.
import '../../domain/model/photo.dart';

import 'package:json_annotation/json_annotation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'home_state.g.dart';

part 'home_state.freezed.dart';

@freezed
class HomeState with _$HomeState {
  factory HomeState(
    List<Photo> photos,
    bool isLoading,
  ) = _HomeState;

  factory HomeState.fromJson(Map<String, dynamic> json) =>
      _$HomeStateFromJson(json);
}
-  해당 객체는 현재 불변객체로 여겨진다. 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.