WIP: add option to vote on polls

This commit is contained in:
2025-10-10 11:39:57 +02:00
parent 81f65750b7
commit b68bec9ebd
5 changed files with 70 additions and 10 deletions

View File

@@ -1,5 +1,4 @@
import 'dart:convert';
import 'dart:developer';
import 'package:http/http.dart' as http;
@@ -12,10 +11,7 @@ class GetPollState extends TalkApi<GetPollStateResponse> {
GetPollState({required this.token, required this.pollId}) : super('v1/poll/$token/$pollId', null);
@override
GetPollStateResponse assemble(String raw) {
log(raw);
return GetPollStateResponse.fromJson(jsonDecode(raw)['ocs']);
}
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,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

@@ -1,5 +1,3 @@
import 'dart:developer';
import 'package:bubble/bubble.dart';
import 'package:emoji_picker_flutter/emoji_picker_flutter.dart' as emojis;
import 'package:flowder/flowder.dart';
@@ -9,6 +7,8 @@ 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';
@@ -303,8 +303,8 @@ class _ChatBubbleState extends State<ChatBubble> with SingleTickerProviderStateM
onDoubleTap: showOptionsDialog,
onTap: () {
if(message.originalData?['object']?.type == RichObjectStringObjectType.talkPoll) {
log(message.originalData!['object']!.id);
var pollState = GetPollState(token: widget.bubbleData.token, pollId: int.parse(message.originalData!['object']!.id)).run();
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)),
@@ -323,9 +323,10 @@ class _ChatBubbleState extends State<ChatBubble> with SingleTickerProviderStateM
),
actions: [
TextButton(
onPressed: () {
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')
),