1/* 2 * Copyright (c) 2024 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 */ 15export interface AnimateCallback{ 16 finish:((isPush:boolean,isExit:boolean) => void|undefined)|undefined 17 start:((isPush:boolean,isExit:boolean) => void|undefined)|undefined 18 onFinish:((isPush:boolean,isExit:boolean) => void|undefined)|undefined 19 interactive:((operation:NavigationOperation) => void|undefined)|undefined 20 timeout:(number|undefined)|undefined 21 22} 23export class CustomTransition{ 24 static delegate = new CustomTransition() 25 interactive:boolean = false; 26 proxy:NavigationTransitionProxy | undefined = undefined 27 operation:NavigationOperation = NavigationOperation.PUSH 28 29 private customTransitionMap:Map<string,AnimateCallback> = new Map() 30 31 static getInstance(){ 32 return CustomTransition.delegate; 33 } 34 35 registerNavParam( 36 name:string, 37 startCallback:(operation:boolean,isExit:boolean) => void, 38 endCallback:(operation:boolean,isExit:boolean) => void, 39 onFinish:(operation:boolean,isExit:boolean) => void, 40 interactiveCallback:(operation:NavigationOperation) => void, 41 timeout:number 42 ):void{ 43 if (this.customTransitionMap.has(name)){ 44 let param = this.customTransitionMap.get(name) 45 if (param != undefined){ 46 param.start = startCallback; 47 param.finish = endCallback; 48 param.timeout = timeout; 49 param.onFinish = onFinish 50 param.interactive = interactiveCallback 51 return 52 } 53 } 54 let params:AnimateCallback = { 55 timeout:timeout, 56 start:startCallback, 57 finish:endCallback, 58 onFinish:onFinish, 59 interactive:interactiveCallback 60 }; 61 this.customTransitionMap.set(name,params) 62 } 63 64 getAnimationId(){ 65 return Date.now(); 66 } 67 68 unRegisterNavParam(name:string):void{ 69 this.customTransitionMap.delete(name) 70 } 71 72 fireInteractiveAnimation(id:string,operation:NavigationOperation){ 73 let animation = this.customTransitionMap.get(id)?.interactive 74 if (!animation){ 75 return 76 } 77 animation(operation); 78 } 79 80 updateProgress(progress:number){ 81 if (!this.proxy?.updateTransition) { 82 return 83 } 84 progress = this.operation !== NavigationOperation.POP ? 1 - progress : progress; 85 this.proxy?.updateTransition(progress) 86 } 87 88 cancelTransition(){ 89 if (!this.proxy?.cancelTransition) { 90 return this.proxy.cancelTransition() 91 } 92 } 93 recoverState(){ 94 if (!this.proxy?.from.navDestinationId) { 95 return; 96 } 97 let param = this.customTransitionMap.get(this.proxy.from.navDestinationId); 98 if (param?.onFinish) { 99 param.onFinish(false,false) 100 } 101 let toIndex:number = this.proxy?.to.param as number; 102 if (!this.proxy.to.navDestinationId){ 103 return; 104 } 105 let toParam = this.customTransitionMap.get(this.proxy.to.navDestinationId) 106 if (toParam?.onFinish) { 107 toParam.onFinish(true,true) 108 109 } 110 } 111 finishTransition(){ 112 this.proxy?.finishTransition() 113 } 114 finishInteractiveAnimation(rate:number){ 115 if (this.operation ! = NavigationOperation.POP) { 116 if (rate > 0.5) { 117 if (this.proxy?.cancelTransition) { 118 this.proxy.cancelTransition() 119 } 120 }else { 121 this.proxy?.finishTransition() 122 } 123 }else{ 124 if (rate > 0.5) { 125 this.proxy?.finishTransition() 126 }else { 127 if (this.proxy?.cancelTransition) { 128 this.proxy.cancelTransition() 129 } 130 } 131 } 132 } 133 134 getAnimateParam(name:string):AnimateCallback{ 135 let keys = this.customTransitionMap.keys() 136 if (!this.customTransitionMap.has(name)) { 137 138 } 139 let result:AnimateCallback = { 140 start:this.customTransitionMap.get(name)?.start, 141 finish:this.customTransitionMap.get(name)?.finish, 142 timeout:this.customTransitionMap.get(name)?.timeout, 143 onFinish:this.customTransitionMap.get(name)?.onFinish, 144 interactive:this.customTransitionMap.get(name)?.interactive 145 } 146 return result 147 } 148 149 150} 151