Skip to content

Commit

Permalink
Restructured
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainDaVinci committed Mar 27, 2020
1 parent 001df44 commit f84400a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 181 deletions.
Expand Up @@ -27,12 +27,11 @@
import androidx.appcompat.app.AppCompatActivity;

import com.example.application.musicdownloader.api.APIClientInstance;
import com.example.application.musicdownloader.api.github.GithubData;
import com.example.application.musicdownloader.api.github.GithubDataService;
import com.example.application.musicdownloader.api.server.ServerData;
import com.example.application.musicdownloader.api.server.ServerDataService;
import com.example.application.musicdownloader.api.youtube.YouTubeData;
import com.example.application.musicdownloader.api.youtube.YouTubeDataService;
import com.example.application.musicdownloader.api.github.model.GithubData;
import com.example.application.musicdownloader.api.github.service.GithubDataService;
import com.example.application.musicdownloader.api.server.model.ServerDownloadData;
import com.example.application.musicdownloader.api.server.model.ServerQueryData;
import com.example.application.musicdownloader.api.server.service.ServerDataService;
import com.example.application.musicdownloader.query.Encoding;
import com.example.application.musicdownloader.query.Quality;
import com.example.application.musicdownloader.query.Query;
Expand Down Expand Up @@ -101,7 +100,7 @@ private void checkForUpdate() {
}
});

GithubDataService service = APIClientInstance.getGithubRetrofitInstance().create(GithubDataService.class);
GithubDataService service = APIClientInstance.getGithubDataService();
Call<GithubData> call = service.getLatestRelease();

call.enqueue(new Callback<GithubData>() {
Expand Down Expand Up @@ -203,16 +202,12 @@ void searchQuery() {
setStatus("Searching", Color.BLACK);
spinningProgress.setVisibility(View.VISIBLE);

YouTubeDataService service = APIClientInstance.getYouTubeRetrofitInstance().create(YouTubeDataService.class);
Call<YouTubeData> call = service.getYouTubeData("snippet",
query.getSearch(),
"video",
"1",
APIClientInstance.YOUTUBE_API_KEY);
ServerDataService service = APIClientInstance.getServerDataService();
Call<ServerQueryData> call = service.getYouTubeId(query.getSearch());

call.enqueue(new Callback<YouTubeData>() {
call.enqueue(new Callback<ServerQueryData>() {
@Override
public void onResponse(Call<YouTubeData> call, Response<YouTubeData> response) {
public void onResponse(Call<ServerQueryData> call, Response<ServerQueryData> response) {
Log.i(TAG, "Obtained YouTube response");
if (!response.isSuccessful() || response.body() == null) {
spinningProgress.setVisibility(View.GONE);
Expand All @@ -227,15 +222,15 @@ public void onResponse(Call<YouTubeData> call, Response<YouTubeData> response) {
}

@Override
public void onFailure(Call<YouTubeData> call, Throwable t) {
public void onFailure(Call<ServerQueryData> call, Throwable t) {
spinningProgress.setVisibility(View.GONE);
setStatus("Oh, snap! Search failed", Color.BLACK);
Log.d(TAG, "YouTube error: " + t.getMessage());
}
});
}

private void showResponse(final YouTubeData data) {
private void showResponse(final ServerQueryData data) {
spinningProgress.setVisibility(View.GONE);
setStatus("", Color.BLACK);

Expand All @@ -246,7 +241,7 @@ private void showResponse(final YouTubeData data) {

Log.i(TAG, "Set information for confirmation dialog");
titleTextView.setText(data.getTitle());
setThumbnail(imageView, data.getThumbnailURL());
setThumbnail(imageView, data.getThumbnailUrl());

DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
switch (which) {
Expand Down Expand Up @@ -291,18 +286,18 @@ public void onResponse(okhttp3.Call call, okhttp3.Response response) {
});
}

private void getDownloadLink(final YouTubeData data) {
private void getDownloadLink(final ServerQueryData data) {
spinningProgress.setVisibility(View.VISIBLE);
setStatus("Converting...", Color.BLACK);

ServerDataService service = APIClientInstance.getServerRetrofitInstance().create(ServerDataService.class);
Call<ServerData> call = service.getDownloadLink(data.getId(),
ServerDataService service = APIClientInstance.getServerDataService();
Call<ServerDownloadData> call = service.getDownloadLink(data.getId(),
query.getEncoding().toString(),
query.getQuality().toString());

call.enqueue(new Callback<ServerData>() {
call.enqueue(new Callback<ServerDownloadData>() {
@Override
public void onResponse(Call<ServerData> call, Response<ServerData> response) {
public void onResponse(Call<ServerDownloadData> call, Response<ServerDownloadData> response) {
spinningProgress.setVisibility(View.GONE);
Log.i(TAG, "Obtained server response");

Expand All @@ -320,7 +315,7 @@ public void onResponse(Call<ServerData> call, Response<ServerData> response) {
}

@Override
public void onFailure(Call<ServerData> call, Throwable t) {
public void onFailure(Call<ServerDownloadData> call, Throwable t) {
spinningProgress.setVisibility(View.GONE);
Log.d(TAG, "Server error: " + t.getMessage());
setStatus("Oh, snap! Conversion failed.", Color.BLACK);
Expand Down
@@ -1,8 +1,7 @@
package com.example.application.musicdownloader.api;

import android.util.Log;

import com.example.application.musicdownloader.MainActivity;
import com.example.application.musicdownloader.api.github.service.GithubDataService;
import com.example.application.musicdownloader.api.server.service.ServerDataService;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;

import java.util.concurrent.TimeUnit;
Expand All @@ -12,27 +11,15 @@
import retrofit2.converter.gson.GsonConverterFactory;

public class APIClientInstance {
private static final String YOUTUBE_URL = "https://www.googleapis.com/youtube/v3/";
private static final String GITHUB_URL = "https://api.github.com/repos/CaptainDaVinci/Music-DL/releases/";
public static String YOUTUBE_API_KEY ;
private static String SERVER_URL;
private static Retrofit youTubeRetrofit, serverRetrofit, githubRetrofit;

public static Retrofit getYouTubeRetrofitInstance() {
YOUTUBE_API_KEY = FirebaseRemoteConfig.getInstance().getString("YOUTUBE_API_KEY");
Log.d(MainActivity.TAG, "YouTube API key " + YOUTUBE_API_KEY);
if (youTubeRetrofit == null) {
youTubeRetrofit = new Retrofit.Builder()
.baseUrl(YOUTUBE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return youTubeRetrofit;
}
private static Retrofit serverRetrofit, githubRetrofit;
private static ServerDataService serverDataService;
private static GithubDataService githubDataService;


public static Retrofit getServerRetrofitInstance() {
private static Retrofit getServerRetrofitInstance() {
SERVER_URL = FirebaseRemoteConfig.getInstance().getString("SERVER_URL");
Log.d(MainActivity.TAG, "Server URL " + SERVER_URL);
if (serverRetrofit == null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
Expand All @@ -49,7 +36,7 @@ public static Retrofit getServerRetrofitInstance() {
return serverRetrofit;
}

public static Retrofit getGithubRetrofitInstance() {
private static Retrofit getGithubRetrofitInstance() {
if (githubRetrofit == null) {
githubRetrofit = new Retrofit.Builder()
.baseUrl(GITHUB_URL)
Expand All @@ -58,4 +45,18 @@ public static Retrofit getGithubRetrofitInstance() {
}
return githubRetrofit;
}

public static ServerDataService getServerDataService() {
if (serverDataService == null) {
serverDataService = APIClientInstance.getServerRetrofitInstance().create(ServerDataService.class);
}
return serverDataService;
}

public static GithubDataService getGithubDataService() {
if (githubDataService == null) {
githubDataService = APIClientInstance.getGithubRetrofitInstance().create(GithubDataService.class);
}
return githubDataService;
}
}
@@ -1,4 +1,4 @@
package com.example.application.musicdownloader.api.github;
package com.example.application.musicdownloader.api.github.model;

import com.google.gson.annotations.SerializedName;

Expand Down
@@ -1,4 +1,6 @@
package com.example.application.musicdownloader.api.github;
package com.example.application.musicdownloader.api.github.service;

import com.example.application.musicdownloader.api.github.model.GithubData;

import retrofit2.Call;
import retrofit2.http.GET;
Expand Down

This file was deleted.

@@ -1,8 +1,8 @@
package com.example.application.musicdownloader.api.server;
package com.example.application.musicdownloader.api.server.model;

import com.google.gson.annotations.SerializedName;

public class ServerData {
public class ServerDownloadData {
@SerializedName("download_link")
private String downloadLink;

Expand Down
@@ -0,0 +1,26 @@
package com.example.application.musicdownloader.api.server.model;

import com.google.gson.annotations.SerializedName;

public class ServerQueryData {
@SerializedName("id")
private String id;

@SerializedName("title")
private String title;

@SerializedName("thumbnailUrl")
private String thumbnailUrl;

public String getId() {
return id;
}

public String getTitle() {
return title;
}

public String getThumbnailUrl() {
return thumbnailUrl;
}
}
@@ -0,0 +1,22 @@
package com.example.application.musicdownloader.api.server.service;

import com.example.application.musicdownloader.api.server.model.ServerDownloadData;
import com.example.application.musicdownloader.api.server.model.ServerQueryData;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface ServerDataService {
@GET("download")
Call<ServerDownloadData> getDownloadLink(
@Query("id") String id,
@Query("encoding") String format,
@Query("quality") String quality
);

@GET("id")
Call<ServerQueryData> getYouTubeId(
@Query("q") String query
);
}

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions app/src/main/res/xml/remote_config.xml
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>YOUTUBE_API_KEY</key>
<value>AIzaSyDBs8dEBTzXOac8jUDnInjvdLirGyMYNZI</value>
</entry>

<entry>
<key>SERVER_URL</key>
<value>http://3.84.203.42/</value>
Expand Down

0 comments on commit f84400a

Please sign in to comment.