Browse Source
Convert UserRepository and UserService to kotlin
renovate/org.robolectric-robolectric-4.x
Convert UserRepository and UserService to kotlin
renovate/org.robolectric-robolectric-4.x
Ammar Githam
4 years ago
7 changed files with 148 additions and 256 deletions
-
41app/src/main/java/awais/instagrabber/dialogs/ProfilePicDialogFragment.java
-
66app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
72app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
30app/src/main/java/awais/instagrabber/repositories/UserRepository.kt
-
21app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java
-
60app/src/main/java/awais/instagrabber/viewmodels/UserSearchViewModel.java
-
110app/src/main/java/awais/instagrabber/webservices/UserService.kt
@ -1,25 +1,25 @@ |
|||
package awais.instagrabber.repositories; |
|||
package awais.instagrabber.repositories |
|||
|
|||
import awais.instagrabber.repositories.responses.FriendshipStatus; |
|||
import awais.instagrabber.repositories.responses.UserSearchResponse; |
|||
import awais.instagrabber.repositories.responses.WrappedUser; |
|||
import retrofit2.Call; |
|||
import retrofit2.http.GET; |
|||
import retrofit2.http.Path; |
|||
import retrofit2.http.Query; |
|||
|
|||
public interface UserRepository { |
|||
import awais.instagrabber.repositories.responses.FriendshipStatus |
|||
import awais.instagrabber.repositories.responses.UserSearchResponse |
|||
import awais.instagrabber.repositories.responses.WrappedUser |
|||
import retrofit2.http.GET |
|||
import retrofit2.http.Path |
|||
import retrofit2.http.Query |
|||
|
|||
interface UserRepository { |
|||
@GET("/api/v1/users/{uid}/info/") |
|||
Call<WrappedUser> getUserInfo(@Path("uid") final long uid); |
|||
suspend fun getUserInfo(@Path("uid") uid: Long): WrappedUser |
|||
|
|||
@GET("/api/v1/users/{username}/usernameinfo/") |
|||
Call<WrappedUser> getUsernameInfo(@Path("username") final String username); |
|||
suspend fun getUsernameInfo(@Path("username") username: String): WrappedUser |
|||
|
|||
@GET("/api/v1/friendships/show/{uid}/") |
|||
Call<FriendshipStatus> getUserFriendship(@Path("uid") final long uid); |
|||
suspend fun getUserFriendship(@Path("uid") uid: Long): FriendshipStatus |
|||
|
|||
@GET("/api/v1/users/search/") |
|||
Call<UserSearchResponse> search(@Query("timezone_offset") float timezoneOffset, |
|||
@Query("q") String query); |
|||
suspend fun search( |
|||
@Query("timezone_offset") timezoneOffset: Float, |
|||
@Query("q") query: String, |
|||
): UserSearchResponse |
|||
} |
@ -1,101 +1,29 @@ |
|||
package awais.instagrabber.webservices; |
|||
package awais.instagrabber.webservices |
|||
|
|||
import androidx.annotation.NonNull; |
|||
import awais.instagrabber.repositories.UserRepository |
|||
import awais.instagrabber.repositories.responses.FriendshipStatus |
|||
import awais.instagrabber.repositories.responses.User |
|||
import awais.instagrabber.repositories.responses.UserSearchResponse |
|||
import awais.instagrabber.webservices.RetrofitFactory.retrofit |
|||
import java.util.* |
|||
|
|||
import java.util.TimeZone; |
|||
object UserService : BaseService() { |
|||
private val repository: UserRepository = retrofit.create(UserRepository::class.java) |
|||
|
|||
import awais.instagrabber.repositories.UserRepository; |
|||
import awais.instagrabber.repositories.responses.FriendshipStatus; |
|||
import awais.instagrabber.repositories.responses.User; |
|||
import awais.instagrabber.repositories.responses.UserSearchResponse; |
|||
import awais.instagrabber.repositories.responses.WrappedUser; |
|||
import retrofit2.Call; |
|||
import retrofit2.Callback; |
|||
import retrofit2.Response; |
|||
|
|||
public class UserService extends BaseService { |
|||
private static final String TAG = UserService.class.getSimpleName(); |
|||
|
|||
private final UserRepository repository; |
|||
|
|||
private static UserService instance; |
|||
|
|||
private UserService() { |
|||
repository = RetrofitFactory.INSTANCE |
|||
.getRetrofit() |
|||
.create(UserRepository.class); |
|||
suspend fun getUserInfo(uid: Long): User { |
|||
val response = repository.getUserInfo(uid) |
|||
return response.user |
|||
} |
|||
|
|||
public static UserService getInstance() { |
|||
if (instance == null) { |
|||
instance = new UserService(); |
|||
} |
|||
return instance; |
|||
} |
|||
|
|||
public void getUserInfo(final long uid, final ServiceCallback<User> callback) { |
|||
final Call<WrappedUser> request = repository.getUserInfo(uid); |
|||
request.enqueue(new Callback<WrappedUser>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<WrappedUser> call, @NonNull final Response<WrappedUser> response) { |
|||
final WrappedUser user = response.body(); |
|||
if (user == null) { |
|||
callback.onSuccess(null); |
|||
return; |
|||
} |
|||
callback.onSuccess(user.getUser()); |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<WrappedUser> call, @NonNull final Throwable t) { |
|||
callback.onFailure(t); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void getUsernameInfo(final String username, final ServiceCallback<User> callback) { |
|||
final Call<WrappedUser> request = repository.getUsernameInfo(username); |
|||
request.enqueue(new Callback<WrappedUser>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<WrappedUser> call, @NonNull final Response<WrappedUser> response) { |
|||
final WrappedUser user = response.body(); |
|||
if (user == null) { |
|||
callback.onFailure(null); |
|||
return; |
|||
} |
|||
callback.onSuccess(user.getUser()); |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<WrappedUser> call, @NonNull final Throwable t) { |
|||
callback.onFailure(t); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
public void getUserFriendship(final long uid, final ServiceCallback<FriendshipStatus> callback) { |
|||
final Call<FriendshipStatus> request = repository.getUserFriendship(uid); |
|||
request.enqueue(new Callback<FriendshipStatus>() { |
|||
@Override |
|||
public void onResponse(@NonNull final Call<FriendshipStatus> call, @NonNull final Response<FriendshipStatus> response) { |
|||
final FriendshipStatus status = response.body(); |
|||
if (status == null) { |
|||
callback.onSuccess(null); |
|||
return; |
|||
} |
|||
callback.onSuccess(status); |
|||
} |
|||
|
|||
@Override |
|||
public void onFailure(@NonNull final Call<FriendshipStatus> call, @NonNull final Throwable t) { |
|||
callback.onFailure(t); |
|||
} |
|||
}); |
|||
suspend fun getUsernameInfo(username: String): User { |
|||
val response = repository.getUsernameInfo(username) |
|||
return response.user |
|||
} |
|||
|
|||
suspend fun getUserFriendship(uid: Long): FriendshipStatus = repository.getUserFriendship(uid) |
|||
|
|||
public Call<UserSearchResponse> search(final String query) { |
|||
final float timezoneOffset = (float) TimeZone.getDefault().getRawOffset() / 1000; |
|||
return repository.search(timezoneOffset, query); |
|||
suspend fun search(query: String): UserSearchResponse { |
|||
val timezoneOffset = TimeZone.getDefault().rawOffset.toFloat() / 1000 |
|||
return repository.search(timezoneOffset, query) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue