mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-25 02:18:23 +01:00
Preparing to fetch the subreddit info and save it to the database.
This commit is contained in:
parent
7f8ca49190
commit
9334d3860e
52
.idea/assetWizardSettings.xml
generated
52
.idea/assetWizardSettings.xml
generated
@ -1,52 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="WizardSettings">
|
||||
<option name="children">
|
||||
<map>
|
||||
<entry key="imageWizard">
|
||||
<value>
|
||||
<PersistentState />
|
||||
</value>
|
||||
</entry>
|
||||
<entry key="vectorWizard">
|
||||
<value>
|
||||
<PersistentState>
|
||||
<option name="children">
|
||||
<map>
|
||||
<entry key="vectorAssetStep">
|
||||
<value>
|
||||
<PersistentState>
|
||||
<option name="children">
|
||||
<map>
|
||||
<entry key="clipartAsset">
|
||||
<value>
|
||||
<PersistentState>
|
||||
<option name="values">
|
||||
<map>
|
||||
<entry key="url" value="jar:file:/home/alex/Android%20Studio/plugins/android/lib/android.jar!/images/material_design_icons/action/ic_account_circle_black_24dp.xml" />
|
||||
</map>
|
||||
</option>
|
||||
</PersistentState>
|
||||
</value>
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
<option name="values">
|
||||
<map>
|
||||
<entry key="color" value="9e9e9e" />
|
||||
<entry key="outputName" value="ic_account_circle_grey_24dp" />
|
||||
<entry key="sourceFile" value="$USER_HOME$" />
|
||||
</map>
|
||||
</option>
|
||||
</PersistentState>
|
||||
</value>
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
</PersistentState>
|
||||
</value>
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
BIN
.idea/caches/build_file_checksums.ser
generated
BIN
.idea/caches/build_file_checksums.ser
generated
Binary file not shown.
@ -25,7 +25,7 @@ class FetchComment {
|
||||
|
||||
void queryComment(FetchCommentListener fetchCommentListener) {
|
||||
mFetchCommentListener = fetchCommentListener;
|
||||
StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQueryCommentURI(subredditName, article), new Response.Listener<String>() {
|
||||
StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQueryCommentUri(subredditName, article), new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
mFetchCommentListener.onFetchCommentSuccess(response);
|
||||
|
@ -0,0 +1,40 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.StringRequest;
|
||||
|
||||
class FetchSubredditData {
|
||||
interface FetchSubredditDataListener {
|
||||
void onFetchSubredditDataSuccess(String response);
|
||||
void onFetchSubredditDataFail();
|
||||
}
|
||||
|
||||
private RequestQueue requestQueue;
|
||||
private String subredditName;
|
||||
private FetchSubredditDataListener mFetchSubredditDataListener;
|
||||
|
||||
FetchSubredditData(RequestQueue requestQueue, String subredditName) {
|
||||
this.requestQueue = requestQueue;
|
||||
this.subredditName = subredditName;
|
||||
}
|
||||
|
||||
void querySubredditData(FetchSubredditDataListener fetchSubredditDataListener) {
|
||||
mFetchSubredditDataListener = fetchSubredditDataListener;
|
||||
StringRequest commentRequest = new StringRequest(Request.Method.GET, RedditUtils.getQuerySubredditDataUri(subredditName), new Response.Listener<String>() {
|
||||
@Override
|
||||
public void onResponse(String response) {
|
||||
mFetchSubredditDataListener.onFetchSubredditDataSuccess(response);
|
||||
}
|
||||
}, new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
mFetchSubredditDataListener.onFetchSubredditDataFail();
|
||||
}
|
||||
}) {};
|
||||
commentRequest.setTag(FetchSubredditData.class);
|
||||
requestQueue.add(commentRequest);
|
||||
}
|
||||
}
|
@ -58,6 +58,8 @@ class JSONUtils {
|
||||
static final String COMMENT_KARMA_KEY = "comment_karma";
|
||||
static final String DISPLAY_NAME = "display_name";
|
||||
static final String SUBREDDIT_TYPE_KEY = "subreddit_type";
|
||||
|
||||
static final String SUBREDDIT_TYPE_VALUE_USER = "user";
|
||||
static final String SUBSCRIBERS_KEY = "subscribers";
|
||||
static final String PUBLIC_DESCRIPTION = "public_description";
|
||||
static final String ACTIVE_USER_COUNT = "active_user_count";
|
||||
}
|
||||
|
@ -0,0 +1,65 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
class ParseSubredditData {
|
||||
interface ParseSubredditDataListener {
|
||||
void onParseSubredditDataSuccess();
|
||||
void onParseSubredditDataFail();
|
||||
}
|
||||
|
||||
private ParseSubredditDataListener mParseSubredditDataListener;
|
||||
|
||||
void parseComment(String response, ParseSubredditDataListener parseSubredditDataListener) {
|
||||
mParseSubredditDataListener = parseSubredditDataListener;
|
||||
new ParseSubredditDataAsyncTask(response, mParseSubredditDataListener).execute();
|
||||
}
|
||||
|
||||
private static class ParseSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
private JSONObject jsonResponse;
|
||||
private boolean parseFailed;
|
||||
private ParseSubredditDataListener parseSubredditDataListener;
|
||||
|
||||
ParseSubredditDataAsyncTask(String response, ParseSubredditDataListener parseSubredditDataListener){
|
||||
this.parseSubredditDataListener = parseSubredditDataListener;
|
||||
try {
|
||||
jsonResponse = new JSONObject(response);
|
||||
parseFailed = false;
|
||||
} catch (JSONException e) {
|
||||
Log.i("comment json error", e.getMessage());
|
||||
parseSubredditDataListener.onParseSubredditDataFail();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
try {
|
||||
JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY);
|
||||
String id = data.getString(JSONUtils.NAME_KEY);
|
||||
String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION);
|
||||
String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY);
|
||||
String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY);
|
||||
int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);
|
||||
int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT);
|
||||
} catch (JSONException e) {
|
||||
parseFailed = true;
|
||||
Log.i("parse", "SubredditData error");
|
||||
parseSubredditDataListener.onParseSubredditDataFail();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void aVoid) {
|
||||
if(!parseFailed) {
|
||||
parseSubredditDataListener.onParseSubredditDataSuccess();
|
||||
} else {
|
||||
parseSubredditDataListener.onParseSubredditDataFail();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -69,7 +69,11 @@ class RedditUtils {
|
||||
return params;
|
||||
}
|
||||
|
||||
static String getQueryCommentURI(String subredditName, String article) {
|
||||
static String getQueryCommentUri(String subredditName, String article) {
|
||||
return API_BASE_URI + "/" + subredditName + "/comments/" + article + ".json";
|
||||
}
|
||||
|
||||
static String getQuerySubredditDataUri(String subredditName) {
|
||||
return API_BASE_URI + "/r/" + subredditName + "/about.json";
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,19 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.persistence.room.Dao;
|
||||
import android.arch.persistence.room.Insert;
|
||||
import android.arch.persistence.room.OnConflictStrategy;
|
||||
import android.arch.persistence.room.Query;
|
||||
|
||||
@Dao
|
||||
public interface SubredditDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void insert(SubredditData SubredditData);
|
||||
|
||||
@Query("DELETE FROM subreddits")
|
||||
void deleteAllSubreddits();
|
||||
|
||||
@Query("SELECT * from subreddits WHERE id = :id")
|
||||
LiveData<SubredditData> getSubreddit(String id);
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.arch.persistence.room.ColumnInfo;
|
||||
import android.arch.persistence.room.Entity;
|
||||
import android.arch.persistence.room.PrimaryKey;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
@Entity(tableName = "subreddits")
|
||||
class SubredditData {
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
@ColumnInfo(name = "id")
|
||||
private String id;
|
||||
|
||||
@ColumnInfo(name = "name")
|
||||
private String name;
|
||||
|
||||
@ColumnInfo(name = "icon_url")
|
||||
private String iconUrl;
|
||||
|
||||
@ColumnInfo(name = "banner_url")
|
||||
private String bannerUrl;
|
||||
|
||||
@ColumnInfo(name = "description")
|
||||
private String description;
|
||||
|
||||
@ColumnInfo(name = "subscribers_count")
|
||||
private int nSubscribers;
|
||||
|
||||
SubredditData(@NonNull String id, String name, String iconUrl, String bannerUrl, String description, int nSubscribers) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.iconUrl = iconUrl;
|
||||
this.bannerUrl = bannerUrl;
|
||||
this.description = description;
|
||||
this.nSubscribers = nSubscribers;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getIconUrl() {
|
||||
return iconUrl;
|
||||
}
|
||||
|
||||
public String getBannerUrl() {
|
||||
return bannerUrl;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public int getnSubscribers() {
|
||||
return nSubscribers;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
public class SubredditRepository {
|
||||
private SubredditDao mSubredditDao;
|
||||
private LiveData<SubredditData> mSubredditData;
|
||||
|
||||
SubredditRepository(Application application, String id) {
|
||||
SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
|
||||
mSubredditDao = db.subredditDao();
|
||||
mSubredditData = mSubredditDao.getSubreddit(id);
|
||||
}
|
||||
|
||||
LiveData<SubredditData> getSubredditData() {
|
||||
return mSubredditData;
|
||||
}
|
||||
|
||||
public void insert(SubredditData subredditData) {
|
||||
new SubredditRepository.insertAsyncTask(mSubredditDao).execute(subredditData);
|
||||
}
|
||||
|
||||
private static class insertAsyncTask extends AsyncTask<SubredditData, Void, Void> {
|
||||
|
||||
private SubredditDao mAsyncTaskDao;
|
||||
|
||||
insertAsyncTask(SubredditDao dao) {
|
||||
mAsyncTaskDao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(final SubredditData... params) {
|
||||
mAsyncTaskDao.insert(params[0]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.arch.persistence.room.Database;
|
||||
import android.arch.persistence.room.Room;
|
||||
import android.arch.persistence.room.RoomDatabase;
|
||||
import android.content.Context;
|
||||
|
||||
@Database(entities = {SubredditData.class}, version = 1)
|
||||
public abstract class SubredditRoomDatabase extends RoomDatabase{
|
||||
private static SubredditRoomDatabase INSTANCE;
|
||||
|
||||
public abstract SubredditDao subredditDao();
|
||||
|
||||
public static SubredditRoomDatabase getDatabase(final Context context) {
|
||||
if(INSTANCE == null) {
|
||||
synchronized (SubredditRoomDatabase.class) {
|
||||
if(INSTANCE == null) {
|
||||
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
|
||||
SubredditRoomDatabase.class, "subreddits")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.AndroidViewModel;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
|
||||
public class SubredditViewModel extends AndroidViewModel {
|
||||
private SubredditRepository mSubredditRepository;
|
||||
private LiveData<SubredditData> mSubredditData;
|
||||
|
||||
public SubredditViewModel(Application application, String id) {
|
||||
super(application);
|
||||
mSubredditRepository = new SubredditRepository(application, id);
|
||||
mSubredditData = mSubredditRepository.getSubredditData();
|
||||
}
|
||||
|
||||
public LiveData<SubredditData> getSubredditData() {
|
||||
return mSubredditData;
|
||||
}
|
||||
|
||||
public void insert(SubredditData subredditData) {
|
||||
mSubredditRepository.insert(subredditData);
|
||||
}
|
||||
}
|
@ -18,8 +18,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity);
|
||||
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout_view_subreddit_detail_activity);
|
||||
final CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity);
|
||||
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity);
|
||||
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
|
||||
boolean isShow = true;
|
||||
int scrollRange = -1;
|
||||
@ -39,7 +39,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
|
||||
FloatingActionButton fab = findViewById(R.id.fab);
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
|
Loading…
Reference in New Issue
Block a user