From 4e88d7563944a29acb3b68ec37cfca0d33617f3f Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Wed, 8 Jan 2020 08:13:48 +0100 Subject: [PATCH] move common shared methods to helper --- src/component/fileListItem/view.js | 26 ++++-------------------- src/component/fileResultItem/view.js | 30 ++++------------------------ src/utils/helper.js | 22 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/component/fileListItem/view.js b/src/component/fileListItem/view.js index 34d4edb..26b4e93 100644 --- a/src/component/fileListItem/view.js +++ b/src/component/fileListItem/view.js @@ -1,7 +1,7 @@ import React from 'react'; import { normalizeURI, parseURI } from 'lbry-redux'; import { ActivityIndicator, Image, Platform, Text, TouchableOpacity, View } from 'react-native'; -import { navigateToUri, formatBytes } from 'utils/helper'; +import { navigateToUri, formatTitle, getDownloadProgress, getStorageForFileInfo } from 'utils/helper'; import Colors from 'styles/colors'; import ChannelIconItem from 'component/channelIconItem'; import channelIconStyle from 'styles/channelIcon'; @@ -21,24 +21,6 @@ class FileListItem extends React.PureComponent { url: null, }; - getStorageForFileInfo = fileInfo => { - if (!fileInfo.completed) { - const written = formatBytes(fileInfo.written_bytes); - const total = formatBytes(fileInfo.total_bytes); - return `(${written} / ${total})`; - } - - return formatBytes(fileInfo.written_bytes); - }; - - formatTitle = title => { - if (!title) { - return title; - } - - return title.length > 80 ? title.substring(0, 77).trim() + '...' : title; - }; - getDownloadProgress = fileInfo => { return Math.ceil((fileInfo.written_bytes / fileInfo.total_bytes) * 100); }; @@ -223,7 +205,7 @@ class FileListItem extends React.PureComponent { {(title || name) && ( - {this.formatTitle(title) || this.formatTitle(name)} + {formatTitle(title) || formatTitle(name)} {isRewardContent && } @@ -253,7 +235,7 @@ class FileListItem extends React.PureComponent { {fileInfo && !isNaN(fileInfo.written_bytes) && fileInfo.written_bytes > 0 && ( - {this.getStorageForFileInfo(fileInfo)} + {getStorageForFileInfo(fileInfo)} )} @@ -266,7 +248,7 @@ class FileListItem extends React.PureComponent { color={Colors.NextLbryGreen} height={3} style={fileListStyle.progress} - progress={this.getDownloadProgress(fileInfo)} + progress={getDownloadProgress(fileInfo)} /> )} diff --git a/src/component/fileResultItem/view.js b/src/component/fileResultItem/view.js index 0339532..ecf2e8c 100644 --- a/src/component/fileResultItem/view.js +++ b/src/component/fileResultItem/view.js @@ -1,7 +1,7 @@ import React from 'react'; import { normalizeURI, parseURI } from 'lbry-redux'; import { ActivityIndicator, Platform, Text, TouchableOpacity, View } from 'react-native'; -import { navigateToUri, formatBytes } from 'utils/helper'; +import { navigateToUri, formatTitle, getDownloadProgress, getStorageForFileInfo } from 'utils/helper'; import Colors from 'styles/colors'; import ChannelIconItem from 'component/channelIconItem'; import channelIconStyle from 'styles/channelIcon'; @@ -28,28 +28,6 @@ class FileResultItem extends React.PureComponent { }); } - getStorageForFileInfo = fileInfo => { - if (!fileInfo.completed) { - const written = formatBytes(fileInfo.written_bytes); - const total = formatBytes(fileInfo.total_bytes); - return `(${written} / ${total})`; - } - - return formatBytes(fileInfo.written_bytes); - }; - - formatTitle = title => { - if (!title) { - return title; - } - - return title.length > 80 ? title.substring(0, 77).trim() + '...' : title; - }; - - getDownloadProgress = fileInfo => { - return Math.ceil((fileInfo.written_bytes / fileInfo.total_bytes) * 100); - }; - onPressHandler = () => { const { autoplay, navigation, result } = this.props; const { claimId, name } = result; @@ -135,7 +113,7 @@ class FileResultItem extends React.PureComponent { {(title || name) && ( - {this.formatTitle(title) || this.formatTitle(name)} + {formatTitle(title) || formatTitle(name)} {isRewardContent && } @@ -160,7 +138,7 @@ class FileResultItem extends React.PureComponent { {fileInfo && !isNaN(fileInfo.written_bytes) && fileInfo.written_bytes > 0 && ( - {this.getStorageForFileInfo(fileInfo)} + {getStorageForFileInfo(fileInfo)} )} )} diff --git a/src/utils/helper.js b/src/utils/helper.js index bf3e938..4f57b62 100644 --- a/src/utils/helper.js +++ b/src/utils/helper.js @@ -370,3 +370,25 @@ export function uploadImageAsset(filePath, success, failure) { export function formatLbryUrlForWeb(url) { return url.replace('lbry://', '/').replace(/#/g, ':'); } + +export function getDownloadProgress(fileInfo) { + return Math.ceil((fileInfo.written_bytes / fileInfo.total_bytes) * 100); +} + +export function getStorageForFileInfo(fileInfo) { + if (!fileInfo.completed) { + const written = formatBytes(fileInfo.written_bytes); + const total = formatBytes(fileInfo.total_bytes); + return `(${written} / ${total})`; + } + + return formatBytes(fileInfo.written_bytes); +} + +export function formatTitle(title) { + if (!title) { + return title; + } + + return title.length > 80 ? title.substring(0, 77).trim() + '...' : title; +}