Skip to content

Commit

Permalink
added support for castpollvote broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
noumantahir committed Aug 16, 2024
1 parent 99eb171 commit f10bbdd
Show file tree
Hide file tree
Showing 16 changed files with 346 additions and 35 deletions.
36 changes: 36 additions & 0 deletions android/app/src/main/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,42 @@
);
}


//Add poll vote method here
function castPollVote(
id,
username,
pollId,
choices,
postingKey,
token,
authKey
) {
let op = [
"custom_json",
{
"id": "polls",
"required_auths": [],
"required_posting_auths": [
username
],
"json": JSON.stringify({ "poll": pollId, "action": "vote", "choices": choices })
}

];
performOperations(
id,
[op],
"polls",
username,
postingKey,
token,
authKey
);
}



function muteUser(
id,
username,
Expand Down
8 changes: 8 additions & 0 deletions android/app/src/main/kotlin/com/ecency/waves/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ class MainActivity: FlutterActivity() {
"voteContent('$id','$username', '$author', '$permlink', '$weight', '$postingKey', '$token', '$authKey');",
null
)
}
else if (call.method == "castPollVote" && username != null && pollId != null
&& choices != null && postingKey != null && token != null
&& authKey != null ) {
webView?.evaluateJavascript(
"castPollVote('$id','$username', '$pollId', '$choices', '$postingKey', '$token', '$authKey');",
null
)
} else if (call.method == "getImageUploadProofWithPostingKey" && username != null && postingKey != null) {
webView?.evaluateJavascript(
"getImageUploadProofWithPostingKey('$id', '$username', '$postingKey');",
Expand Down
16 changes: 16 additions & 0 deletions ios/Runner/AppBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ class AppBridge: NSObject {
id: id,
jsCode: "voteContent('\(id)','\(username)', '\(author)', '\(permlink)', '\(weight)', '\(postingKey)', '\(token)', '\(authKey)');"
) { text in result(text) }
case "castPollVote":
guard
let username = arguments ["username"] as? String,
let pollId = arguments ["pollId"] as? String,
let choices = arguments ["choices"] as? [Int],
let postingKey = arguments ["postingKey"] as? String,
let token = arguments ["token"] as? String,
let authKey = arguments ["authKey"] as? String
else {
debugPrint("username, pollId, choices, postingkey, token, authKey - are note set")
return result(FlutterMethodNotImplemented)
}
webVC.runThisJS(
id: id,
jsCode: "castPollVote('\(id)','\(username)', '\(pollId)', '\(choices)', '\(postingKey)', '\(token)', '\(authKey)');"
) { text in result(text) }
case "muteUser":
guard
let username = arguments ["username"] as? String,
Expand Down
33 changes: 33 additions & 0 deletions ios/Runner/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,39 @@
);
}

//Add poll vote method here
function castPollVote(
id,
username,
pollId,
choices,
postingKey,
token,
authKey
) {
let op = [
"custom_json",
{
"id": "polls",
"required_auths": [],
"required_posting_auths": [
username
],
"json": JSON.stringify({ "poll": pollId, "action": "vote", "choices": choices })
}

];
performOperations(
id,
[op],
"polls",
username,
postingKey,
token,
authKey
);
}

function muteUser(
id,
username,
Expand Down
27 changes: 27 additions & 0 deletions lib/core/models/broadcast_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ class VoteBroadCastModel {
}
}

class PollVoteBroadcastModel {
final String username;
final String pollId;
final List<int> choices;

const PollVoteBroadcastModel({
required this.username,
required this.pollId,
required this.choices
});


Map<String, dynamic> toJson() {
return {
"id": "polls",
"required_posting_auths": [username],
"json": json.encode(
{
"poll": pollId,
"action": "vote",
"choices": choices
}
)
};
}
}

class CommentBroadCastModel {
final String parentAuthor;
final String parentPermlink;
Expand Down
23 changes: 23 additions & 0 deletions lib/core/services/data_service/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ class ApiService {
}
}


Future<ActionSingleDataResponse<String>> castPollVote(
String username,
String pollId,
List<int> choices,
String? postingKey,
String? authKey,
String? token,
) async {
try {
String jsonString = await castPollVoteFromPlatform(
username, pollId, choices, postingKey, authKey, token);
ActionSingleDataResponse<String> response =
ActionSingleDataResponse.fromJsonString(jsonString, null,
ignoreFromJson: true);
return response;
} catch (e) {
return ActionSingleDataResponse(
status: ResponseStatus.failed, errorMessage: e.toString());
}
}


Future<ActionSingleDataResponse> broadcastTransactionUsingHiveSigner<T>(
String accessToken, BroadcastModel<T> args) async {
final url = Uri.parse('https://hivesigner.com/api/broadcast');
Expand Down
29 changes: 26 additions & 3 deletions lib/core/services/data_service/mobile_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ Future<String> voteContentFromPlatform(
return response;
}

//ADD custom json support for vote poll support here

Future<String> getImageUploadProofWithPostingKeyFromPlatform(
String username,
String postingKey,
) async {
final String id = 'getImageUploadProofWithPostingKey${DateTime.now().toIso8601String()}';
final String response = await platform.invokeMethod('getImageUploadProofWithPostingKey', {
final String id =
'getImageUploadProofWithPostingKey${DateTime.now().toIso8601String()}';
final String response =
await platform.invokeMethod('getImageUploadProofWithPostingKey', {
'id': id,
'username': username,
'postingKey': postingKey,
Expand All @@ -128,4 +132,23 @@ Future<String> muteUserFromPlatform(
return response;
}


Future<String> castPollVoteFromPlatform(
String username,
String pollId,
List<int> choices,
String? postingKey,
String? authKey,
String? token,
) async {
final String id = 'castPollVote${DateTime.now().toIso8601String()}';
final String response = await platform.invokeMethod('castPollVote', {
'id': id,
'username': username,
'pollId': pollId,
'choices': choices,
'postingKey': postingKey ?? '',
'token': token ?? '',
'authKey': authKey ?? ''
});
return response;
}
11 changes: 11 additions & 0 deletions lib/core/services/data_service/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ Future<String> voteContentFromPlatform(
return _error();
}

Future<String> castPollVoteFromPlatform(
String username,
String pollId,
List<int> choices,
String? postingKey,
String? authKey,
String? token,
) async {
return _error();
}

Future<String> getImageUploadProofWithPostingKeyFromPlatform(
String username,
String postingKey,
Expand Down
11 changes: 11 additions & 0 deletions lib/core/services/data_service/web_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ Future<String> voteContentFromPlatform(
throw UnimplementedError();
}

Future<String> castPollVoteFromPlatform(
String username,
String pollId,
List<int> choices,
String? postingKey,
String? authKey,
String? token,
) async {
throw UnimplementedError();
}

Future<String> getImageUploadProofWithPostingKeyFromPlatform(
String username,
String postingKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class SignTransactionNavigationModel {
final String? permlink;
final String? comment;
final List<String>? imageLinks;
final String? pollId;
final List<int>? choices;
final double? weight;
final bool ishiveKeyChainMethod;
final SignTransactionType transactionType;
Expand All @@ -13,6 +15,8 @@ class SignTransactionNavigationModel {
required this.author,
this.permlink,
this.imageLinks,
this.pollId,
this.choices,
this.comment,
this.weight,
required this.transactionType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,35 @@ class SignTransactionHiveController extends HiveTransactionController {
final double? weight;
final SignTransactionType transactionType;
String? _generatedPermlink;
final String? pollId;
final List<int>? choices;

SignTransactionHiveController({
required this.transactionType,
required this.author,
required this.authData,
required super.showError,
required super.onSuccess,
required super.onFailure,
required super.ishiveKeyChainMethod,
this.permlink,
this.comment,
this.imageLinks,
this.weight,
}) : assert(
SignTransactionHiveController(
{required this.transactionType,
required this.author,
required this.authData,
required super.showError,
required super.onSuccess,
required super.onFailure,
required super.ishiveKeyChainMethod,
this.permlink,
this.comment,
this.imageLinks,
this.weight,
this.pollId,
this.choices})
: assert(
!(transactionType == SignTransactionType.comment &&
(comment == null || imageLinks == null || permlink == null)),
"comment,permlink and imageLinks parameters are required"),
assert(
!(transactionType == SignTransactionType.vote &&
(weight == null || permlink == null)),
"weight and permlink parameters are required") {
"weight and permlink parameters are required"),
assert(
!(transactionType == SignTransactionType.pollvote &&
(weight == null || permlink == null)),
"pollId and choices parameters are required") {
_initSignTransactionSocketSubscription();
}

Expand Down Expand Up @@ -72,6 +80,7 @@ class SignTransactionHiveController extends HiveTransactionController {
String get successMessage {
switch (transactionType) {
case SignTransactionType.vote:
case SignTransactionType.pollvote:
return LocaleText.smVoteSuccessMessage;
case SignTransactionType.comment:
return LocaleText.smCommentPublishMessage;
Expand All @@ -83,6 +92,7 @@ class SignTransactionHiveController extends HiveTransactionController {
String get failureMessage {
switch (transactionType) {
case SignTransactionType.vote:
case SignTransactionType.pollvote:
return LocaleText.emVoteFailureMessage;
case SignTransactionType.comment:
return LocaleText.emCommentDeclineMessage;
Expand Down Expand Up @@ -134,6 +144,15 @@ class SignTransactionHiveController extends HiveTransactionController {
authData.auth.authKey,
authData.auth.token,
);
case SignTransactionType.pollvote:
return _threadRepository.castPollVote(
authData.accountName,
pollId!,
choices!,
null,
authData.auth.authKey,
authData.auth.token,
);
case SignTransactionType.mute:
return _threadRepository.muteUser(
authData.accountName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ class SignTransactionHiveSignerController {
}
}

Future<void> initPollVoteProcess({
required String pollId,
required List<int> choices,
required UserAuthModel<HiveSignerAuthModel> authdata,
required VoidCallback onSuccess,
required Function(String) showToast,
}) async {
ActionSingleDataResponse response = await _threadRepository
.broadcastTransactionUsingHiveSigner<PollVoteBroadcastModel>(
authdata.auth.token,
BroadcastModel(
type: BroadCastType.vote,
data: PollVoteBroadcastModel(
username: authdata.accountName,
pollId: pollId,
choices: choices,
)),
);
if (response.isSuccess) {
showToast(LocaleText.smVoteSuccessMessage);
onSuccess();
} else {
showToast(LocaleText.emVoteFailureMessage);
}
}

Future<void> initMuteProcess({
required String author,
required UserAuthModel<HiveSignerAuthModel> authdata,
Expand Down
Loading

0 comments on commit f10bbdd

Please sign in to comment.