这两天再基于 ShareSDK Cocos2d-x 的 Sample 代码做 Lua 的分享接口。
有些 java 代码不得不来吐槽一下,顺便给出我的重构代码,请大家帮忙指正下:
片段一
if (!(content.get("title") == null)) {
map.put("title", content.get("title"));
}
if (!(content.get("description") == null)) {
map.put("comment", content.get("description"));
}
if (!(content.get("url") == null)) {
map.put("url", content.get("url"));
map.put("titleUrl", content.get("url"));
}
if (!(content.get("site") == null)) {
map.put("site", content.get("site"));
}
if (!(content.get("siteUrl") == null)) {
map.put("siteUrl", content.get("siteUrl"));
}
if (!(content.get("musicUrl") == null)) {
map.put("musicUrl", content.get("musicUrl"));
}
if (!(content.get("extInfo") == null)) {
map.put("extInfo", content.get("extInfo"));
}
碍眼的地方:
- 莫非 Java 里面 (!(content.get("content") == null)) 和 (content.get("content") != null) 有什么不同?我 Java 学的少,别骗我。
- 这段采用重复手法的代码,让我一个用惯 Lua 的人情何以堪啊。用一个名字 的映射表,然后循环就搞定的事情。
private static final Map<String, String> NATIVE_FIELDS;
static {
Map<String, String> map = new HashMap<String, String>();
map.put("text", "content");
map.put("title", "title");
map.put("comment", "description");
map.put("url", "url");
map.put("titleUrl", "url");
map.put("site", "site");
map.put("siteUrl", "siteUrl");
map.put("musicUrl", "musicUrl");
map.put("extInfo", "extInfo");
NATIVE_FIELDS = Collections.unmodifiableMap(map);
}
for (Map.Entry<String, String> entry : NATIVE_FIELDS.entrySet())
{
String nativeName = entry.getValue();
String wantedName = entry.getKey();
Object value = content.get(nativeName);
if (value != null)
{
map.put(wantedName, value);
}
}
片段二
if (map.containsKey("text")) {
oks.setText(String.valueOf(map.get("text")));
}
if (map.containsKey("imagePath")) {
oks.setImagePath(String.valueOf(map.get("imagePath")));
}
if (map.containsKey("imageUrl")) {
oks.setImageUrl(String.valueOf(map.get("imageUrl")));
}
if (map.containsKey("title")) {
oks.setTitle(String.valueOf(map.get("title")));
}
if (map.containsKey("comment")) {
oks.setComment(String.valueOf(map.get("comment")));
}
if (map.containsKey("url")) {
oks.setUrl(String.valueOf(map.get("url")));
}
if (map.containsKey("titleUrl")) {
oks.setTitleUrl(String.valueOf(map.get("titleUrl")));
}
if (map.containsKey("site")) {
oks.setSite(String.valueOf(map.get("site")));
}
if (map.containsKey("siteUrl")) {
oks.setSiteUrl(String.valueOf(map.get("siteUrl")));
}
比片段一难一点,但是在一个习惯了 Lua 的 字段就是字符的人眼里,还是看着碍眼。
Java 不是有反射吗?当然反射的代码有点麻烦,还要异常处理。 Android 的 Java 是不是 Java 8 哦?
private static final Map<String, Method> OKS_FIELDS;
private static Method getSetter(String name) throws NoSuchMethodException {
return OnekeyShare.class.getMethod(name, String.class);
}
static {
Map<String, Method> map = new HashMap<String, Method>();
try {
map.put("comment", getSetter("setComment"));
map.put("imagePath", getSetter("setImagePath"));
map.put("imageUrl", getSetter("setImageUrl"));
map.put("site", getSetter("setSite"));
map.put("siteUrl", getSetter("setSiteUrl"));
map.put("text", getSetter("setText"));
map.put("title", getSetter("setTitle"));
map.put("titleUrl", getSetter("setTitleUrl"));
map.put("url", getSetter("setUrl"));
}
catch (NoSuchMethodException e)
{
Log.e("share", "error", e);
}
OKS_FIELDS = Collections.unmodifiableMap(map);
}
try {
for (Map.Entry<String, Method> entry : OKS_FIELDS.entrySet()) {
String field = entry.getKey();
Method set = entry.getValue();
String value = (String)content.get(field);
if (value != null) {
set.invoke(oks, value);
}
}
}
catch (Exception e)
{
Log.e("share", "error", e);
}
最后总结
- ShareSDK 在 Android 和 iOS 上的消息格式不一样,导致这里的字段转换,不知道有没有基于 JSON 的借口。
- Java 语法还是有点麻烦啊, 比如 初始化 HashMap ,要一个一个设置。