1
- import React , { Component , useState } from "react" ;
1
+ import React , { Component } from "react" ;
2
2
import {
3
3
StyleSheet ,
4
4
View ,
9
9
Switch ,
10
10
} from "react-native" ;
11
11
12
+ import AsyncStorage from "@react-native-async-storage/async-storage" ;
12
13
import { Table , TableWrapper , Row , Cell } from "react-native-table-component" ;
14
+
13
15
export default class InputTable extends Component {
14
16
constructor ( props ) {
15
17
super ( props ) ;
@@ -33,6 +35,7 @@ export default class InputTable extends Component {
33
35
isAnimationDone : false ,
34
36
isIoEnabled : false ,
35
37
time_Quantam : 2 , // Time Quantam , Initially '2' (Added for Round robin Algorithm)
38
+ from_history : false , // If Input Data comes from history
36
39
} ;
37
40
this . addProcess = this . addProcess . bind ( this ) ;
38
41
this . deleteProcess = this . deleteProcess . bind ( this ) ;
@@ -43,7 +46,101 @@ export default class InputTable extends Component {
43
46
this . timeQuantamTextInput = this . timeQuantamTextInput . bind ( this ) ;
44
47
this . toggleButton = this . toggleButton . bind ( this ) ;
45
48
}
49
+
50
+ checkForStorageKey = async ( ) => {
51
+ try {
52
+ const jsonValue = await AsyncStorage . getItem ( "storage_key" ) ;
53
+
54
+ if ( jsonValue != null ) {
55
+ } else {
56
+ AsyncStorage . setItem ( "storage_key" , JSON . stringify ( 0 ) ) ;
57
+ }
58
+ } catch ( e ) {
59
+ console . log ( "Couldn't Check For The Storage_Key Value" ) ;
60
+ console . log ( e ) ;
61
+ }
62
+ } ;
63
+
64
+ componentDidMount ( ) {
65
+ if ( this . props . from_history == true ) {
66
+ // If the Input comes from History
67
+ var history_data = this . props . inpt_data ;
68
+ var state = this . state ;
69
+ state . tableData = history_data [ "tableData" ] ;
70
+ state . tableHead = history_data [ "tableHead" ] ;
71
+ state . totalProcess = history_data [ "totalProcess" ] ;
72
+ state . processes = history_data [ "processes" ] ;
73
+ state . from_history = true ;
74
+ state . isIoEnabled = history_data [ "isIoEnabled" ] ;
75
+ state . time_Quantam = history_data [ "time_Quantam" ] ;
76
+ this . setState ( { state } ) ;
77
+ // console.log(this.state);
78
+ }
79
+ this . checkForStorageKey ( ) ; // If there is not storage key Place a storage Key
80
+ }
81
+ getDate = ( ) => {
82
+ let today = new Date ( ) ;
83
+ var date =
84
+ today . getFullYear ( ) +
85
+ "-" +
86
+ ( today . getMonth ( ) + 1 ) +
87
+ "-" +
88
+ today . getDate ( ) ;
89
+ var time =
90
+ today . getHours ( ) + ":" + today . getMinutes ( ) + ":" + today . getSeconds ( ) ;
91
+ today = date + " @ " + time ;
92
+
93
+ return today ;
94
+ } ;
95
+
96
+ storeData = async ( id , data ) => {
97
+ var inpt_data = data ;
98
+ if ( id != "storage_key" ) {
99
+ inpt_data = { ...inpt_data , date : this . getDate ( ) } ;
100
+ }
101
+
102
+ try {
103
+ const jsonValue = id != "storage_key" ? JSON . stringify ( inpt_data ) : data ;
104
+ // console.log(jsonValue);
105
+ await AsyncStorage . setItem ( id , jsonValue ) ;
106
+ // return jsonValue;
107
+ } catch ( e ) {
108
+ console . log ( "Couldn't Store the Value" ) ;
109
+ console . log ( e ) ;
110
+ }
111
+ } ;
112
+
46
113
getAnswer = ( ) => {
114
+ if ( this . props . from_history != true ) {
115
+ var inpt_data = {
116
+ algorithm : this . props . algorithm ,
117
+ totalProcess : this . state . totalProcess ,
118
+ tableHead : this . state . tableHead ,
119
+ tableData : this . state . tableData ,
120
+ processes : this . state . processes ,
121
+ isIoEnabled : this . state . isIoEnabled ,
122
+ time_Quantam : this . state . time_Quantam ,
123
+ } ;
124
+ var current_storage_key ;
125
+
126
+ AsyncStorage . getItem ( "storage_key" , ( err , value ) => {
127
+ if ( err ) {
128
+ console . log ( err ) ;
129
+ } else {
130
+ current_storage_key = JSON . parse ( value ) ;
131
+ inpt_data = { ...inpt_data , date : this . getDate ( ) } ; // Store the Date
132
+ AsyncStorage . setItem (
133
+ current_storage_key . toString ( ) ,
134
+ JSON . stringify ( inpt_data )
135
+ ) ; // Store the Data
136
+ AsyncStorage . setItem (
137
+ "storage_key" ,
138
+ JSON . stringify ( current_storage_key + 1 )
139
+ ) ; // Update the Storage Key
140
+ }
141
+ } ) ;
142
+ }
143
+
47
144
( this . state . currentCpuPID = "IDLE" ) ,
48
145
( this . state . currentQueuePID = [ ] ) ,
49
146
( this . state . currentSecond = 0 ) ,
@@ -52,14 +149,15 @@ export default class InputTable extends Component {
52
149
this . state . avgtat = parseFloat ( this . state . avgtat ) . toFixed ( 2 ) ;
53
150
this . state . avgwaiting = parseFloat ( this . state . avgwaiting ) . toFixed ( 2 ) ;
54
151
} ;
152
+
55
153
showAnimation ( ) {
56
154
setTimeout ( ( ) => {
57
155
let total_seconds = this . state . ganntChartArray . length ;
58
156
if ( total_seconds == this . state . currentSecond ) {
59
157
this . state . isAnimationDone = true ;
60
158
}
61
159
62
- console . log ( this . state . currentSecond ) ;
160
+ // console.log(this.state.currentSecond);
63
161
var temp_cpu_id = this . state . ganntChartArray [ this . state . currentSecond ] ;
64
162
if ( temp_cpu_id == "/" ) temp_cpu_id = "IDLE" ;
65
163
this . setState ( {
@@ -71,9 +169,11 @@ export default class InputTable extends Component {
71
169
} ) ;
72
170
} , 2000 ) ;
73
171
}
172
+
74
173
animationCompleted ( ) {
75
174
alert ( "Process Completed!" ) ;
76
175
}
176
+
77
177
ganntChart ( ) {
78
178
if ( this . state . ganntChartArray . length == 0 ) {
79
179
alert ( "No Processes!" ) ;
@@ -481,6 +581,7 @@ export default class InputTable extends Component {
481
581
</ View >
482
582
) ;
483
583
}
584
+
484
585
showAnswer ( ) {
485
586
if ( this . state . gotAnswer == true ) {
486
587
return (
@@ -540,6 +641,7 @@ export default class InputTable extends Component {
540
641
) ;
541
642
}
542
643
}
644
+
543
645
addProcess ( ) {
544
646
const state = this . state ;
545
647
( state . currentCpuPID = "IDLE" ) ,
@@ -558,6 +660,7 @@ export default class InputTable extends Component {
558
660
this . state . ganntChartArray = [ ] ;
559
661
this . setState ( { state } ) ;
560
662
}
663
+
561
664
deleteProcess ( ) {
562
665
const state = this . state ;
563
666
if ( state . totalProcess == - 1 ) {
@@ -576,6 +679,7 @@ export default class InputTable extends Component {
576
679
this . state . ganntChartArray = [ ] ;
577
680
this . setState ( { state } ) ;
578
681
}
682
+
579
683
toggleButton ( ) {
580
684
const toggleSwitch = ( ) => {
581
685
var isIoEnabled = this . state . isIoEnabled ;
@@ -614,6 +718,7 @@ export default class InputTable extends Component {
614
718
}
615
719
}
616
720
totalProcess = tableData . length - 1 ;
721
+ // processes = ["P0"];
617
722
this . setState ( {
618
723
isIoEnabled,
619
724
tableHead,
@@ -691,7 +796,11 @@ export default class InputTable extends Component {
691
796
const element = ( index , cellIndex ) => (
692
797
< TextInput
693
798
editable = { true }
694
- placeholder = { "edit" }
799
+ placeholder = {
800
+ this . state . from_history
801
+ ? this . state . tableData [ index ] [ cellIndex ] . toString ( )
802
+ : "edit"
803
+ }
695
804
onChangeText = { ( text ) => ( this . state . tableData [ index ] [ cellIndex ] = text ) }
696
805
keyboardType = "number-pad"
697
806
style = { {
0 commit comments