Commit 7fb9d633 authored by Black Hat's avatar Black Hat

Fix roomAt cross thread operation.

parent d480c58d
......@@ -32,6 +32,10 @@ Page {
Layout.maximumWidth: 360
listModel: roomListModel
onCurrentIndexChanged: {
roomForm.setRoom(roomListModel.roomAt(currentIndex));
}
}
RoomForm {
......@@ -40,7 +44,8 @@ Page {
Layout.fillWidth: true
Layout.fillHeight: true
currentRoom: roomListForm.currentItem
// currentRoom: roomListForm.currentIndex != -1 ? roomListModel.roomAt(roomListForm.currentIndex) : null
}
}
}
......@@ -10,7 +10,7 @@ import "qrc:/qml/component"
Item {
property alias listModel: delegateModel.model
property alias currentItem: listView.currentItem
property alias currentIndex: listView.currentIndex
readonly property bool mini: width <= 80 // Used as an indicator of whether the listform should be displayed as "Mini mode".
DelegateModel {
......@@ -23,8 +23,6 @@ Item {
filterOnGroup: "filterGroup"
delegate: ItemDelegate {
property variant myData: model
width: parent.width
height: 80
onClicked: listView.currentIndex = index
......@@ -37,7 +35,7 @@ Item {
ImageStatus {
width: parent.height
height: parent.height
source: model.avatar == null || model.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + model.avatar
source: avatar == null || avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + avatar
opaqueBackground: true
}
......@@ -48,13 +46,13 @@ Item {
Text {
width: parent.width
text: {
if (model.name != "") {
return model.name;
if (name != "") {
return name;
}
if (model.alias != "") {
return model.alias;
if (alias != "") {
return alias;
}
return model.id
return id
}
color: "#424242"
font.pointSize: 16
......@@ -63,7 +61,7 @@ Item {
}
Text {
width: parent.width
text: model.topic === "" ? "No topic yet." : model.topic
text: topic === "" ? "No topic yet." : topic
color: "#424242"
elide: Text.ElideRight
wrapMode: Text.NoWrap
......
......@@ -6,7 +6,12 @@ import QtGraphicalEffects 1.0
import "qrc:/qml/component"
Item {
property Item currentRoom: null
property var currentRoom
function setRoom(room) {
console.log("RoomView setting room: "+ room.id);
currentRoom = room;
}
Pane {
anchors.fill: parent
......@@ -52,7 +57,7 @@ Item {
ImageStatus {
Layout.preferredWidth: parent.height
Layout.fillHeight: true
source: currentRoom == null || currentRoom.myData.avatar == null || currentRoom.myData.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + currentRoom.myData.avatar
source: currentRoom == null || currentRoom.avatar == null || currentRoom.avatar == "" ? "qrc:/qml/asset/img/avatar.png" : "image://mxc/" + currentRoom.avatar
}
ColumnLayout {
......@@ -61,7 +66,7 @@ Item {
Text {
Layout.fillWidth: true
text: currentRoom != null ? currentRoom.myData.name : ""
text: currentRoom != null ? currentRoom.name : ""
font.pointSize: 18
color: "#424242"
elide: Text.ElideRight
......@@ -69,7 +74,7 @@ Item {
}
Text {
Layout.fillWidth: true
text: currentRoom != null ? currentRoom.myData.topic : ""
text: currentRoom != null ? currentRoom.topic : ""
color: "#424242"
elide: Text.ElideRight
wrapMode: Text.NoWrap
......
package ui
import (
"log"
"strings"
"github.com/therecipe/qt/core"
"matrique/matrix"
)
type Room struct {
core.QObject
controller *Controller
matrix.Room
_ string `property:"ID"`
_ string `property:"id"`
_ string `property:"alias"`
_ string `property:"name"`
_ string `property:"topic"`
_ string `property:"avatar"`
}
func (r *Room) Init(controller *Controller) {
r.SetController(controller)
r.GetAlias()
r.GetName()
r.GetTopic()
r.GetAvatar()
}
func (r *Room) SetController(controller *Controller) {
r.controller = controller
}
func (r *Room) GetAlias() {
type RespRoomAlias struct {
Alias string `json:"alias"`
}
resp := new(RespRoomAlias)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.canonical_alias", "", resp)
if err == nil && resp.Alias != "" {
r.SetAlias(resp.Alias)
return
}
}
func (r *Room) GetName() {
type RespRoomName struct {
Name string `json:"name"`
}
resp := new(RespRoomName)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.name", "", resp)
if err == nil && resp.Name != "" {
r.SetName(resp.Name)
return
}
respM, _ := r.controller.matrixCli.JoinedMembers(r.ID())
memberSlice := respM.Joined
if len(memberSlice) == 2 {
for eachID, eachMember := range memberSlice {
if eachID == r.controller.matrixCli.UserID {
continue
}
if eachMember.DisplayName != nil {
r.SetName(*eachMember.DisplayName)
return
}
break
}
}
}
func (r *Room) GetTopic() {
type RespRoomTopic struct {
Topic string `json:"topic"`
}
resp := new(RespRoomTopic)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.topic", "", resp)
if err != nil {
log.Println("Error when fetching topic of room", r.ID(), ":", err.Error())
return
}
r.SetTopic(resp.Topic)
func (r *Room) Init() {
r.ConnectId(func() string { return r.Room.ID })
r.ConnectSetId(func(id string) { r.Room.ID = id })
r.ConnectAlias(func() string { return r.Room.Alias })
r.ConnectSetAlias(func(alias string) { r.Room.Alias = alias })
r.ConnectName(func() string { return r.Room.Name })
r.ConnectSetName(func(name string) { r.Room.Name = name })
r.ConnectTopic(func() string { return r.Room.Topic })
r.ConnectSetTopic(func(topic string) { r.Room.Topic = topic })
r.ConnectAvatar(func() string { return r.Room.Avatar })
r.ConnectSetAvatar(func(avatar string) { r.Room.Avatar = avatar })
}
func (r *Room) GetAvatar() {
type RespRoomAvatar struct {
Avatar string `json:"url"`
}
resp := new(RespRoomAvatar)
err := r.controller.matrixCli.StateEvent(r.ID(), "m.room.avatar", "", resp)
if err == nil && resp.Avatar != "" {
r.SetAvatar(resp.Avatar)
return
}
respM, _ := r.controller.matrixCli.JoinedMembers(r.ID())
memberSlice := respM.Joined
if len(memberSlice) == 2 {
for eachID, eachMember := range memberSlice {
if eachID == r.controller.matrixCli.UserID {
continue
}
if eachMember.AvatarURL != nil {
r.SetAvatar(*eachMember.AvatarURL)
return
}
break
}
}
r.SetAvatar(strings.ToUpper(r.Name()))
func (r *Room) FromRoom(room *matrix.Room) {
r.SetId(room.ID)
r.SetAlias(room.Alias)
r.SetName(room.Name)
r.SetTopic(room.Topic)
r.SetAvatar(room.Avatar)
}
......@@ -4,6 +4,7 @@ import (
"log"
"github.com/therecipe/qt/core"
"matrique/matrix"
)
type RoomListModel struct {
......@@ -16,7 +17,7 @@ type RoomListModel struct {
_ map[int]*core.QByteArray `property:"roles"`
_ []*Room `property:"room"`
_ func(*[]*Room) `signal:"doResetModel"`
_ func(interface{}) `signal:"doResetModel"`
_ func(int) *Room `slot:"roomAt"`
_ func(*Room) `slot:"addRoom"`
......@@ -24,7 +25,6 @@ type RoomListModel struct {
}
func init() {
Room_QRegisterMetaType()
}
func (m *RoomListModel) init() {
......@@ -62,32 +62,42 @@ func (m *RoomListModel) fetchModel() {
resp, err := m.Controller().matrixCli.JoinedRooms()
checkErr(err)
roomChan := make(chan *Room)
roomChan := make(chan *matrix.Room)
defer close(roomChan)
for _, roomID := range resp.JoinedRooms {
go func(id string) {
log.Println("ID:", id)
defer m.Controller().recovery()
room := NewRoom(nil)
room.SetID(id)
room.Init(m.Controller())
roomChan <- room
room := matrix.Room{ID: id}
room.Init(m.Controller().matrixCli)
log.Println(room)
roomChan <- &room
}(roomID)
}
var rooms []*Room
var rooms []*matrix.Room
for i := 0; i < len(resp.JoinedRooms); i++ {
rooms = append(rooms, <-roomChan)
}
m.Controller().SetBusy(false)
m.DoResetModel(&rooms)
log.Println("BEFORE:", rooms)
m.DoResetModel(rooms)
}
func (m *RoomListModel) resetModel(rooms *[]*Room) {
func (m *RoomListModel) resetModel(roomsInterface interface{}) {
rooms := roomsInterface.([]*matrix.Room)
log.Println("Begin resetting roomlistmodel.")
m.BeginResetModel()
m.SetRoom(*rooms)
var qmlRooms []*Room
for _, room := range rooms {
qmlRoom := NewRoom(nil)
qmlRoom.FromRoom(room)
qmlRooms = append(qmlRooms, qmlRoom)
}
m.SetRoom(qmlRooms)
m.EndResetModel()
log.Println("Reset complete.")
}
......@@ -109,7 +119,7 @@ func (m *RoomListModel) data(index *core.QModelIndex, role int) *core.QVariant {
switch role {
case ID:
return core.NewQVariant14(r.ID())
return core.NewQVariant14(r.Id())
case Name:
return core.NewQVariant14(r.Name())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment