CodePush自定義更新彈框及下載進度條

来源:https://www.cnblogs.com/guangqiang/archive/2018/09/05/9589404.html
-Advertisement-
Play Games

CodePush 熱更新之自定義更新彈框及下載進度 先來幾張彈框效果圖 非強制更新場景 image 強制更新場景 image 更新包下載進度效果 image 非強制更新場景 image image 強制更新場景 image image 更新包下載進度效果 image image 核心代碼 這裡的熱更 ...


CodePush 熱更新之自定義更新彈框及下載進度

先來幾張彈框效果圖

  • 非強制更新場景


    image
    image
  • 強制更新場景


    image
    image
  • 更新包下載進度效果


    image
    image

核心代碼

這裡的熱更新Modal框,是封裝成一個功能獨立的組件來使用的,需不需要更新以及是否為強制更新等邏輯均在組件內實現

image
image

UpdateComp 熱更新組件核心代碼如下:

/**
 * Created by guangqiang on 2018/3/29.
 */
import React, {Component} from 'react'
import {View, Text, StyleSheet, Modal, TouchableOpacity, Image} from 'react-native'
import Progress from './index'
import {GlobalStyles} from '../../../constants/GlobalStyles'
import {deviceInfo} from "../../../constants/DeviceInfo"
import {Icon} from '../../../utils/iconFont'
import CodePush from "react-native-code-push"
import {Toast} from "../../../utils/toast"

const CODE_PUSH_KEY = 'jE39cjdnkzqfpXgRylPXDDNkEzJm3ac740b8-b071-474f-afbf-369c6e4642ab'
let codePushOptions = {
  checkFrequency : CodePush.CheckFrequency.ON_APP_START
}

class ProgressBar extends Component {

  constructor(props) {
    super(props)
    this.currProgress = 0.0
    this.syncMessage = ''
    this.state = {
      modalVisible: false,
      isMandatory: false,
      immediateUpdate: false,
      updateInfo: {}
    }
  }

  codePushStatusDidChange(syncStatus) {
    if (this.state.immediateUpdate) {
      switch(syncStatus) {
        case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
          this.syncMessage = 'Checking for update'
          break;
        case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
          this.syncMessage = 'Downloading package'
          break;
        case CodePush.SyncStatus.AWAITING_USER_ACTION:
          this.syncMessage = 'Awaiting user action'
          break;
        case CodePush.SyncStatus.INSTALLING_UPDATE:
          this.syncMessage = 'Installing update'
          break;
        case CodePush.SyncStatus.UP_TO_DATE:
          this.syncMessage = 'App up to date.'
          break;
        case CodePush.SyncStatus.UPDATE_IGNORED:
          this.syncMessage = 'Update cancelled by user'
          break;
        case CodePush.SyncStatus.UPDATE_INSTALLED:
          this.syncMessage = 'Update installed and will be applied on restart.'
          break;
        case CodePush.SyncStatus.UNKNOWN_ERROR:
          this.syncMessage = 'An unknown error occurred'
          Toast.showError('更新出錯,請重啟應用!')
          this.setState({modalVisible: false})
          break;
      }
    }
  }

  codePushDownloadDidProgress(progress) {
    if (this.state.immediateUpdate) {
      this.currProgress = parseFloat(progress.receivedBytes / progress.totalBytes).toFixed(2)
      if(this.currProgress >= 1) {
        this.setState({modalVisible: false})
      } else {
        this.refs.progressBar.progress = this.currProgress
      }
    }
  }

  syncImmediate() {
    CodePush.checkForUpdate(CODE_PUSH_KEY).then((update) => {
      console.log('-------' + update)
      if (!update) {
        Toast.showLongSuccess('已是最新版本!')
      } else {
        this.setState({modalVisible: true, updateInfo: update, isMandatory: update.isMandatory})
      }
    })
  }

  componentWillMount() {
    CodePush.disallowRestart()
    this.syncImmediate()
  }

  componentDidMount() {
    CodePush.allowRestart()
  }

  _immediateUpdate() {
    this.setState({immediateUpdate: true})
    CodePush.sync(
        {deploymentKey: CODE_PUSH_KEY, updateDialog: {}, installMode: CodePush.InstallMode.IMMEDIATE},
        this.codePushStatusDidChange.bind(this),
        this.codePushDownloadDidProgress.bind(this)
    )
  }

  renderModal() {
    return (
        <Modal
            animationType={"none"}
            transparent={true}
            visible={this.state.modalVisible}
            onRequestClose={() => alert("Modal has been closed.")}>
          <View style={styles.modal}>
            <View style={styles.modalContainer}>
              {
                !this.state.immediateUpdate ?
                    <View>
                      <Image style={{width: deviceInfo.deviceWidth - 60}} source={require('../../../assets/images/me/updateBg.png')} resizeMode={'stretch'}/>
                      <View style={{backgroundColor: GlobalStyles.white}}>
                        <View style={{marginHorizontal: 15}}>
                          <Text style={{marginVertical: 20, fontSize: 17, color: GlobalStyles.textBlockColor, fontWeight: 'bold'}}>更新內容</Text>
                          <Text style={{lineHeight: 20}}>{this.state.updateInfo.description}</Text>
                        </View>
                        <View style={{alignItems: GlobalStyles.center, marginTop: 20}}>
                          <Text style={{fontSize: 14, color: GlobalStyles.textGrayColor}}>wifi情況下更新不到30秒</Text>
                        </View>
                        {
                          !this.state.isMandatory ?
                              <View style={{flexDirection: GlobalStyles.row, height: 50, alignItems: GlobalStyles.center, marginTop: 20, borderTopColor: GlobalStyles.lineColor, borderTopWidth: 1 }}>
                                <TouchableOpacity
                                    onPress={() => this.setState({modalVisible: false})}>
                                  <View style={{flexDirection: GlobalStyles.row, alignItems: GlobalStyles.center, width: (deviceInfo.deviceWidth - 60) / 2, height: 50, borderRightColor: GlobalStyles.lineColor, borderRightWidth: 1, alignItems: GlobalStyles.center, justifyContent: GlobalStyles.center}}>
                                    <Icon name={'oneIcon|reject_o'} size={20} color={'#B6B6B6'}/>
                                    <Text style={{fontSize: 17, fontWeight: 'bold', color: GlobalStyles.textGrayColor, marginLeft: 10}}>殘忍拒絕</Text>
                                  </View>
                                </TouchableOpacity>
                                <TouchableOpacity
                                    style={{flexDirection: GlobalStyles.row, alignItems: GlobalStyles.center, width: (deviceInfo.deviceWidth - 60) / 2, height: 50, alignItems: GlobalStyles.center, justifyContent: GlobalStyles.center}}
                                    onPress={() => this._immediateUpdate()}
                                >
                                  <View style={{backgroundColor: '#3496FA', flex: 1, height: 40, alignItems: GlobalStyles.center, justifyContent: GlobalStyles.center, margin: 10, borderRadius: 20}}>
                                    <Text style={{fontSize: 17, color: GlobalStyles.white, fontWeight: 'bold'}}>極速下載</Text>
                                  </View>
                                </TouchableOpacity>
                              </View> :
                              <View style={{flexDirection: GlobalStyles.row, height: 60, alignItems: GlobalStyles.center, marginTop: 20, borderTopColor: GlobalStyles.lineColor, borderTopWidth: 1, width: deviceInfo.deviceWidth - 60}}>
                                <TouchableOpacity
                                    style={{flexDirection: GlobalStyles.row, alignItems: GlobalStyles.center, width: (deviceInfo.deviceWidth - 60), height: 50, alignItems: GlobalStyles.center, justifyContent: GlobalStyles.center}}
                                    onPress={() => this._immediateUpdate()}
                                >
                                  <View style={{backgroundColor: '#3496FA', flex: 1, height: 40, alignItems: GlobalStyles.center, justifyContent: GlobalStyles.center, borderRadius: 20, marginHorizontal: 40}}>
                                    <Text style={{fontSize: 17, color: GlobalStyles.white, fontWeight: 'bold'}}>立即更新</Text>
                                  </View>
                                </TouchableOpacity>
                              </View>
                        }
                      </View>
                    </View> :
                    <View>
                      <Image style={{width: deviceInfo.deviceWidth - 60}} source={require('../../../assets/images/me/updateBg.png')} resizeMode={'stretch'}/>
                      <View style={{backgroundColor: GlobalStyles.white, paddingVertical: 20, backgroundColor: GlobalStyles.white, alignItems: GlobalStyles.center}}>
                        <Progress
                            ref="progressBar"
                            progressColor={'#89C0FF'}
                            style={{
                              marginTop: 20,
                              height: 10,
                              width: deviceInfo.deviceWidth - 100,
                              backgroundColor: GlobalStyles.bgColor,
                              borderRadius: 10,
                            }}
                        />
                        <View style={{alignItems: GlobalStyles.center, marginVertical: 20}}>
                          <Text style={{fontSize: 14, color: GlobalStyles.textGrayColor}}>版本正在努力更新中,請等待</Text>
                        </View>
                      </View>
                    </View>
              }
            </View>
          </View>
        </Modal>
    )
  }

  render(){
    return(
        <View style={styles.container}>
          {this.renderModal()}
        </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: GlobalStyles.bgColor
  },
  modal: {
    height: deviceInfo.deviceHeight,
    width: deviceInfo.deviceWidth,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'rgba(0,0,0,0.3)'
  },
  modalContainer: {
    marginHorizontal: 60,
    borderBottomLeftRadius: 10,
    borderBottomRightRadius: 10,
  }
})

export default CodePush(codePushOptions)(ProgressBar)

下載進度條組件Progress 這裡也是封裝成一個組件,核心代碼如下:

image
image
/**
 * Created by guangqiang on 2018/3/29.
 */
import React, {Component}from 'react'
import {View, StyleSheet, Animated, Easing}from 'react-native'

import PropTypes from 'prop-types'

export default class CusProgressBar extends Component {

  static propTypes = {
    ...View.propTypes,
    // 當前進度
    progress: PropTypes.number,
    // second progress進度
    buffer: PropTypes.number,
    // 進度條顏色
    progressColor: PropTypes.string,
    // buffer進度條顏色
    bufferColor: PropTypes.string,
    // 進度動畫時長
    progressAniDuration: PropTypes.number,
    // buffer動畫時長
    bufferAniDuration: PropTypes.number
  }

  static defaultProps = {
    // 進度條顏色
    progressColor: 'white',
    // buffer進度條顏色
    bufferColor: 'rgba(255,0,0,0.7)',
    // 進度條動畫時長
    progressAniDuration: 100,
    // buffer進度條動畫時長
    bufferAniDuration: 100
  }

  constructor(props) {
    super(props)
    this._progressAni = new Animated.Value(0)
    this._bufferAni = new Animated.Value(0)
  }

  componentWillReceiveProps(nextProps) {
    this._progress = nextProps.progress
    this._buffer = nextProps.buffer
  }

  componentWillMount() {
    this._progress = this.props.progress
    this._buffer = this.props.buffer
  }

  render() {
    return (
        <View
            style={[styles.container,this.props.style]}
            onLayout={this._onLayout.bind(this)}>
          <Animated.View
              ref="progress"
              style={{
                position:'absolute',
                width: this._progressAni,
                backgroundColor:this.props.progressColor,
                borderRadius: 10
              }}/>
          <Animated.View
              ref="buffer"
              style={{
                position:'absolute',
                width: this._bufferAni,
                backgroundColor:this.props.bufferColor,
                borderRadius: 10,
              }}/>
        </View>
    )
  }

  _onLayout({nativeEvent: {layout:{width, height}}}) {
    // 防止多次調用,當第一次獲取後,後面就不再去獲取了
    if (width > 0 && this.totalWidth !== width) {
      // 獲取progress控制項引用
      let progress = this._getProgress()
      // 獲取buffer控制項引用
      let buffer = this._getBuffer()
      // 獲取父佈局寬度
      this.totalWidth = width
      //給progress控制項設置高度
      progress.setNativeProps({
        style: {
          height: height
        }
      })

      // 給buffer控制項設置高度
      buffer.setNativeProps({
        style: {
          height: height
        }
      })

      // 開始執行進度條動畫
      this._startAniProgress(this.progress)
      // 開始執行buffer動畫
      this._startAniBuffer(this.buffer)
    }
  }

  _startAniProgress(progress) {
    if (this._progress >= 0 && this.totalWidth !== 0) {
      Animated.timing(this._progressAni, {
        toValue: progress * this.totalWidth,
        duration: this.props.progressAniDuration,
        easing: Easing.linear
      }).start()
    }
  }

  _startAniBuffer(buffer) {
    if (this._buffer >= 0 && this.totalWidth !== 0) {
      Animated.timing(this._bufferAni, {
        toValue: buffer * this.totalWidth,
        duration: this.props.bufferAniDuration,
      }).start()
    }
  }

  _getProgress() {
    if (typeof this.refs.progress.refs.node !== 'undefined') {
      return this.refs.progress.refs.node
    }
    return this.refs.progress._component
  }

  _getBuffer() {
    if (typeof this.refs.buffer.refs.node !== 'undefined') {
      return this.refs.buffer.refs.node;
    }
    return this.refs.buffer._component;
  }
}

Object.defineProperty(CusProgressBar.prototype, 'progress', {
  set(value){
    if (value >= 0 && this._progress !== value) {
      this._progress = value;
      this._startAniProgress(value);
    }
  },
  get() {
    return this._progress;
  },
  enumerable: true,
})

Object.defineProperty(CusProgressBar.prototype, 'buffer', {
  set(value){
    if (value >= 0 && this._buffer !== value) {
      this._buffer = value;
      this._startAniBuffer(value);
    }
  },
  get() {
    return this._buffer;
  },
  enumerable: true,
})

const styles = StyleSheet.create({
  container: {
    height: 4,
    backgroundColor: 'blue'
  }
})

UpdateComp組件中的熱更新核心代碼講解

image
image

這我們在UpdateComp 組件中,在 componentWillMount 的生命周期函數中,我們調用codepush提供的這兩個函數:併在syncImmediate 函數中,我們調用codepush的checkForUpdate 函數來檢查是否已有新版本,以及新版本的信息等,具體代碼實現如下:

image
image

註意:

codepush有兩個代理函數我們需要調用:

image
image
  • codePushStatusDidChange: codepush狀態的變化的鉤子函數

  • codePushDownloadDidProgress: codepush下載更新包的進度鉤子函數

當我們處理完上面的內容,codepush的基本功能我們就處理完畢了,剩下的工作就是處理一些邏輯了,包括該不該彈更新框,以及更新彈框和更新進度的處理

總結:

本篇教程主要是講解codepush中如何處理安裝包的下載進度,以及如何自定義更新彈框和下載進度條,上面的彈框功能和下載進度條功能基本都已處理完畢,可以直接複製兩個組件代碼到自己項目中,稍作修改即可使用。如果還有小伙伴對codepush詳細的接入流程不熟悉的,請點擊查看作者的CodePush熱更新詳細接入教程一文,如果還有其他的問題,也可以簡書留言或者進群提問

RN實戰總結

    • 作者React Native開源項目OneM地址(按照企業開發標準搭建框架完成開發的):https://github.com/guangqiang-liu/OneM:歡迎小伙伴們 star
    • 作者簡書主頁:包含60多篇RN開發相關的技術文章http://www.jianshu.com/u/023338566ca5歡迎小伙伴們:多多關註,多多點贊
    • 作者React Native QQ技術交流群:620792950 歡迎小伙伴進群交流學習
    • 友情提示:在開發中有遇到RN相關的技術問題,歡迎小伙伴加入交流群(620792950),在群里提問、互相交流學習。交流群也定期更新最新的RN學習資料給大家,謝謝大家支持!

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 基於Html5 Plus + Vue + Mui 移動App 開發(二) 界面效果: 本頁面採用Html5 Plus + Vue + Mui 開發移動界面,本頁面實現: 1、下拉刷新、上拉獲取更多功能; 2、使用Vue 進行數據綁定; 3、使用WebView 創建打開新的界面; 源碼如下: App下 ...
  • ideviceinstaller -i .ipa包所在的路徑 環境搭建:Mac上安裝brew(brew裡面有很多命令,可以安裝自己想用的命令) 安裝命令如下:curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xv ...
  • 使用Html5 plus + Mui 進行移動App開發,有一段時間了,這幾日得空,做個資訊App分享給大家。 今天主要分享主頁實現,首先看下效果: 此界面主要分為:標題、內容分類列表、搜索及設置按鈕。 標題 內容分類列表 搜索框 設置按鈕 界面定義完後,接下來進行事件定義,實現交互效果: 完整代碼 ...
  • 前言 說起內購,其實挺令開發者厭煩的,原因呢,先不說漏單的問題,首先蘋果要扣除30%的銷售額哦,可恨不?(我覺得可恨),有些想辦法先隱藏掉第三方支付(支付寶、微信等),等項目上線了,再跳過內購使用第三方支付,emmmm.......這個方法確實不錯,但是如果被蘋果發現了,APP內虛擬產品調用第三方支 ...
  • 概述 OKhttp是一個網路請求開源項目,Android網路請求輕量級框架,支持文件上傳與下載,支持https,由移動支付Square公司貢獻。 依賴 Get請求 Get方式發送同步請求 Get方式發送非同步請求 Post請求 FormBody傳遞鍵值對參數 RequestBody傳遞Json或Fil ...
  • 和上一篇文章一樣,數組的重要性不言而喻,在OC編程的過程中我們會不斷的使用到NSArray,和C語言不同的是,我們這裡的數組只能存OC對象類型,不能存C語言基本數據類型,也不能存NSNull類型,但是我們這裡的數組可以存多種對象類型,比如,在同一個數組裡面,既可以存字元串類型,又可以存對象化了以後的 ...
  • 環境要求: 至少Mac OSX 10.12.6 iOS 11 Xcode 9 1. ”自己的工程“ -> windows -> Device and Simulators ,打開設備和模擬器界面 https://www.jianshu.com/p/e9c1525cc540 https://www.j ...
  • jQuery內容: 下載鏈接:jQuery官網 中文文檔:jQuery AP中文文檔 詳情請戳鏈接:前端之jQuery(一) 前端之jQuery(二) ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...