首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中使用Dagger2进行图表更新的Sci图

在Android中使用Dagger2进行图表更新的Sci图
EN

Stack Overflow用户
提问于 2020-03-02 08:46:51
回答 1查看 101关注 0票数 1

我正在构建一个基于arch的应用程序。实时图形数据的MVVM+Databinding。使用sci图表是快速和容易的,但使用Dagger2使用DI更新应用程序是在生成sci图表生成器实例,而试图使用SuspendUpdates()更新图形。ISuspendable接口已经通过ViewModelModule中的@Binds绑定。

代码语言:javascript
复制
@Module
abstract class ViewModelModule { 
@Singleton
@Binds
abstract fun getSuspendable(aSuspendable: AccurynSuspendableImpl): ISuspendable

@Binds
@IntoMap
@ViewModelKey(AViewModel::class)
abstract fun bindAViewModel(aViewModel: AViewModel): ViewModel

@Binds
internal abstract fun bindViewModelFactory(factory: AppViewModelFactory): ViewModelProvider.Factory
}

@Singleton
class AccurynSuspendableImpl @Inject constructor() : ISuspendable {


    override fun decrementSuspend() {
    }

   override fun suspendUpdates(): IUpdateSuspender? {
    return null
   }

   override fun getIsSuspended(): Boolean {
    return true
   }

    override fun resumeUpdates(p0: IUpdateSuspender?) {
    }
  }


 class AMFragment : Fragment() {
 private val acmVM by viewModel<AViewModel>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
     mBinding = binding(inflater, R.layout.display, container)
     mBinding.viewModel =  acmVM
     mBinding.lifecycleOwner = this
     mBinding.executePendingBindings()
     return mBinding.root
   }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      super.onViewCreated(view, savedInstanceState)
      initializeVM()
    }
    private fun initializeVM() {

       **//TODO: how to bind these xml feature through dagger ahead of time.**
        /*acmVM = AViewModel(
        mBinding.multiPaneChart.iChart,
        mBinding.multiPaneChart.uChart)*/
    }
 }

 class AViewModel @Inject constructor(
     private val iSusp: ISuspendable,
     private val uSusp: ISuspendable,
      ) :    BaseViewModel() {....

    var iRenderDataSeries = XyDataSeries<Date, Float>().apply { 
    fifoCapacity = FIFO_CAPACITY }
    var uColumnDataSeries = XyDataSeries<Date, Float>().apply { 
    fifoCapacity = FIFO_CAPACITY }
    var uLineDataSeries = XyDataSeries<Date, Float>().apply { 
    fifoCapacity = FIFO_CAPACITY }

    private val iColor = ContextCompat.getColor(mContext, 
    R.color.pink)
    private val uColumnColor = ContextCompat.getColor(mContext, 
    R.color.yellow)
    private val uLineColor = ContextCompat.getColor(mContext, 
    R.color.gray)

    val xIAxes = AxisCollection()
    val xUAxes = AxisCollection()
    val yIAxes = AxisCollection()
    val yUAxes = AxisCollection()

    val iRenderableSeries = RenderableSeriesCollection()
    val uRenderableSeries = RenderableSeriesCollection()
    private var iChartModifiers = ChartModifierCollection()
    private var uChartModifiers = ChartModifierCollection()

    init {
       initChartDisplay(context)
    }

    private fun initChartDisplay(context: Context) {
        xIAxes.add(generateXAxis(context, View.GONE, 
        AxisAlignment.Auto))
        xUAxes.add(generateXAxis(context, View.VISIBLE, 
        AxisAlignment.Top))

        yIAxes.add(generateYAxis(context, LABEL_ID_I))
        yUAxes.add(generateYAxis(context, LABEL_ID_U))

        iRenderableSeries.add(
        generateLineRenderableSeries(
            LABEL_ID_I, iRenderDataSeries, SolidPenStyle(iColor, 
            true, lineThickness, null)
            )
        )

        val uColumnSeries =
        generateColumnRenderableSeries(LABEL_ID_U, 
        uColumnDataSeries, SolidBrushStyle(uColumnColor))
        uColumnSeries.dataPointWidth = .95
        uRenderableSeries.add(uColumnSeries)

        uRenderableSeries.add(
           generateLineRenderableSeries(
           LABEL_ID_U, uLineDataSeries, SolidPenStyle(uLineColor, 
           true, lineThickness, null)
           )
        )

     }

     private fun loadData() {
         UpdateSuspender.using(iSusp) {
                iRenderDataSeries.append(Date(lastTimeStamp), 
                median)
            }
         }

       **--Based on certain condition need to update line and column 
       UpdateSuspender.using(uRenderableSeries.single()) {
                uColumnDataSeries.updateYAt(uCurIndex, hourlyTotal) 
       //hour is a current one.  We're going to update with the latest 
       // total for the hour
            }
       **-- whereas on other condition update line series over same 
       renderable series
       UpdateSuspender.using(uRenderableSeries.single()) {
            if (priorTimeStamp + GRAPH_CALC_GAP_CONSTANT < 
            lastTimeStamp)
            {
                UpdateSuspender.using(uRenderableSeries.single()) {
                    uLineDataSeries.append(Date(priorTimeStamp + 
                    GRAPH_DISPLAY_GAP_CONSTANT), Float.NaN)
                }
            }
            //line data series updates
            uLineDataSeries.append(

       Date(DateTimeUtils.toEpochMilli(mData.getRtcTimeStamp())),
                mData.uRate.toFloat()
            )
        }

  }

  XML:

   <com.xyz.widgets.ASciChart
       android:id="@+id/u_chart"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:visibility="@{graphViewModel.displayChartU ? 
               View.VISIBLE : View.GONE}"
       scichart:verticalGroup="@{graphViewModel.sharedVG}"        
      scichart:renderableSeries="@{graphViewModel.uRenderableSeries}"
      scichart:xAxes="@{graphViewModel.xUAxes}"
      scichart:yAxes="@{graphViewModel.yUAxes}"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

   <com.xyz.widgets.ASciChart
       android:id="@+id/i_chart"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:visibility="@{graphViewModel.displayChartI ? 
            View.VISIBLE : View.GONE}"
       scichart:verticalGroup="@{graphViewModel.sharedVG}"
       scichart:renderableSeries="@{graphViewModel.iRenderableSeries}"
       scichart:xAxes="@{graphViewModel.xIAxes}"
       scichart:yAxes="@{graphViewModel.yIAxes}"/>

如何在dagger2中绑定这个sci图表实例以在ViewModel中访问它?任何帮助都将不胜感激。

为错误的文献道歉。

你好,PK

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-03 16:30:13

我认为将SciChartSurface (即Android )传递到ViewModel中将违反MVVM模式的原则。

正如我从XML中看到的,您可以将RenderableSeries存储在ViewModel中。我建议您挂起与需要更新的RenderableSeries实例关联的DataSeries实例的更新:

代码语言:javascript
复制
private fun loadData() {
    // I assume you have only one renderable series in collection
    val renderableSeries = iRenderableSeries.single()
    UpdateSuspender.using(renderableSeries) {
        iRenderDataSeries.append(Date(lastTimeStamp), median)
    }
}

在这种情况下,您将不需要将SciChartSurface传递到ViewModel,并且将视图层与ViewModel隔离开来。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60485211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档