Skip to content

Commit

Permalink
Merge pull request #5 from ecency/nt/polls-support
Browse files Browse the repository at this point in the history
Nt/polls support
  • Loading branch information
feruzm authored Aug 19, 2024
2 parents 5c5baf9 + 9260a5f commit 7e70bb9
Show file tree
Hide file tree
Showing 27 changed files with 1,644 additions and 25 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
17 changes: 10 additions & 7 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
"sm_posting_login_message": "You have successfully logged in with key",
"sm_comment_publish_message": "Comment is published successfully",
"sm_vote_success_message": "You have successfully voted the user",
"something_went_wrong":"Something went wrong",
"login_with_posting_key":"Login with posting key",
"please_enter_the_username":"Please enter the username",
"please_enter_the_posting_key":"Please enter the posting key",
"username":"username",
"posting_key":"posting key",
"no_threads_found":"No threads found"
"something_went_wrong": "Something went wrong",
"login_with_posting_key": "Login with posting key",
"please_enter_the_username": "Please enter the username",
"please_enter_the_posting_key": "Please enter the posting key",
"username": "username",
"posting_key": "posting key",
"no_threads_found": "No threads found",
"age_limit": "\u2022 Only accounts older than {} days allowed",
"interpretation_token": "\u2022 User HP based vote interpretation",
"max_choices": "\u2022 You may select upto {} choices"
}
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
5 changes: 5 additions & 0 deletions lib/core/locales/locale_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ class LocaleText {
static const String username = "username";
static const String postingKey = "posting_key";
static const String noThreadsFound = "no_threads_found";

//polls realted texts
static String ageLimit (int days) => "age_limit".tr(args: [days.toString()]);
static String get interpretationToken => "interpretation_token".tr();
static String maxChoices (int choices) => "max_choices".tr(args: [choices.toString()]);
}
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
12 changes: 12 additions & 0 deletions lib/core/providers/global_providers.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:provider/single_child_widget.dart';
import 'package:provider/provider.dart';
import 'package:waves/core/utilities/theme/theme_mode.dart';
import 'package:waves/features/threads/presentation/thread_feed/controller/poll_controller.dart';
import 'package:waves/features/threads/presentation/thread_feed/controller/thread_feed_controller.dart';
import 'package:waves/features/user/view/user_controller.dart';

Expand All @@ -23,6 +24,17 @@ class GlobalProviders {
return previousThreadFeedController;
}
},
),
ChangeNotifierProxyProvider<UserController, PollController>(
create: (context) => PollController(userData: null),
update: (context, userController, prevPollController) {
if (prevPollController == null) {
return PollController(userData: userController.userData);
} else {
prevPollController.updateUserData(userController.userData);
return prevPollController;
}
},
)
];
}
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
45 changes: 45 additions & 0 deletions lib/core/services/poll_service/poll_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


import 'package:http/http.dart' as http;
import 'package:waves/core/models/action_response.dart';
import 'package:waves/core/services/poll_service/poll_model.dart';
import 'package:waves/core/utilities/enum.dart';


///
/// swapper importable api url
/// https://polls-beta.hivehub.dev/
///
/// hive polls docs reference:
/// https://gitlab.com/peakd/hive-open-polls
///
Future<ActionSingleDataResponse<PollModel>> fetchPoll(
String author,
String permlink
) async {
try {
var url = Uri.parse(
"https://polls.hivehub.dev/rpc/poll?author=eq.$author&permlink=eq.$permlink");



http.Response response = await http.get(
url,
);

if (response.statusCode == 200) {
return ActionSingleDataResponse<PollModel>(
data: PollModel.fromJsonString(response.body).first,
status: ResponseStatus.success,
isSuccess: true,
errorMessage: "");
} else {
return ActionSingleDataResponse(
status: ResponseStatus.failed, errorMessage: "Server Error");
}
} catch (e) {
return ActionSingleDataResponse(
status: ResponseStatus.failed, errorMessage: e.toString());
}
}
Loading

0 comments on commit 7e70bb9

Please sign in to comment.