1/* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import router from '@ohos.router' 17import Http from '../model/http' 18 19AppStorage.SetOrCreate('receiveSize', 0) 20AppStorage.SetOrCreate('totalSize', 0) 21AppStorage.SetOrCreate('dataLength', 0) 22 23@Entry 24@Component 25export struct setting { 26 private getUri: string = '' 27 private getOption?: object 28 @State url: string = '' 29 @State option?: object = undefined 30 @State flag: number = 1 31 @State keys: string[] = [] 32 @State list: number[] = [0] 33 @State values: string[] = [] 34 @State result: string = '' 35 @State method: string = 'GET' 36 @State showPage: boolean = false 37 @State resultInfo: string = '' 38 @State methods: Array<string> = ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'DELETE', 'POST', 'PUT', 'CONNECT'] 39 40 @Builder MenuBuilder() { 41 Column() { 42 ForEach(this.methods, (item: string) => { 43 Text(item) 44 .margin(5) 45 .fontSize(16) 46 .textAlign(TextAlign.Center) 47 .onClick(() => { 48 this.method = item 49 }) 50 51 Divider().height(1) 52 }, (item: string) => item.toString()) 53 } 54 .width('180vp') 55 } 56 57 aboutToAppear() { 58 this.url = this.getUri 59 this.option = this.getOption 60 Http.setUrl(this.url) 61 let context: Context = getContext(this) 62 this.resultInfo = context.resourceManager.getStringSync($r('app.string.result').id) 63 setInterval(() => { 64 if (Http.url !== '') { 65 this.result = "\r\nThe length of the data received by this callback: " + 66 JSON.stringify(AppStorage.Get('dataLength') as number) + 67 "\r\n" + "The length of the data received: " + 68 JSON.stringify(AppStorage.Get('receiveSize') as number) + "\r\n" + "Total length of data: " + 69 JSON.stringify(AppStorage.Get('totalSize') as number) + "\r\n" + "Percentage: " + 70 JSON.stringify(Math.floor((AppStorage.Get('receiveSize') as number) / 71 (AppStorage.Get('totalSize') as number) * 10000) / 100) + '%' 72 } else { 73 this.result = 'Failed' 74 } 75 }, 10) 76 } 77 78 build() { 79 Scroll() { 80 Column() { 81 if (!this.showPage) { 82 Text($r('app.string.configuration')) 83 .margin('2%') 84 .fontSize(28) 85 86 Row() { 87 Text(this.method) 88 .width('20%') 89 .fontSize(18) 90 .textAlign(TextAlign.Center) 91 .bindMenu(this.MenuBuilder) 92 .margin({ left: 2, right: 4 }) 93 94 TextInput({ placeholder: $r('app.string.web') }) 95 .width('75%') 96 .margin({ left: 4, right: 2 }) 97 .enableKeyboardOnFocus(false) 98 .onChange((value: string) => { 99 this.url = value 100 }) 101 .id('GET') 102 } 103 .width('95%') 104 .height('10%') 105 106 ForEach(this.list, (item: number, index: number) => { 107 Row() { 108 Text('Key: ') 109 .width('20%') 110 .fontSize(18) 111 .margin({ left: 2, right: 4 }) 112 .textAlign(TextAlign.Center) 113 TextInput({ placeholder: $r('app.string.key') }) 114 .width('76%') 115 .margin({ left: 4, right: 2 }) 116 .onChange((value: string) => { 117 this.keys[this.flag - 1] = value 118 }) 119 .id(`key${index + 1}`) 120 } 121 .width('95%') 122 .height('10%') 123 124 Row() { 125 Text('Value: ') 126 .width('20%') 127 .fontSize(18) 128 .margin({ left: 2, right: 4 }) 129 .textAlign(TextAlign.Center) 130 TextInput({ placeholder: $r('app.string.value') }) 131 .width('75%') 132 .margin({ left: 4, right: 2 }) 133 .onChange((value: string) => { 134 this.values[this.flag -1] = value 135 }) 136 .id(`value${index + 1}`) 137 } 138 .width('95%') 139 .height('10%') 140 }, (item: number) => item.toString()) 141 142 Column() { 143 Button($r('app.string.add')) 144 .margin(10) 145 .fontSize(20) 146 .width('60%') 147 .onClick(() => { 148 this.flag += 1 149 this.list = Http.setList(this.list, this.flag) 150 }) 151 .id('add') 152 153 Button($r('app.string.reduce')) 154 .margin(10) 155 .fontSize(20) 156 .width('60%') 157 .onClick(() => { 158 if (this.flag !== 1) { 159 this.flag -= 1 160 } 161 this.list = Http.setList(this.list, this.flag) 162 }) 163 .id('reduce') 164 165 Button($r('app.string.reset')) 166 .id('reset') 167 .margin(10) 168 .fontSize(20) 169 .width('60%') 170 .onClick(() => { 171 this.flag = 1 172 this.list = [0] 173 }) 174 175 Button($r('app.string.confirm')) 176 .margin(10) 177 .fontSize(20) 178 .width('60%') 179 .onClick(async () => { 180 Http.setUrl(this.url) 181 Http.setMethod(this.method) 182 Http.setExtraData(Http.setParameter(this.keys, this.values)) 183 try { 184 Http.request() 185 } catch (err) { 186 this.result = 'Failed' 187 } 188 this.showPage = !this.showPage 189 }) 190 .id('submit') 191 192 Button($r('app.string.back')) 193 .id('back') 194 .margin(10) 195 .fontSize(20) 196 .width('60%') 197 .onClick(() => { 198 router.replace({ 199 url: 'pages/Index', 200 params: { 201 url: this.url === '' ? Http.url : this.url, 202 option: Http.options 203 } 204 }) 205 }) 206 } 207 .margin({ top: '2%', bottom: '2%' }) 208 .width('100%') 209 } else { 210 Text(`${this.resultInfo} ${this.result}`) 211 .id(`${this.result === '' || this.result === 'Failed' ? 'failed' : 'succeed'}`) 212 .fontSize(20) 213 .margin('5%') 214 215 Button($r('app.string.back')) 216 .fontSize(25) 217 .onClick(() => { 218 AppStorage.SetOrCreate('receiveData', 0) 219 AppStorage.SetOrCreate('totalSize', 0) 220 AppStorage.SetOrCreate('dataLength', 0) 221 this.url = '' 222 this.flag = 1 223 this.keys = [] 224 this.list = [0] 225 this.values = [] 226 this.result = '' 227 this.method = 'GET' 228 this.showPage = !this.showPage 229 }) 230 .id('back') 231 } 232 } 233 } 234 .width('100%') 235 .height('100%') 236 } 237}