Skip to content

Commit

Permalink
Merge pull request #19 from tetris-11/feature-reddit
Browse files Browse the repository at this point in the history
Feature reddit
  • Loading branch information
walokra authored Jul 5, 2016
2 parents 00031e4 + caa686b commit 052f5d5
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 4 deletions.
52 changes: 52 additions & 0 deletions qml/components/imgur.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var ENDPOINT_GET_CREDITS = BASEURL + "/credits";
// https://api.imgur.com/3/gallery/search/{sort}/{page}?q=string
var ENDPOINT_GALLERY_SEARCH = ENDPOINT_GALLERY + "/search";

//Reddit stuff
var REDDIT_SUB = "clouds";

var ENDPOINT_GALLERY_ALBUM = ENDPOINT_GALLERY + "/album";
var ENDPOINT_GALLERY_IMAGE = ENDPOINT_GALLERY + "/image"
var ENDPOINT_ALBUM = BASEURL + "/album/";
Expand Down Expand Up @@ -100,6 +103,17 @@ function getMemesSubGallery(model, page, settings, onSuccess, onFailure) {
sendJSONRequest(url, 1, model, onSuccess, onFailure);
}

/** Reddit mode **/
function getRedditSubGallery(model, page, settings, onSuccess, onFailure) {
// Url prone to change due to shifting subreddit interests...
var url = ENDPOINT_GALLERY + "/r/" + REDDIT_SUB;

url += "/top/" + settings.window + "/page/" + page;

console.log("URL:", url);
sendJSONRequest(url, 1, model, onSuccess, onFailure); // 1 --> still a gallery
}

function sendJSONRequest(url, actiontype, model, onSuccess, onFailure) {
//console.log("sendJSONRequest, url=" + url + ", actiontype=" + actiontype);
var xhr = new XMLHttpRequest();
Expand All @@ -118,6 +132,8 @@ function sendJSONRequest(url, actiontype, model, onSuccess, onFailure) {
handleAlbumsJSON(xhr.responseText, model);
} else if (actiontype === "images") {
handleImagesJSON(xhr.responseText, model);
} else if (actiontype === "reddit") {
handleRedditJSON(xhr.responseText, model, onFailure);
}
//console.log("RateLimit: user=" + creditsUserRemaining + ", client=" + creditsClientRemaining);
onSuccess(xhr.status);
Expand Down Expand Up @@ -201,6 +217,32 @@ function getAlbum(id, model, albumModel, onSuccess, onFailure) {
xhr.send();
}


//get reddit image (called by GalleryImage first)
function getRedditImage(id, model, albumModel, onSuccess, onFailure) {
var url = ENDPOINT_IMAGE;
url += "/" + id;

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {

if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status == 200) {
handleGalleryImageJSON(xhr.responseText, model, albumModel);
onSuccess(xhr.status);
} else {
var jsonObject = JSON.parse(xhr.responseText);
onFailure(xhr.status, "NOPE: " + url); // jsonObject.data.error);
}
}
}
xhr = createGETHeader(xhr);
xhr.send();
}



// get gallery image
function getGalleryImage(id, model, albumModel, onSuccess, onFailure) {
var url = ENDPOINT_GALLERY_IMAGE;
Expand All @@ -213,6 +255,12 @@ function getGalleryImage(id, model, albumModel, onSuccess, onFailure) {
if (xhr.status == 200) {
handleGalleryImageJSON(xhr.responseText, model, albumModel);
onSuccess(xhr.status);

} else if (xhr.status == 404 ) {
// Try again, but under a different URL startpoint.
getRedditImage(id, model, albumModel, onSuccess, onFailure);


} else {
var jsonObject = JSON.parse(xhr.responseText);
onFailure(xhr.status, xhr.statusText + ": " + jsonObject.data.error);
Expand Down Expand Up @@ -664,6 +712,10 @@ function processGalleryMode(query, model, page, settings, onSuccess, onFailure)
getRandomGalleryImages(model, page, onSuccess, onFailure);
} else if (settings.mode === "memes") {
getMemesSubGallery(model, page, settings, onSuccess, onFailure);
} else if (settings.mode === "reddit") {
REDDIT_SUB = settings.reddit_sub;
console.log("REDDIT SUB=", REDDIT_SUB);
getRedditSubGallery(model, page, settings, onSuccess, onFailure);
} else if (settings.mode === "favorites") {
getFavorites(model, onSuccess, onFailure);
} else if (settings.mode === "albums") {
Expand Down
1 change: 1 addition & 0 deletions qml/pages/Constant.qml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ QtObject {
property string mode_random : "random";
property string mode_score : "score";
property string mode_memes : "memes";
property string mode_reddit : "reddit";
property string mode_favorites : "favorites";
property string mode_albums : "albums";
property string mode_images : "images";
Expand Down
10 changes: 8 additions & 2 deletions qml/pages/GalleryContentPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Page {

property bool is_album: false;
property bool is_gallery: true;
property bool is_reddit: false;
property string imgur_id : "";

property string galleryContentPageTitle : constant.appName;
Expand Down Expand Up @@ -36,12 +37,17 @@ Page {

onLoad: {
//console.log("galleryContentPage.onLoad: total=" + galleryContentModel.count + ", currentIndex=" + currentIndex);

is_reddit = (settings.mode === constant.mode_reddit);
///console.log("TEST", is_reddit);

galleryContentModel.resetVariables();
galleryContentModel.clear();
commentsModel.resetVariables();
commentsModel.clear();
loadingRectComments.visible = false;


if (galleryModel) {
imgur_id = galleryModel.get(currentIndex).id;
is_album = galleryModel.get(currentIndex).is_album;
Expand All @@ -56,7 +62,7 @@ Page {
galleryContentModel.getImage(imgur_id, is_gallery);
}

if (settings.showComments && is_gallery == true) {
if ((!is_reddit) && settings.showComments && is_gallery == true) {
loadingRectComments.visible = true;
commentsModel.getComments(imgur_id);
}
Expand Down Expand Up @@ -384,7 +390,7 @@ Page {
anchors.rightMargin: constant.paddingSmall;
height: childrenRect.height + showCommentsItem.height + galleryNavigation.height + constant.paddingMedium;
width: parent.width;
visible: is_gallery == true;
visible: ((is_reddit == false) && (is_gallery == true));

Item {
id: showCommentsItem;
Expand Down
84 changes: 84 additions & 0 deletions qml/pages/GalleryMode.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ Row {
case constant.mode_memes:
modeBox.currentIndex = 4;
break;
case constant.mode_reddit:
modeBox.currentIndex = 5;
break;
default:
modeBox.currentIndex = 0;
}
Expand Down Expand Up @@ -119,6 +122,15 @@ Row {
internal.setMode("memes");
}
}

MenuItem {
id: redditMode;
text: qsTr("reddit");
onClicked: {
internal.setMode("reddit");
}
}

}
}

Expand Down Expand Up @@ -150,6 +162,64 @@ Row {
}
}

ComboBox {
id: windowBox;
width: parent.width / 2;
currentIndex: 0;
label: qsTr("window:");
visible: accountModeLabel.visible == false;

menu: ContextMenu {
MenuItem {
id: dayWind;
text: qsTr("day");
onClicked: {
settings.window = "day";
internal.setWindowCommon();
}
}

MenuItem {
id: weekWind;
text: qsTr("week");
onClicked: {
settings.window = "week";
internal.setWindowCommon();
}
}

MenuItem {
id: monthWind;
text: qsTr("month");
onClicked: {
settings.window = "month";
internal.setWindowCommon();
}
}

MenuItem {
id: yearWind;
text: qsTr("year");
onClicked: {
settings.window = "year";
internal.setWindowCommon();
}
}

MenuItem {
id: allWind;
text: qsTr("all");
onClicked: {
settings.window = "all";
internal.setWindowCommon();
}
}
}
}




QtObject {
id: internal;

Expand Down Expand Up @@ -177,6 +247,13 @@ Row {
modeBox.currentIndex = 4;
sortBox.visible = true;
settings.mode = constant.mode_memes;
} else if (mode === "reddit") {
modeBox.currentIndex = 5;
sortBox.visible = false;
settings.sort = "time";
settings.showViral = false;
settings.showComments = false;
settings.mode = constant.mode_reddit;
} else {
modeBox.currentIndex = 0;
sortBox.visible = true;
Expand All @@ -202,6 +279,13 @@ Row {
galleryModel.clear();
galleryModel.processGalleryMode(galleryModel.query);
}

function setWindowCommon() {
settings.saveSetting("window", settings.window);
galgrid.scrollToTop();
galleryModel.clear();
galleryModel.processGalleryMode(galleryModel.query);
}
}

}
34 changes: 32 additions & 2 deletions qml/pages/MainPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ Page {
}

Item {
anchors.fill: parent
focus: true
id: flickerMode;
anchors.fill: parent;
focus: true;
Keys.onPressed: {
// Main page grid scroll
if (event.key === Qt.Key_Down) {
Expand Down Expand Up @@ -115,6 +116,35 @@ Page {
modeChanged("memes");
event.accepted = true;
}
if (event.key === Qt.Key_6) {
modeChanged("reddit");
event.accepted = true;
}
}
}

TextField {
id: redditSubInputField;
anchors.right: parent.right;
anchors.top: parent.top;
anchors.topMargin: 20;

visible: settings.mode === constant.mode_reddit;

width: parent.width / 1.2;

placeholderText: qsTr(settings.reddit_sub);

font.pixelSize: constant.fontSizeLarge;

EnterKey.enabled: text.length > 0;
EnterKey.iconSource: "image://theme/icon-m-enter-accept";
EnterKey.onClicked: {
settings.reddit_sub = redditSubInputField.text.trim();

galgrid.scrollToTop();
galleryModel.clear();
galleryModel.processGalleryMode(galleryModel.query);
}
}

Expand Down
2 changes: 2 additions & 0 deletions qml/pages/Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ QtObject {
property string section : "hot"; // hot | top | user
property string sort : "viral"; // viral | time
property string window : "day"; // day | week | month | year | all
property string reddit_sub : "clouds";
property bool showViral : false; // true | false
property bool autoplayAnim: true; // play anim gifs automatically?

Expand Down Expand Up @@ -64,6 +65,7 @@ QtObject {
Storage.writeSetting("useGalleryPage", settings.useGalleryPage);
Storage.writeSetting("toolbarBottom", settings.toolbarBottom);
Storage.writeSetting("useVideoLoader", settings.useVideoLoader);
Storage.writeSetting("redditSub", settings.reddit_sub);

settingsSaved();
}
Expand Down
30 changes: 30 additions & 0 deletions qml/pages/SettingsDialog.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Dialog {
checked ? settings.showComments = true : settings.showComments = false;
}
}

Label {
anchors { left: parent.left; right: parent.right; }
anchors.leftMargin: constant.paddingExtraLarge;
Expand All @@ -59,6 +60,20 @@ Dialog {
text: qsTr("Load comments automatically.");
}

Label {
anchors { left:parent.left;}
text: qsTr("Reddit Sub:");
font.pixelSize: constant.fontSizeMedium;
}

TextField {
id: redditSubInput;
anchors { right: parent.right;}
width: parent.width / 1.1;
placeholderText: qsTr(settings.reddit_sub);
}


TextSwitch {
anchors { left: parent.left; right: parent.right; }
anchors.leftMargin: constant.paddingMedium;
Expand Down Expand Up @@ -177,7 +192,22 @@ Dialog {
}

onAccepted: {
var oldRedditSub = settings.reddit_sub;

// Upon change, and reddit mode activated -- reset gallery.
if (oldRedditSub !== redditSubInput.text.trim()){
console.log("differs");
settings.reddit_sub = redditSubInput.text.trim();

if (settings.mode === constant.mode_reddit){
console.log("reddit mode");
galleryModel.clear();
galleryModel.processGalleryMode(galleryModel.query);
}
}
settings.saveSettings();


}

Component.onCompleted: {
Expand Down

0 comments on commit 052f5d5

Please sign in to comment.