|
| 1 | +/* |
| 2 | + * This program is free software: you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation, either version 3 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.pokegoapi.api.news; |
| 17 | + |
| 18 | +import POGOProtos.Data.News.CurrentNewsOuterClass; |
| 19 | +import POGOProtos.Data.News.NewsArticleOuterClass; |
| 20 | +import POGOProtos.Data.News.NewsArticleOuterClass.NewsArticle; |
| 21 | +import POGOProtos.Networking.Requests.Messages.MarkReadNewsArticleMessageOuterClass; |
| 22 | +import POGOProtos.Networking.Requests.Messages.MarkReadNewsArticleMessageOuterClass.MarkReadNewsArticleMessage; |
| 23 | +import POGOProtos.Networking.Requests.RequestTypeOuterClass; |
| 24 | +import POGOProtos.Networking.Responses.MarkReadNewsArticleResponseOuterClass; |
| 25 | +import POGOProtos.Networking.Responses.MarkReadNewsArticleResponseOuterClass.MarkReadNewsArticleResponse; |
| 26 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 27 | +import com.pokegoapi.api.PokemonGo; |
| 28 | +import com.pokegoapi.exceptions.request.RequestFailedException; |
| 29 | +import com.pokegoapi.main.ServerRequest; |
| 30 | +import com.pokegoapi.main.ServerRequestEnvelope; |
| 31 | +import com.pokegoapi.util.ClientInterceptor; |
| 32 | +import com.pokegoapi.util.Log; |
| 33 | +import lombok.Getter; |
| 34 | +import okhttp3.OkHttpClient; |
| 35 | +import okhttp3.Request; |
| 36 | +import okhttp3.Response; |
| 37 | + |
| 38 | +import java.io.BufferedReader; |
| 39 | +import java.io.IOException; |
| 40 | +import java.io.InputStreamReader; |
| 41 | +import java.util.*; |
| 42 | +import java.util.stream.Collector; |
| 43 | +import java.util.stream.Collectors; |
| 44 | +import java.util.stream.Stream; |
| 45 | + |
| 46 | + |
| 47 | +public class News { |
| 48 | + private static final java.lang.String TAG = News.class.getSimpleName(); |
| 49 | + |
| 50 | + private final PokemonGo api; |
| 51 | + @Getter |
| 52 | + private CurrentNewsOuterClass.CurrentNews currentNews; |
| 53 | + // the Key of the Language = Key |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor |
| 57 | + * @param api Pokemon Go Core Api |
| 58 | + */ |
| 59 | + public News(PokemonGo api) { |
| 60 | + this.api = api; |
| 61 | + } |
| 62 | + |
| 63 | + public void setCurrentNews(CurrentNewsOuterClass.CurrentNews currentNews) { |
| 64 | + this.currentNews = currentNews; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Mark Unread News to read |
| 69 | + */ |
| 70 | + public void markUnreadNews(){ |
| 71 | + |
| 72 | + if(currentNews == null || currentNews.getNewsArticlesCount() <= 0){ |
| 73 | + // do nothing |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Stored enabled and un-read article |
| 78 | + List<String> unReadNewsList = new ArrayList<>(); |
| 79 | + for(NewsArticle newsArticle:currentNews.getNewsArticlesList()){ |
| 80 | + if(newsArticle.getEnabled() && !newsArticle.getArticleRead()) |
| 81 | + unReadNewsList.add(newsArticle.getId()); |
| 82 | + } |
| 83 | + |
| 84 | + Log.i(TAG, "markUnreadNews total Article count:"+ unReadNewsList.size()); |
| 85 | + |
| 86 | + if(unReadNewsList.size() > 0) { |
| 87 | + MarkReadNewsArticleMessage msg = MarkReadNewsArticleMessage.newBuilder() |
| 88 | + .addAllNewsIds(unReadNewsList).build(); |
| 89 | + ServerRequest request = new ServerRequest(RequestTypeOuterClass.RequestType.MARK_READ_NEWS_ARTICLE, msg); |
| 90 | + ServerRequestEnvelope envelope = ServerRequestEnvelope.create(request); |
| 91 | + try { |
| 92 | + api.getRequestHandler().sendServerRequests(envelope); |
| 93 | + MarkReadNewsArticleResponse response = MarkReadNewsArticleResponse.parseFrom(request.getData()); |
| 94 | + if(response.getResult() == MarkReadNewsArticleResponse.Result.SUCCESS){ |
| 95 | + Log.i(TAG, "Mark News Article -> success"); |
| 96 | + }else{ |
| 97 | + Log.w(TAG, "Mark News Article -> !success"); |
| 98 | + } |
| 99 | + } catch (RequestFailedException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + Log.e(TAG, "RequestFailedException: cause:" + e.getCause() + " message:" + e.getMessage()); |
| 102 | + } catch (InvalidProtocolBufferException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + Log.e(TAG, "InvalidProtocolBufferException: cause:" + e.getCause() + " message:" + e.getMessage()); |
| 105 | + } |
| 106 | + }else { |
| 107 | + Log.i(TAG, "no unmarked news found -> skipped"); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +} |
0 commit comments