在golang中如何使用rating-input来进行打分及评价

159 篇文章 2 订阅
132 篇文章 0 订阅

在我们之前的C++文章“利用rating-input PreviewWidget来对事物进行评价及打分”,我们已经展示了如何使用C++来在Scope中的Preview中对事物进行评价或打分。在今天的这篇文章中,我们将介绍如何在Go Scope中来做同样的事。我们可以通过这个例子来展示如何捕获在Go Preview中的按钮并得到它们的action id以进行分别的处理。


在Go文件中的Preview方法中:


unc (s *MyScope) Preview(result *scopes.Result, metadata *scopes.ActionMetadata, reply *scopes.PreviewReply, cancelled <-chan bool) error {
	layout1col := scopes.NewColumnLayout(1)
	layout2col := scopes.NewColumnLayout(2)
	layout3col := scopes.NewColumnLayout(3)

	// Single column layout
	layout1col.AddColumn("image", "header", "summary", "rating", "actions")

	// Two column layout
	layout2col.AddColumn("image")
	layout2col.AddColumn("header", "summary", "rating", "actions")

	// Three cokumn layout
	layout3col.AddColumn("image")
	layout3col.AddColumn("header", "summary", "rating", "actions")
	layout3col.AddColumn()

	// Register the layouts we just created
	reply.RegisterLayout(layout1col, layout2col, layout3col)

	header := scopes.NewPreviewWidget("header", "header")

	// It has title and a subtitle properties
	header.AddAttributeMapping("title", "title")
	header.AddAttributeMapping("subtitle", "subtitle")

	// Define the image section
	image := scopes.NewPreviewWidget("image", "image")
	// It has a single source property, mapped to the result's art property
	image.AddAttributeMapping("source", "art")

	// Define the summary section
	description := scopes.NewPreviewWidget("summary", "text")
	// It has a text property, mapped to the result's description property
	description.AddAttributeMapping("text", "description")

	actions := scopes.NewPreviewWidget("actions", "actions")
	actions.AddAttributeValue("actions", []actionInfo{
		actionInfo{Id: "my_action", Label: "Close"},
		actionInfo{Id: "my_action2", Label: "Refresh"},
	})

	rating := scopes.NewPreviewWidget("rating", "rating-input")
	rating.AddAttributeValue("visible", "both")
	rating.AddAttributeValue("required", "rating")

	var scope_data string
	metadata.ScopeData(scope_data)
	if len(scope_data) > 0 {
		extra := scopes.NewPreviewWidget("extra", "text")
		extra.AddAttributeValue("text", "test Text")
		reply.PushWidgets(header, image, description, actions, rating, extra)
	} else {
		reply.PushWidgets(header, image, description, rating, actions)
	}

	return nil
}


我们可以在上面的文字中,看到:


	rating := scopes.NewPreviewWidget("rating", "rating-input")
	rating.AddAttributeValue("visible", "both")
	rating.AddAttributeValue("required", "rating")


它用来产生一个rating的PreviewWidget:即有rating,也有review,所以在设置“visible”中为both。


func (sc *MyScope) PerformAction(result *scopes.Result, metadata *scopes.ActionMetadata, widgetId, actionId string) (*scopes.ActivationResponse, error) {
	log.Printf("Perform action for widget=%s, action=%s\n", widgetId, actionId)
	
	// var scope_data interface{}
	var scope_data map[string]interface{}
	metadata.ScopeData(&scope_data)
	
	log.Println("rating: ", scope_data["rating"])
	log.Println("review: ", scope_data["review"])

	for key, value := range scope_data {
		log.Println("key: ", key)
		log.Println("value: ", value)		
	}
	
	if widgetId == "actions" && actionId == "my_action" {
		resp := scopes.NewActivationResponse(scopes.ActivationHideDash)
		resp.SetScopeData([]string{"hello", "world"})
		return resp, nil
	} 

	return scopes.NewActivationResponse(scopes.ActivationShowPreview), nil
}


在Go 文件中实现上面的PerformAction就可以捕获在Preview中的按钮事件。我们在上面的代码中打印出rating及review的值。


运行我们的代码:


  


在运行时的输出:




整个项目的源码在:https://github.com/liu-xiao-guo/goscope_rating


我们可以在Terminal中:


$chmod +x *.sh


通过上述命令来使得.sh文件变为可以执行的文件


$./run.sh


通过上面的脚本执行,在Desktop上运行我们的Scope


$./build.sh -d


通过我们上面的脚本执行,可以编译goscope,并部署到手机中去


$./clean.sh


通过上面的脚本执行,清除所有的编译的中间文件



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值