Move GraphQLService object to GraphQLRepository constructor parameter.

This commit is contained in:
Ammar Githam 2021-06-12 12:39:55 +09:00
parent 69b685ae0d
commit 205e21b07c
11 changed files with 36 additions and 22 deletions

View File

@ -104,6 +104,7 @@ class MainActivity : BaseLanguageActivity(), FragmentManager.OnBackStackChangedL
} }
} }
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() } private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() }
private val graphQLRepository: GraphQLRepository by lazy { GraphQLRepository.getInstance() }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -638,7 +639,7 @@ class MainActivity : BaseLanguageActivity(), FragmentManager.OnBackStackChangedL
alertDialog.show() alertDialog.show()
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
try { try {
val media = if (isLoggedIn) mediaRepository.fetch(shortcodeToId(shortCode)) else GraphQLRepository.fetchPost(shortCode) val media = if (isLoggedIn) mediaRepository.fetch(shortcodeToId(shortCode)) else graphQLRepository.fetchPost(shortCode)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
if (media == null) { if (media == null) {
Toast.makeText(applicationContext, R.string.post_not_found, Toast.LENGTH_SHORT).show() Toast.makeText(applicationContext, R.string.post_not_found, Toast.LENGTH_SHORT).show()

View File

@ -25,7 +25,7 @@ public class HashtagPostFetchService implements PostFetcher.PostFetchService {
this.hashtagModel = hashtagModel; this.hashtagModel = hashtagModel;
this.isLoggedIn = isLoggedIn; this.isLoggedIn = isLoggedIn;
tagsService = isLoggedIn ? TagsService.getInstance() : null; tagsService = isLoggedIn ? TagsService.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
} }
@Override @Override

View File

@ -25,7 +25,7 @@ public class LocationPostFetchService implements PostFetcher.PostFetchService {
this.locationModel = locationModel; this.locationModel = locationModel;
this.isLoggedIn = isLoggedIn; this.isLoggedIn = isLoggedIn;
locationService = isLoggedIn ? LocationService.getInstance() : null; locationService = isLoggedIn ? LocationService.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
} }
@Override @Override

View File

@ -25,7 +25,7 @@ public class ProfilePostFetchService implements PostFetcher.PostFetchService {
public ProfilePostFetchService(final User profileModel, final boolean isLoggedIn) { public ProfilePostFetchService(final User profileModel, final boolean isLoggedIn) {
this.profileModel = profileModel; this.profileModel = profileModel;
this.isLoggedIn = isLoggedIn; this.isLoggedIn = isLoggedIn;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
profileService = isLoggedIn ? ProfileService.getInstance() : null; profileService = isLoggedIn ? ProfileService.getInstance() : null;
} }

View File

@ -29,7 +29,7 @@ public class SavedPostFetchService implements PostFetcher.PostFetchService {
this.type = type; this.type = type;
this.isLoggedIn = isLoggedIn; this.isLoggedIn = isLoggedIn;
this.collectionId = collectionId; this.collectionId = collectionId;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
profileService = isLoggedIn ? ProfileService.getInstance() : null; profileService = isLoggedIn ? ProfileService.getInstance() : null;
} }

View File

@ -299,7 +299,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
tagsService = isLoggedIn ? TagsService.getInstance() : null; tagsService = isLoggedIn ? TagsService.getInstance() : null;
storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null; storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -113,7 +113,7 @@ public final class LikesViewerFragment extends BottomSheetDialogFragment impleme
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
if (csrfToken == null) return; if (csrfToken == null) return;
mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null; mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
// setHasOptionsMenu(true); // setHasOptionsMenu(true);
} }

View File

@ -292,7 +292,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR
isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0; isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) > 0;
locationService = isLoggedIn ? LocationService.getInstance() : null; locationService = isLoggedIn ? LocationService.getInstance() : null;
storiesRepository = StoriesRepository.Companion.getInstance(); storiesRepository = StoriesRepository.Companion.getInstance();
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -338,7 +338,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null; storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null;
mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null; mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null;
userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null; userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null;
graphQLRepository = isLoggedIn ? null : GraphQLRepository.INSTANCE; graphQLRepository = isLoggedIn ? null : GraphQLRepository.Companion.getInstance();
final Context context = getContext(); final Context context = getContext();
if (context == null) return; if (context == null) return;
accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context)); accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(context));

View File

@ -121,7 +121,7 @@ public class CommentsViewerViewModel extends ViewModel {
}; };
public CommentsViewerViewModel() { public CommentsViewerViewModel() {
graphQLRepository = GraphQLRepository.INSTANCE; graphQLRepository = GraphQLRepository.Companion.getInstance();
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); final String deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);

View File

@ -7,13 +7,11 @@ import awais.instagrabber.repositories.responses.*
import awais.instagrabber.utils.Constants import awais.instagrabber.utils.Constants
import awais.instagrabber.utils.ResponseBodyUtils import awais.instagrabber.utils.ResponseBodyUtils
import awais.instagrabber.utils.extensions.TAG import awais.instagrabber.utils.extensions.TAG
import awais.instagrabber.webservices.RetrofitFactory.retrofitWeb
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
object GraphQLRepository { class GraphQLRepository(private val service: GraphQLService) {
private val service: GraphQLService = retrofitWeb.create(GraphQLService::class.java)
// TODO convert string response to a response class // TODO convert string response to a response class
private suspend fun fetch( private suspend fun fetch(
@ -145,14 +143,16 @@ object GraphQLRepository {
val userModels: MutableList<User> = ArrayList() val userModels: MutableList<User> = ArrayList()
for (j in 0 until usersLen) { for (j in 0 until usersLen) {
val userObject = users.getJSONObject(j).getJSONObject("node") val userObject = users.getJSONObject(j).getJSONObject("node")
userModels.add(User( userModels.add(
userObject.getLong("id"), User(
userObject.getString("username"), userObject.getLong("id"),
userObject.optString("full_name"), userObject.getString("username"),
userObject.optBoolean("is_private"), userObject.optString("full_name"),
userObject.getString("profile_pic_url"), userObject.optBoolean("is_private"),
userObject.optBoolean("is_verified") userObject.getString("profile_pic_url"),
)) userObject.optBoolean("is_verified")
)
)
} }
return GraphQLUserListFetchResponse(newEndCursor, status, userModels) return GraphQLUserListFetchResponse(newEndCursor, status, userModels)
} }
@ -240,7 +240,8 @@ object GraphQLRepository {
body.getString("name"), body.getString("name"),
timelineMedia.getLong("count"), timelineMedia.getLong("count"),
if (body.optBoolean("is_following")) FollowingType.FOLLOWING else FollowingType.NOT_FOLLOWING, if (body.optBoolean("is_following")) FollowingType.FOLLOWING else FollowingType.NOT_FOLLOWING,
null) null
)
} }
// TODO convert string response to a response class // TODO convert string response to a response class
@ -263,4 +264,16 @@ object GraphQLRepository {
body.optDouble("lat", 0.0) body.optDouble("lat", 0.0)
) )
} }
companion object {
@Volatile
private var INSTANCE: GraphQLRepository? = null
fun getInstance(): GraphQLRepository {
return INSTANCE ?: synchronized(this) {
val service: GraphQLService = RetrofitFactory.retrofitWeb.create(GraphQLService::class.java)
GraphQLRepository(service).also { INSTANCE = it }
}
}
}
} }