start implementing everything with observables

This commit is contained in:
Felix 2017-03-24 23:52:38 +01:00
parent 0a3ef55fcd
commit a600bd0d01
12 changed files with 167 additions and 160 deletions

View file

@ -1,7 +1,6 @@
package de.nicidienase.chaosflix;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v17.leanback.widget.ImageCardView;

View file

@ -0,0 +1,51 @@
package de.nicidienase.chaosflix.activities;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import de.nicidienase.chaosflix.network.MediaApiService;
import io.reactivex.Observable;
/**
* Created by felix on 24.03.17.
*/
public class AbstractServiceConnectedAcitivty extends Activity {
private MediaApiService mMediaApiService;
private Observable<MediaApiService> mApiServiceObservable;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
Intent s = new Intent(this, MediaApiService.class);
mApiServiceObservable = Observable.create(e -> {
AbstractServiceConnectedAcitivty.this.bindService(s, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMediaApiService = ((MediaApiService.LocalBinder) service).getService();
e.onNext(mMediaApiService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Context.BIND_AUTO_CREATE);
});
}
public MediaApiService getmMediaApiService() {
return mMediaApiService;
}
public Observable<MediaApiService> getmApiServiceObservable() {
return mApiServiceObservable;
}
}

View file

@ -9,7 +9,7 @@ import de.nicidienase.chaosflix.R;
* Created by felix on 18.03.17.
*/
public class ConferencesActivity extends Activity {
public class ConferencesActivity extends AbstractServiceConnectedAcitivty {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View file

@ -1,6 +1,10 @@
package de.nicidienase.chaosflix.fragments;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v17.leanback.app.BrowseFragment;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.HeaderItem;
@ -33,26 +37,21 @@ public class ConferencesBrowseFragment extends BrowseFragment {
private static final String TAG = ConferencesBrowseFragment.class.getSimpleName();
public static final int FRAGMENT = R.id.browse_fragment;
private RecordingClient mMediaCCCClient;
private ArrayObjectAdapter mRowsAdapter;
private Map<String, List<Conference>> mConferences;
private BrowseErrorFragment mErrorFragment;
private MediaApiService mMediaApiService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(getResources().getString(R.string.app_name));
// setBadgeDrawable(getResources().getDrawable(R.drawable.chaosflix_logo));
mMediaCCCClient = new RecordingClient();
final BrowseErrorFragment errorFragment =
BrowseErrorFragment.showErrorFragment(getFragmentManager(),FRAGMENT);
mMediaCCCClient.listConferences().enqueue(new Callback<Conferences>() {
@Override
public void onResponse(Call<Conferences> call, Response<Conferences> response) {
Conferences body = response.body();
mConferences = body.getConferencesBySeries();
((ConferencesActivity)getActivity()).getmApiServiceObservable().subscribe(mediaApiService -> {
mMediaApiService = mediaApiService;
mMediaApiService.getConferences().subscribe(conferences -> {
mConferences = conferences.getConferencesBySeries();
mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
CardPresenter cardPresenter = new CardPresenter();
Set<String> keySet = mConferences.keySet();
@ -68,16 +67,8 @@ public class ConferencesBrowseFragment extends BrowseFragment {
}
errorFragment.dismiss();
setAdapter(mRowsAdapter);
}
@Override
public void onFailure(Call<Conferences> call, Throwable t) {
Log.d(TAG,"Error loading conferences",t);
errorFragment.setErrorContent(t.getMessage());
t.printStackTrace();
}
});
});
setOnItemViewClickedListener(new ItemViewClickedListener(this));
}

View file

@ -37,7 +37,7 @@ public class ConferencesGridFragment extends VerticalGridFragment {
mMediaCCCClient = new RecordingClient();
mMediaCCCClient.listConferences().enqueue(new Callback<Conferences>() {
mMediaCCCClient.getConferences().enqueue(new Callback<Conferences>() {
@Override
public void onResponse(Call<Conferences> call, Response<Conferences> response) {
List<Conference> conferences = response.body().getConferences();

View file

@ -6,15 +6,20 @@ import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import java.util.List;
import de.nicidienase.chaosflix.R;
import de.nicidienase.chaosflix.entities.recording.Conference;
import de.nicidienase.chaosflix.entities.recording.Conferences;
import de.nicidienase.chaosflix.entities.recording.Event;
import de.nicidienase.chaosflix.entities.recording.Recording;
import de.nicidienase.chaosflix.entities.streaming.LiveConference;
import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Path;
/**
* Created by felix on 24.03.17.
@ -23,21 +28,35 @@ import retrofit2.converter.gson.GsonConverterFactory;
public class MediaApiService extends Service {
private final IBinder mBinder = new LocalBinder();
private final MediaCCCService mApiService;
private final RecordingService mRecordingApiService;
private final StreamingService mStreamingApiService;
public class LocalBinder extends Binder {
MediaApiService getService() {
public MediaApiService getService() {
return MediaApiService.this;
}
}
public MediaApiService(){
Retrofit retrofit = new Retrofit.Builder()
OkHttpClient client = new OkHttpClient();
GsonConverterFactory gsonConverterFactory = GsonConverterFactory.create();
RxJava2CallAdapterFactory rxJava2CallAdapterFactory = RxJava2CallAdapterFactory.create();
Retrofit retrofitRecordings = new Retrofit.Builder()
.baseUrl(getString(R.string.api_media_ccc_url))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build();
mApiService = retrofit.create(MediaCCCService.class);
mRecordingApiService = retrofitRecordings.create(RecordingService.class);
Retrofit retrofigStreaming = new Retrofit.Builder()
.baseUrl(getString(R.string.streaming_media_ccc_url))
.client(client)
.addConverterFactory(gsonConverterFactory)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.build();
mStreamingApiService = retrofigStreaming.create(StreamingService.class);
}
@Nullable
@ -46,4 +65,27 @@ public class MediaApiService extends Service {
return mBinder;
}
public Observable<Conferences> getConferences(){
return mRecordingApiService.getConferences();
};
public Observable<Conference> getConference(int id){
return mRecordingApiService.getConference(id);
}
public Observable<List<Event>> getEvents(){
return mRecordingApiService.getAllEvents();
}
public Observable<Event> getEvent(int id){
return mRecordingApiService.getEvent(id);
}
public Observable<Recording> getRecording(int id){
return mRecordingApiService.getRecording(id);
}
public Observable<List<LiveConference>> getStreamingConferences(){
return mStreamingApiService.getStreamingConferences();
}
}

View file

@ -6,6 +6,7 @@ import de.nicidienase.chaosflix.entities.recording.Conference;
import de.nicidienase.chaosflix.entities.recording.Conferences;
import de.nicidienase.chaosflix.entities.recording.Event;
import de.nicidienase.chaosflix.entities.recording.Recording;
import io.reactivex.Observable;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Path;
@ -33,92 +34,27 @@ public class RecordingClient implements RecordingService {
@Override
public Call<Conferences> listConferences() {
return service.listConferences();
public Observable<Conferences> getConferences() {
return service.getConferences();
}
@Override
public Call<Conference> getConference(@Path("id") int id) {
public Observable<Conference> getConference(@Path("id") int id) {
return service.getConference(id);
}
@Override
public Call<List<Event>> getEvents() {
return service.getEvents();
public Observable<List<Event>> getAllEvents() {
return service.getAllEvents();
}
@Override
public Call<Event> getEvent(@Path("id") int id) {
public Observable<Event> getEvent(@Path("id") int id) {
return service.getEvent(id);
}
@Override
public Call<Recording> getRecording(@Path("id") int id) {
public Observable<Recording> getRecording(@Path("id") int id) {
return service.getRecording(id);
}
public void init(){
try {
long time = System.currentTimeMillis();
List<Conference> conferences = this.listConferences().execute().body().getConferences();
for(Conference conf : conferences){
conf.setId((long) conf.getApiID());
conf.save();
for(Event e :conf.getEvents()){
e.setParentConferenceID(conf.getId());
e.setId((long) e.getApiID());
e.save();
for(Recording r : e.getRecordings()){
r.setParentEventID(e.getId());
r.setId((long) r.getApiID());
r.save();
}
}
}
Log.d(TAG,String.format("updated entities in $1%i ms", System.currentTimeMillis() - time));
} catch (IOException e) {
e.printStackTrace();
}
}
public void update(){
try {
long time = System.currentTimeMillis();
List<Conference> conferences = this.listConferences().execute().body().getConferences();
for(Conference conf : conferences){
Conference conferenceDB = Conference.findById(Conference.class, conf.getApiID());
if(conferenceDB == null){
conf.setId((long) conf.getApiID());
conf.save();
} else {
conferenceDB.update(conf);
}
List<Event> eventList = this.getConference(conf.getApiID()).execute().body().getEvents();
for(Event e :eventList){
Event eventDB = Event.findById(Event.class, e.getApiID());
if(eventDB == null){
eventDB.setParentConferenceID(conf.getId());
eventDB.setId((long) eventDB.getApiID());
eventDB.save();
} else {
eventDB.update(e);
}
List<Recording> recordingList = this.getEvent(e.getApiID()).execute().body().getRecordings();
for(Recording r : recordingList){
Recording recordingDB = Recording.findById(Recording.class, r.getApiID());
if(recordingDB == null){
r.setParentEventID(eventDB.getId());
r.setId((long) r.getApiID());
r.save();
} else {
recordingDB.update(r);
}
}
}
}
Log.d(TAG,String.format("updated entities in $1%i ms", System.currentTimeMillis() - time));
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -17,18 +17,18 @@ import retrofit2.http.Path;
public interface RecordingService {
@GET("public/conferences")
Call<Conferences> listConferences();
Observable<Conferences> getConferences();
@GET("public/conferences/{id}")
Call<Conference> getConference(@Path("id") int id);
Observable<Conference> getConference(@Path("id") int id);
@GET("public/events")
Call<List<Event>> getEvents();
Observable<List<Event>> getAllEvents();
@GET("public/events/{id}")
Call<Event> getEvent(@Path("id") int id);
Observable<Event> getEvent(@Path("id") int id);
@GET("public/recordings/{id}")
Call<Recording> getRecording(@Path("id") int id);
Observable<Recording> getRecording(@Path("id") int id);
}

View file

@ -3,6 +3,7 @@ package de.nicidienase.chaosflix.network;
import java.util.List;
import de.nicidienase.chaosflix.entities.streaming.LiveConference;
import io.reactivex.Observable;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@ -15,7 +16,7 @@ public class StreamingClient implements StreamingService {
private StreamingService service;
public StreamingClient(){
this("https://api.media.ccc.de");
this("https://streaming.media.ccc.de");
}
public StreamingClient(String serviceUrl){
@ -27,7 +28,7 @@ public class StreamingClient implements StreamingService {
}
@Override
public Call<List<StreamingConference>> getStreamingConferences() {
public Observable<List<LiveConference>> getStreamingConferences() {
return service.getStreamingConferences();
}
}

View file

@ -13,5 +13,5 @@ import retrofit2.http.GET;
public interface StreamingService {
@GET("streams/v2.json")
Call<List<StreamingConference>> getStreamingConferences();
Observable<List<LiveConference>> getStreamingConferences();
}

View file

@ -1,7 +1,6 @@
package de.nicidienase.chaosflix;
import com.google.common.collect.Lists;
import static org.junit.Assert.*;
@ -26,88 +25,81 @@ public class RecordingClientTest {
private RecordingClient client;
@Before
public void beforeTest(){
public void beforeTest() {
this.client = new RecordingClient();
}
@Test
public void getConferenceTest() throws IOException {
Conference conference = client.getConference(101).execute().body();
assertEquals(conference.getAcronym(),"33c3");
client.getConference(101).subscribe
(conference -> assertEquals("33c3", conference.getAcronym()));
}
@Test
public void getEventTest() throws IOException {
Event event = this.client.getEvent(3674).execute().body();
assertEquals(event.getGuid(),"bfc2ab1f-8384-4d7d-801a-dde8c81e039c");
client.getEvent(3674).subscribe(
event -> assertEquals("bfc2ab1f-8384-4d7d-801a-dde8c81e039c", event.getGuid()));
}
@Test
public void getEventRecordingsTest() throws IOException {
Event event = this.client.getEvent(3674).execute().body();
assertEquals(event.getRecordings().size(),9);
client.getEvent(3674).subscribe(event -> assertEquals(9, event.getRecordings().size()));
}
@Test
public void getRecordingTest() throws IOException {
Recording recording = this.client.getRecording(14142).execute().body();
assertEquals(recording.getUpdatedAt(), "2016-12-29T03:16:16.105+01:00");
client.getRecording(14142).subscribe(
recording -> assertEquals("2016-12-29T03:16:16.105+01:00",
recording.getUpdatedAt()));
}
@Test
public void getConferencEventListTest() throws IOException {
Conferences conferences = this.client.listConferences().execute().body();
assertEquals(99,conferences.getConferences().size());
client.getConferences().subscribe(conferences ->
assertEquals(99, conferences.getConferences().size()));
}
@Test
public void eventTagsTest() throws IOException {
Conference conference = this.client.getConference(101).execute().body();
assertEquals(12,conference.getEventsByTags().keySet().size());
client.getConference(101).subscribe(
conference -> assertEquals(12, conference.getEventsByTags().keySet().size()));
}
@Test
public void sortTest() throws IOException {
final RecordingClient client = this.client;
Conferences conferences = client.listConferences().execute().body();
Collections.sort(conferences.getConferences());
for (Conference conf : conferences.getConferences()) {
List<Event> events =
client.getConference(conf.getApiID()).execute().body().getEvents();
Collections.sort(events);
}
client.getConferences().subscribe(conferences -> {
Collections.sort(conferences.getConferences());
for (Conference conf : conferences.getConferences()) {
client.getConference(conf.getApiID())
.subscribe(conference -> Collections.sort(conference.getEvents()));
}
});
}
@Test
public void sortAllEvents() throws IOException {
RecordingClient client = this.client;
Collections.sort(client.getEvents().execute().body());
client.getAllEvents().subscribe(events -> Collections.sort(events));
}
@Test
public void mrmcd13() throws IOException {
RecordingClient client = this.client;
Conference conference = client.getConference(38).execute().body();
ArrayList<String> keyList = Lists.newArrayList(conference.getEventsByTags().keySet());
Collections.sort(keyList);
client.getConference(38).subscribe(conference ->
Collections.sort(Lists.newArrayList(conference.getEventsByTags().keySet())));
}
@Test
public void testTagsToTalksRation() throws IOException {
List<Conference> conferences = client.listConferences().execute().body().getConferences();
for(Conference conf : conferences){
Conference con = client.getConference(conf.getApiID()).execute().body();
System.out.print(con.getAcronym() + ": " + con.getEventsByTags().keySet());
float sum = 0;
for(Event e : con.getEvents()){
sum += e.getTags().size();
client.getConferences().subscribe(conferences -> {
for (Conference conf : conferences.getConferences()) {
client.getConference(conf.getApiID()).subscribe(conference -> {
System.out.print(conference.getAcronym() + ": " + conference.getEventsByTags().keySet());
float sum = 0;
for (Event e : conference.getEvents()) {
sum += e.getTags().size();
}
});
}
// System.out.println("\t\t " + sum/con.getEvents().size() + "\n\t" + con.getEventsByTags().keySet());
// HashMap<String, List<Event>> events = con.getEventsByTags();
// for(String tag: events.keySet()){
// System.out.println("Ration: " + events.keySet().size() + "/" + events.get(tag).size());
// }
}
});
}
}

View file

@ -14,9 +14,6 @@ import de.nicidienase.chaosflix.network.StreamingClient;
import de.nicidienase.chaosflix.network.StreamingService;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by felix on 23.03.17.
@ -33,7 +30,8 @@ public class StreamingClientTest {
server = new MockWebServer();
server.start();
String serverUrl = server.url("/").toString();
service = new StreamingClient(serverUrl);
// service = new StreamingClient(serverUrl);
service = new StreamingClient();
}
@Before
@ -43,16 +41,13 @@ public class StreamingClientTest {
@Test
public void test1() throws IOException {
List<StreamingConference> streamingConferences
= service.getStreamingConferences().execute().body();
assertEquals(1,streamingConferences.size());
service.getStreamingConferences().subscribe((List<LiveConference> liveConferences) -> assertEquals(1, liveConferences.size()));
}
@Test
public void test2() throws IOException {
List<StreamingConference> streamingConferences
= service.getStreamingConferences().execute().body();
StreamingConference streamingConference = streamingConferences.get(0);
assertEquals("FOSSGIS 2017",streamingConference.getConference());
service.getStreamingConferences().subscribe(
(List<LiveConference> liveConferences) -> assertEquals("FOSSGIS 2017", liveConferences.get(0).getConference()));
}
}