-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9954edf
commit e384333
Showing
9 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {Pokemon} from '../../domain/entities/Pokemon'; | ||
|
||
export interface IPokemonRepository { | ||
getAllPokemons(limit: number, offset: number): Promise<Pokemon[]>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {Pokemon} from '../../domain/entities/Pokemon'; | ||
import {fetchPokemons} from '../../infrastructure/network/ApiClient'; | ||
import {IPokemonRepository} from '../models/IPokemonRepository'; | ||
|
||
export class PokemonRepository implements IPokemonRepository { | ||
async getAllPokemons(limit: number, offset: number): Promise<Pokemon[]> { | ||
const data = await fetchPokemons(limit, offset); | ||
return data.results.map((poke: any) => new Pokemon(poke.name, poke.url)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class Pokemon { | ||
constructor(public name: string, public url: string) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {IPokemonRepository} from '../../application/models/IPokemonRepository'; | ||
import {Pokemon} from '../entities/Pokemon'; | ||
|
||
export class GetPokemonsUseCase { | ||
constructor(private pokemonRepository: IPokemonRepository) {} | ||
|
||
async execute(limit: number, offset: number): Promise<Pokemon[]> { | ||
return await this.pokemonRepository.getAllPokemons(limit, offset); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
View, | ||
Text, | ||
FlatList, | ||
ActivityIndicator, | ||
Image, | ||
StyleSheet, | ||
} from 'react-native'; | ||
import React from 'react'; | ||
import usePokemonViewModel from '../viewModels/PokemonViewModel'; | ||
import {Pokemon} from '../../domain/entities/Pokemon'; | ||
|
||
const PokemonScreen: React.FC = () => { | ||
const { | ||
pokemons, | ||
error, | ||
isFetching, | ||
isFetchingNextPage, | ||
fetchNextPage, | ||
hasNextPage, | ||
} = usePokemonViewModel(); | ||
|
||
if (isFetching && !isFetchingNextPage) { | ||
return <ActivityIndicator size="large" />; | ||
} | ||
|
||
if (error) { | ||
return <Text>Bir hata oluştu</Text>; | ||
} | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<FlatList<Pokemon> | ||
data={pokemons} | ||
keyExtractor={item => item.name} | ||
renderItem={({item}) => ( | ||
<View style={styles.cardContainer}> | ||
<Image | ||
source={{ | ||
uri: `https://img.pokemondb.net/artwork/${item.name}.jpg`, | ||
}} | ||
style={styles.image} | ||
resizeMode="contain" | ||
/> | ||
<View style={styles.textContainer}> | ||
<Text style={styles.title}>{item.name}</Text> | ||
</View> | ||
</View> | ||
)} | ||
onEndReached={() => { | ||
if (hasNextPage) { | ||
fetchNextPage(); | ||
} | ||
}} | ||
onEndReachedThreshold={0.8} | ||
ListFooterComponent={ | ||
isFetchingNextPage ? <ActivityIndicator size="large" /> : null | ||
} | ||
/> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: {flex: 1, padding: 20, backgroundColor: '#fff'}, | ||
cardContainer: { | ||
padding: 10, | ||
borderBottomWidth: 1, | ||
borderBottomColor: '#ccc', | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
}, | ||
image: {width: 100, height: 100, marginRight: 10}, | ||
title: {fontSize: 20, fontWeight: '800', textTransform: 'capitalize'}, | ||
textContainer: {gap: 5}, | ||
}); | ||
|
||
export default PokemonScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {useInfiniteQuery} from 'react-query'; | ||
import {GetPokemonsUseCase} from '../../domain/usecases/GetPokemonsUseCase'; | ||
import {PokemonRepository} from '../../application/repositories/PokemonRepository'; | ||
|
||
const pokemonRepository = new PokemonRepository(); | ||
const getPokemonsUseCase = new GetPokemonsUseCase(pokemonRepository); | ||
|
||
const fetchPokemons = async ({pageParam = 0}) => { | ||
const limit = 10; | ||
const offset = pageParam * limit; | ||
return await getPokemonsUseCase.execute(limit, offset); | ||
}; | ||
|
||
export default function usePokemonViewModel() { | ||
const { | ||
data, | ||
error, | ||
fetchNextPage, | ||
hasNextPage, | ||
isFetching, | ||
isFetchingNextPage, | ||
} = useInfiniteQuery( | ||
['pokemons'], | ||
({pageParam}) => fetchPokemons({pageParam}), | ||
{ | ||
getNextPageParam: (lastPage, allPages) => { | ||
return allPages.length; | ||
}, | ||
}, | ||
); | ||
|
||
return { | ||
pokemons: data ? data.pages.flat() : [], | ||
error, | ||
isFetching, | ||
isFetchingNextPage, | ||
fetchNextPage, | ||
hasNextPage, | ||
}; | ||
} |