1- import { useEffect , useState } from "react" ;
2- import { StyleSheet , View , Alert , Dimensions , FlatList } from "react-native" ;
3- import { supabase } from "../../lib/initSupabase" ;
1+ import { StyleSheet , View , Dimensions , FlatList } from "react-native" ;
42import { Text } from '@rneui/themed' ;
3+ import { Button } from "@rneui/base" ;
4+ import { useParagraphs } from '../../hooks/useParagraphs' ;
5+ import { useReaderInfo } from "../../hooks/useReaderInfo" ;
6+ import { useUser } from "../UserContext" ;
7+ import { useCallback , useEffect , useRef , useState } from "react" ;
8+ import { LinearProgress } from "@rneui/base" ;
9+ import { useProgress } from "../../hooks/useProgress" ;
510
611export type Paragraph = {
712 num : number
813 paragraph : string
914 paragraph_length : number
1015 }
16+ //this must be a child belonging to another Screen, for better naming conventions.
17+ export const ParagraphCalc = ( { route, navigation} : any ) => {
18+ const { user} = useUser ( )
1119
12- export const ParagraphCalc = ( ) => {
20+ //progress: this comes from the goal column
21+ const dailyGoal = useProgress ( user ! ) || 0 ;
22+ //RecentBooks: this would arrive as a prop from that component
23+ const { book_num} = route ?. params || 0 ;
1324
14- /**
15- * Create a function that will find the average length of a book's paragraphs
16- * and we can do some of the same calculations under the hood again
17- */
25+ const { updateBookmark, getSingleProgress} = useReaderInfo ( user ! ) ;
26+ const start = getSingleProgress ( book_num ) ?. progress || 0 ;
27+ const { displayedPara} = useParagraphs ( book_num , dailyGoal , start ) ;
28+
29+ const [ paragraphsRead , setParagraphsRead ] = useState ( start ) ;
30+ const [ paragraphPerc , setParagraphPerc ] = useState ( start / displayedPara . length ) ;
1831
19- const start = 0 ;
20- const goal = 40 ;
21- const book_num = 33 ;
22- const [ paragraphs , setParagraphs ] = useState < Array < Paragraph > > ( [ ] )
23- const [ displayedPara , setDisplayedPara ] = useState < Array < Paragraph > > ( [ ] ) ;
24- const [ numPara , setNumPara ] = useState < number > ( 0 ) ;
32+ useEffect ( ( ) => {
33+ const p = ( paragraphsRead / displayedPara . length ) ;
34+ setParagraphPerc ( p ) ;
35+ } , [ paragraphsRead ] )
2536
26- useEffect ( ( ) => {
27- if ( book_num ) {
28- getParagraphs ( )
29- console . log ( 1 )
30- }
31- } , [ ] )
3237
33- useEffect ( ( ) => {
34- const avg = Math . round ( paragraphs . map ( p => p . paragraph )
35- . map ( para => para . split ( ' ' ) . length )
36- . reduce ( ( a , b ) => a + b , 0 ) / paragraphs . length )
37- const num = Math . round ( ( 250 * goal ) / avg ) ;
38- setNumPara ( num ) ;
39- console . log ( 2 )
40- } , [ paragraphs ] ) ;
38+ //https://reactnavigation.org/docs/preventing-going-back
39+ //we can use this to save progress if the user tries leaving the screen early.
4140
42- useEffect ( ( ) => {
43- const displayed = paragraphs . slice ( start , numPara ) ;
44- setDisplayedPara ( displayed ) ;
45- console . log ( 3 )
46- } , [ numPara ] )
47-
48- async function getParagraphs ( ) {
49- try {
50- let { data, error} = await supabase
51- . from ( 'paragraphs' )
52- . select ( '*' )
53- . eq ( 'num' , book_num )
54- if ( error ) {
55- throw error
56- }
57- if ( data ) {
58- setParagraphs ( data )
59- }
60- } catch ( error ) {
61- if ( error instanceof Error ) {
62- Alert . alert ( error . message )
63- }
64- }
65- }
66-
67- const renderText = ( { item, index} : any ) => {
41+ const renderText = ( { item} : any ) => {
6842 return (
6943 < View style = { styles . snippet } >
7044 < Text style = { styles . para } > { item . paragraph } </ Text >
7145 </ View >
7246 )
7347 }
48+
49+ const handleFinish = ( ) => {
50+ updateBookmark ( book_num , paragraphsRead , paragraphPerc ) ;
51+ }
52+ const viewabilityConfig = {
53+ viewAreaCoveragePercentThreshold : 50 ,
54+ }
55+
56+ const onViewableItemsChanged = useCallback ( ( viewableItem : any ) => {
57+ if ( viewableItem . viewableItems [ 0 ] . index ) {
58+ setParagraphsRead ( viewableItem . viewableItems [ 0 ] . index ) ;
59+ }
60+ } , [ ] ) ;
61+
62+ const viewabilityConfigCallbackPairs = useRef ( [ { viewabilityConfig, onViewableItemsChanged } ] )
63+
64+
7465 return (
7566 < View style = { styles . snippetList } >
76- < FlatList pagingEnabled = { true } data = { displayedPara } renderItem = { ( item ) => renderText ( item ) } />
67+ < FlatList pagingEnabled = { true }
68+ viewabilityConfig = { viewabilityConfig }
69+ viewabilityConfigCallbackPairs = { viewabilityConfigCallbackPairs . current }
70+ data = { displayedPara } renderItem = { renderText } />
71+ < LinearProgress value = { paragraphPerc } variant = { "determinate" } trackColor = { 'lightgreen' } color = { 'green' } />
72+ < Button onPress = { handleFinish } > Save Progress</ Button >
7773 </ View >
7874 )
7975}
@@ -92,6 +88,7 @@ const styles = StyleSheet.create({
9288 height : Dimensions . get ( "window" ) . height ,
9389 justifyContent : 'center' ,
9490 alignItems : "center" ,
91+ paddingTop : 16 ,
9592 } ,
9693 viewerText : {
9794 fontSize : 16 ,
0 commit comments