2 Commits

Author SHA1 Message Date
b68bec9ebd WIP: add option to vote on polls 2025-10-10 11:39:57 +02:00
81f65750b7 added functionality to show own votes in polls 2025-10-10 02:01:43 +02:00
9 changed files with 287 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../talkApi.dart';
import 'getPollStateResponse.dart';
class GetPollState extends TalkApi<GetPollStateResponse> {
String token;
int pollId;
GetPollState({required this.token, required this.pollId}) : super('v1/poll/$token/$pollId', null);
@override
GetPollStateResponse assemble(String raw) => GetPollStateResponse.fromJson(jsonDecode(raw)['ocs']);
@override
Future<http.Response> request(Uri uri, Object? body, Map<String, String>? headers) => http.get(uri, headers: headers);
}

View File

@@ -0,0 +1,50 @@
import 'package:json_annotation/json_annotation.dart';
import '../../../apiResponse.dart';
part 'getPollStateResponse.g.dart';
@JsonSerializable(explicitToJson: true)
class GetPollStateResponse extends ApiResponse {
GetPollStateResponseObject data;
GetPollStateResponse(this.data);
factory GetPollStateResponse.fromJson(Map<String, dynamic> json) => _$GetPollStateResponseFromJson(json);
Map<String, dynamic> toJson() => _$GetPollStateResponseToJson(this);
}
@JsonSerializable(explicitToJson: true)
class GetPollStateResponseObject {
int id;
String question;
List<String> options;
dynamic votes;
String actorType;
String actorId;
String actorDisplayName;
int status;
int resultMode;
int maxVotes;
List<int> votedSelf;
int? numVoters;
List<String>? details;
GetPollStateResponseObject(
this.id,
this.question,
this.options,
this.votes,
this.actorType,
this.actorId,
this.actorDisplayName,
this.status,
this.resultMode,
this.maxVotes,
this.votedSelf,
this.numVoters,
this.details);
factory GetPollStateResponseObject.fromJson(Map<String, dynamic> json) => _$GetPollStateResponseObjectFromJson(json);
Map<String, dynamic> toJson() => _$GetPollStateResponseObjectToJson(this);
}

View File

@@ -0,0 +1,60 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'getPollStateResponse.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GetPollStateResponse _$GetPollStateResponseFromJson(
Map<String, dynamic> json) =>
GetPollStateResponse(
GetPollStateResponseObject.fromJson(json['data'] as Map<String, dynamic>),
)..headers = (json['headers'] as Map<String, dynamic>?)?.map(
(k, e) => MapEntry(k, e as String),
);
Map<String, dynamic> _$GetPollStateResponseToJson(
GetPollStateResponse instance) =>
<String, dynamic>{
if (instance.headers case final value?) 'headers': value,
'data': instance.data.toJson(),
};
GetPollStateResponseObject _$GetPollStateResponseObjectFromJson(
Map<String, dynamic> json) =>
GetPollStateResponseObject(
(json['id'] as num).toInt(),
json['question'] as String,
(json['options'] as List<dynamic>).map((e) => e as String).toList(),
json['votes'],
json['actorType'] as String,
json['actorId'] as String,
json['actorDisplayName'] as String,
(json['status'] as num).toInt(),
(json['resultMode'] as num).toInt(),
(json['maxVotes'] as num).toInt(),
(json['votedSelf'] as List<dynamic>)
.map((e) => (e as num).toInt())
.toList(),
(json['numVoters'] as num?)?.toInt(),
(json['details'] as List<dynamic>?)?.map((e) => e as String).toList(),
);
Map<String, dynamic> _$GetPollStateResponseObjectToJson(
GetPollStateResponseObject instance) =>
<String, dynamic>{
'id': instance.id,
'question': instance.question,
'options': instance.options,
'votes': instance.votes,
'actorType': instance.actorType,
'actorId': instance.actorId,
'actorDisplayName': instance.actorDisplayName,
'status': instance.status,
'resultMode': instance.resultMode,
'maxVotes': instance.maxVotes,
'votedSelf': instance.votedSelf,
'numVoters': instance.numVoters,
'details': instance.details,
};

View File

@@ -0,0 +1,30 @@
import 'dart:convert';
import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import '../getPoll/getPollStateResponse.dart';
import '../talkApi.dart';
import 'votePollParams.dart';
class VotePoll extends TalkApi {
String token;
int pollId;
VotePoll({required this.token, required this.pollId, required VotePollParams params}) : super('v1/poll/$token/$pollId', params);
@override
GetPollStateResponse assemble(String raw) {
log(raw);
return GetPollStateResponse.fromJson(jsonDecode(raw)['ocs']);
}
@override
Future<Response>? request(Uri uri, Object? body, Map<String, String>? headers) {
if(body is VotePollParams) {
log(body.toJson().toString());
return http.post(uri, headers: headers, body: body.toJson().toString());
}
return null;
}
}

View File

@@ -0,0 +1,14 @@
import 'package:json_annotation/json_annotation.dart';
import '../../../apiParams.dart';
part 'votePollParams.g.dart';
@JsonSerializable()
class VotePollParams extends ApiParams {
List<int> optionIds;
VotePollParams({required this.optionIds});
factory VotePollParams.fromJson(Map<String, dynamic> json) => _$VotePollParamsFromJson(json);
Map<String, dynamic> toJson() => _$VotePollParamsToJson(this);
}

View File

@@ -0,0 +1,19 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'votePollParams.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
VotePollParams _$VotePollParamsFromJson(Map<String, dynamic> json) =>
VotePollParams(
optionIds: (json['optionIds'] as List<dynamic>)
.map((e) => (e as num).toInt())
.toList(),
);
Map<String, dynamic> _$VotePollParamsToJson(VotePollParams instance) =>
<String, dynamic>{
'optionIds': instance.optionIds,
};

View File

@@ -6,6 +6,9 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:jiffy/jiffy.dart';
import 'package:open_filex/open_filex.dart';
import '../../../../api/marianumcloud/talk/getPoll/getPollState.dart';
import '../../../../api/marianumcloud/talk/votePoll/votePoll.dart';
import '../../../../api/marianumcloud/talk/votePoll/votePollParams.dart';
import '../../../../extensions/text.dart';
import 'package:provider/provider.dart';
@@ -18,11 +21,13 @@ import '../../../../api/marianumcloud/talk/reactMessage/reactMessageParams.dart'
import '../../../../api/marianumcloud/talk/room/getRoomResponse.dart';
import '../../../../model/chatList/chatProps.dart';
import '../../../../widget/debug/debugTile.dart';
import '../../../../widget/loadingSpinner.dart';
import '../../files/fileElement.dart';
import 'answerReference.dart';
import 'chatBubbleStyles.dart';
import 'chatMessage.dart';
import '../messageReactions.dart';
import 'pollOptionsList.dart';
class ChatBubble extends StatefulWidget {
final BuildContext context;
@@ -297,6 +302,42 @@ class _ChatBubbleState extends State<ChatBubble> with SingleTickerProviderStateM
onLongPress: showOptionsDialog,
onDoubleTap: showOptionsDialog,
onTap: () {
if(message.originalData?['object']?.type == RichObjectStringObjectType.talkPoll) {
var pollId = int.parse(message.originalData!['object']!.id);
var pollState = GetPollState(token: widget.bubbleData.token, pollId: pollId).run();
List<int>? ownVotes;
showDialog(context: context, builder: (context) => AlertDialog(
title: Text(message.originalData!['object']!.name, textScaler: TextScaler.linear(0.9)),
content: FutureBuilder(
future: pollState,
builder: (context, snapshot) {
if(snapshot.connectionState == ConnectionState.waiting) return const LoadingSpinner();
var pollData = snapshot.data!.data;
ownVotes = pollData.votedSelf;
return PollOptionsList(
pollData: pollData,
callback: (votes) => ownVotes = votes
);
}
),
actions: [
TextButton(
onPressed: () async {
Navigator.of(context).pop();
if(ownVotes == null) return;
VotePoll(pollId: pollId, token: widget.bubbleData.token, params: VotePollParams(optionIds: ownVotes!)).run();
},
child: const Text('Stimme abgeben')
),
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Abbrechen')
),
],
));
}
if(message.file == null) return;
if(downloadProgress > 0) {

View File

@@ -32,6 +32,14 @@ class ChatMessage {
onOpen: onOpen,
);
if(originalData?['object']?.type == RichObjectStringObjectType.talkPoll) {
return ListTile(
leading: const Icon(Icons.poll_outlined),
title: Text(originalData!['object']!.name),
contentPadding: const EdgeInsets.only(left: 10),
);
}
if(file == null) return contentWidget;
return Padding(

View File

@@ -0,0 +1,47 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../api/marianumcloud/talk/getPoll/getPollStateResponse.dart';
class PollOptionsList extends StatefulWidget {
final GetPollStateResponseObject pollData;
final Function(List<int>) callback;
const PollOptionsList({super.key, required this.pollData, required this.callback});
@override
State<PollOptionsList> createState() => _PollOptionsListState();
}
class _PollOptionsListState extends State<PollOptionsList> {
late List<int> ownVotes;
@override
void initState() {
super.initState();
ownVotes = widget.pollData.votedSelf;
}
@override
Widget build(BuildContext context) => ListView(
shrinkWrap: true,
children: [
...widget.pollData.options.map<Widget>(
(option) => CheckboxListTile(
value: ownVotes.contains(widget.pollData.options.indexOf(option)),
title: Text(option),
onChanged: (value) {
var optionId = widget.pollData.options.indexOf(option);
setState(() {
if(ownVotes.contains(optionId)) {
ownVotes.remove(optionId);
} else {
ownVotes.add(optionId);
}
});
widget.callback(ownVotes);
}
)
)
],
);
}