mirror of
https://github.com/readrops/Readrops.git
synced 2025-02-11 17:30:39 +01:00
Adding simple refresh layout
This commit is contained in:
parent
88aa232f81
commit
43666dfb11
@ -3,6 +3,8 @@ package com.readrops.app;
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.readrops.app.database.Database;
|
||||
import com.readrops.app.database.entities.Feed;
|
||||
@ -22,6 +24,8 @@ import static com.readrops.readropslibrary.localfeed.RSSNetwork.RSSType.RSS_JSON
|
||||
|
||||
public class LocalFeedRepository extends ARepository implements QueryCallback {
|
||||
|
||||
public static final String TAG = LocalFeedRepository.class.getSimpleName();
|
||||
|
||||
private LiveData<List<Item>> items;
|
||||
|
||||
public LocalFeedRepository(Application application) {
|
||||
@ -37,19 +41,24 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
||||
@Override
|
||||
public void sync() {
|
||||
executor.execute(() -> {
|
||||
Log.d(TAG, "starting background thread");
|
||||
RSSNetwork request = new RSSNetwork();
|
||||
|
||||
Log.d(TAG, "getting feed list");
|
||||
List<Feed> feeds = database.feedDao().getAllFeeds();
|
||||
|
||||
for (Feed feed : feeds) {
|
||||
try {
|
||||
Log.d(TAG, "entering RSSNetwork");
|
||||
request.request(feed.getUrl(), this);
|
||||
} catch (Exception e) {
|
||||
failureCallBackInMainThread(e);
|
||||
}
|
||||
}
|
||||
|
||||
callback.onSuccess();
|
||||
// we go back to the main thread
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.post(() -> callback.onSuccess());
|
||||
});
|
||||
}
|
||||
|
||||
@ -100,8 +109,10 @@ public class LocalFeedRepository extends ARepository implements QueryCallback {
|
||||
List<Item> dbItems = Item.itemsFromRSS(items, feed);
|
||||
|
||||
for (Item dbItem : dbItems) {
|
||||
if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid())))
|
||||
if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid()))) {
|
||||
database.itemDao().insert(dbItem);
|
||||
Log.d(TAG, "adding " + dbItem.getTitle());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,42 +1,25 @@
|
||||
package com.readrops.app;
|
||||
|
||||
import android.arch.lifecycle.ViewModelProvider;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.DividerItemDecoration;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
|
||||
import com.readrops.app.database.entities.Item;
|
||||
import com.readrops.readropslibrary.PageParser;
|
||||
import com.readrops.readropslibrary.QueryCallback;
|
||||
import com.readrops.readropslibrary.Utils.Utils;
|
||||
import com.readrops.readropslibrary.localfeed.AItem;
|
||||
import com.readrops.readropslibrary.localfeed.RSSNetwork;
|
||||
import com.readrops.readropslibrary.localfeed.atom.ATOMEntry;
|
||||
import com.readrops.readropslibrary.localfeed.json.JSONFeed;
|
||||
import com.readrops.readropslibrary.localfeed.json.JSONItem;
|
||||
import com.readrops.readropslibrary.localfeed.rss.RSSFeed;
|
||||
import com.readrops.readropslibrary.localfeed.rss.RSSItem;
|
||||
|
||||
import org.simpleframework.xml.Serializer;
|
||||
import org.simpleframework.xml.core.Persister;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.Response;
|
||||
public class MainActivity extends AppCompatActivity implements SimpleCallback, SwipeRefreshLayout.OnRefreshListener {
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
String url = "https://framablog.org/";
|
||||
public static final String TAG = MainActivity.class.getSimpleName();
|
||||
|
||||
private RecyclerView recyclerView;
|
||||
private MainAdapter adapter;
|
||||
private SwipeRefreshLayout refreshLayout;
|
||||
|
||||
private List<Item> itemList;
|
||||
|
||||
@ -47,25 +30,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
/*Thread thread = new Thread(() -> {
|
||||
String imageUrl = PageParser.getOGImageLink("https://usbeketrica.com/galerie/dennis-osadebe-portrait-of-a-bright-generation");
|
||||
Log.d("", "");
|
||||
|
||||
runOnUiThread(() -> {
|
||||
getItems();
|
||||
});
|
||||
});
|
||||
|
||||
thread.start();*/
|
||||
|
||||
viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()).create(MainViewModel.class);
|
||||
viewModel.setSimpleCallback(this);
|
||||
|
||||
viewModel.getItems().observe(this, (List<Item> items) -> {
|
||||
this.itemList = items;
|
||||
initRecyclerView();
|
||||
});
|
||||
|
||||
viewModel.sync();
|
||||
refreshLayout = findViewById(R.id.swipe_refresh_layout);
|
||||
refreshLayout.setOnRefreshListener(this);
|
||||
}
|
||||
|
||||
private void initRecyclerView() {
|
||||
@ -79,4 +53,20 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
refreshLayout.setRefreshing(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception ex) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRefresh() {
|
||||
Log.d(TAG, "syncing started");
|
||||
viewModel.sync();
|
||||
}
|
||||
}
|
||||
|
@ -2,14 +2,16 @@ package com.readrops.app;
|
||||
|
||||
import android.app.Application;
|
||||
import android.arch.lifecycle.AndroidViewModel;
|
||||
import android.arch.lifecycle.LifecycleOwner;
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.readrops.app.database.entities.Item;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainViewModel extends AndroidViewModel implements SimpleCallback {
|
||||
public class MainViewModel extends AndroidViewModel {
|
||||
|
||||
private LiveData<List<Item>> items;
|
||||
private LocalFeedRepository repository;
|
||||
@ -18,7 +20,7 @@ public class MainViewModel extends AndroidViewModel implements SimpleCallback {
|
||||
super(application);
|
||||
|
||||
repository = new LocalFeedRepository(application);
|
||||
repository.setCallback(this);
|
||||
|
||||
items = repository.getItems();
|
||||
}
|
||||
|
||||
@ -26,18 +28,11 @@ public class MainViewModel extends AndroidViewModel implements SimpleCallback {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setSimpleCallback(SimpleCallback simpleCallback) {
|
||||
repository.setCallback(simpleCallback);
|
||||
}
|
||||
|
||||
public void sync() {
|
||||
repository.sync();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Exception ex) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
tools:context=".MainActivity"
|
||||
android:id="@+id/swipe_refresh_layout">
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/items_recycler_view"
|
||||
@ -15,4 +16,4 @@
|
||||
|
||||
</android.support.v7.widget.RecyclerView>
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
</android.support.v4.widget.SwipeRefreshLayout>
|
@ -1,5 +1,7 @@
|
||||
package com.readrops.readropslibrary.localfeed;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.readrops.readropslibrary.QueryCallback;
|
||||
import com.readrops.readropslibrary.Utils.Utils;
|
||||
@ -19,6 +21,8 @@ import okhttp3.Response;
|
||||
|
||||
public class RSSNetwork {
|
||||
|
||||
public static final String TAG = RSSNetwork.class.getName();
|
||||
|
||||
/**
|
||||
* Request the url given in parameter.
|
||||
* This method is synchronous, <b>it has to be called from another thread than the main one</b>.
|
||||
@ -29,6 +33,7 @@ public class RSSNetwork {
|
||||
public void request(String url, final QueryCallback callback) throws Exception {
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
|
||||
Log.d(TAG, "starting request");
|
||||
Request request = new Request.Builder().url(url).build();
|
||||
Response response = okHttpClient.newCall(request).execute();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user