1/* 2 * Copyright (c) 2022-2025 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\n\The length of the data received by this callback: ' + 66 JSON.stringify(AppStorage.get('dataLength') as number) + '\r\n' + 'The length of the data received: ' + 67 JSON.stringify(AppStorage.get('receiveSize') as number) + '\r\n' + 'Total length of data: ' + 68 JSON.stringify(AppStorage.get('totalSize') as number) + '\r\n' + 'Percentage: ' + 69 JSON.stringify(Math.floor((AppStorage.get('receiveSize') as number) / 70 (AppStorage.get('totalSize') as number) * 10000) / 100) + '%' 71 } else { 72 this.result = 'Failed' 73 } 74 }, 10) 75 } 76 77 build() { 78 Scroll() { 79 Column() { 80 if (!this.showPage) { 81 Text($r('app.string.configuration')) 82 .margin('2%') 83 .fontSize(28) 84 85 Row() { 86 Text(this.method) 87 .width('20%') 88 .fontSize(18) 89 .textAlign(TextAlign.Center) 90 .bindMenu(this.MenuBuilder) 91 .margin({ left: 2, right: 4 }) 92 93 TextInput({ placeholder: $r('app.string.web') }) 94 .width('75%') 95 .margin({ left: 4, right: 2 }) 96 .enableKeyboardOnFocus(false) 97 .onChange((value: string) => { 98 this.url = value 99 }) 100 .id('GET') 101 } 102 .width('95%') 103 .height('10%') 104 105 ForEach(this.list, (item: number, index: number) => { 106 Row() { 107 Text('Key: ') 108 .width('20%') 109 .fontSize(18) 110 .margin({ left: 2, right: 4 }) 111 .textAlign(TextAlign.Center) 112 TextInput({ placeholder: $r('app.string.key') }) 113 .width('76%') 114 .margin({ left: 4, right: 2 }) 115 .onChange((value: string) => { 116 this.keys[this.flag - 1] = value 117 }) 118 .id(`key${index + 1}`) 119 } 120 .width('95%') 121 .height('10%') 122 123 Row() { 124 Text('Value: ') 125 .width('20%') 126 .fontSize(18) 127 .margin({ left: 2, right: 4 }) 128 .textAlign(TextAlign.Center) 129 TextInput({ placeholder: $r('app.string.value') }) 130 .width('75%') 131 .margin({ left: 4, right: 2 }) 132 .onChange((value: string) => { 133 this.values[this.flag -1] = value 134 }) 135 .id(`value${index + 1}`) 136 } 137 .width('95%') 138 .height('10%') 139 }, (item: number) => item.toString()) 140 141 Column() { 142 Button($r('app.string.add')) 143 .margin(10) 144 .fontSize(20) 145 .width('60%') 146 .onClick(() => { 147 this.flag += 1 148 this.list = Http.setList(this.list, this.flag) 149 }) 150 .id('add') 151 152 Button($r('app.string.reduce')) 153 .margin(10) 154 .fontSize(20) 155 .width('60%') 156 .onClick(() => { 157 if (this.flag !== 1) { 158 this.flag -= 1 159 } 160 this.list = Http.setList(this.list, this.flag) 161 }) 162 .id('reduce') 163 164 Button($r('app.string.reset')) 165 .id('reset') 166 .margin(10) 167 .fontSize(20) 168 .width('60%') 169 .onClick(() => { 170 this.flag = 1 171 this.list = [0] 172 }) 173 174 Button($r('app.string.confirm')) 175 .margin(10) 176 .fontSize(20) 177 .width('60%') 178 .onClick(async () => { 179 Http.setUrl(this.url) 180 Http.setMethod(this.method) 181 Http.setExtraData(Http.setParameter(this.keys, this.values)) 182 try { 183 Http.request() 184 } catch (err) { 185 this.result = 'Failed' 186 } 187 this.showPage = !this.showPage 188 }) 189 .id('submit') 190 191 Button($r('app.string.back')) 192 .id('back') 193 .margin(10) 194 .fontSize(20) 195 .width('60%') 196 .onClick(() => { 197 router.replace({ 198 url: 'pages/Index', 199 params: { 200 url: this.url === '' ? Http.url : this.url, 201 option: Http.options 202 } 203 }) 204 }) 205 } 206 .margin({ top: '2%', bottom: '2%' }) 207 .width('100%') 208 } else { 209 Text(`${this.resultInfo} ${this.result}`) 210 .id(`${this.result === '' || this.result === 'Failed' ? 'failed' : 'succeed'}`) 211 .fontSize(20) 212 .margin('5%') 213 214 Button($r('app.string.back')) 215 .fontSize(25) 216 .onClick(() => { 217 AppStorage.setOrCreate('receiveData', 0) 218 AppStorage.setOrCreate('totalSize', 0) 219 AppStorage.setOrCreate('dataLength', 0) 220 this.url = '' 221 this.flag = 1 222 this.keys = [] 223 this.list = [0] 224 this.values = [] 225 this.result = '' 226 this.method = 'GET' 227 this.showPage = !this.showPage 228 }) 229 .id('back') 230 } 231 } 232 } 233 .width('100%') 234 .height('100%') 235 } 236}