1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-14 10:47:30 +00:00

Move StoriesService object to StoriesRepository constructor parameter.

This commit is contained in:
Ammar Githam 2021-06-12 08:49:49 +09:00
parent 4f8de4e7df
commit 3d69148d9b
7 changed files with 67 additions and 45 deletions

View File

@ -298,7 +298,7 @@ public class HashTagFragment extends Fragment implements SwipeRefreshLayout.OnRe
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
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.INSTANCE : null; storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null;
graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE;
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -291,7 +291,7 @@ public class LocationFragment extends Fragment implements SwipeRefreshLayout.OnR
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
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.INSTANCE; storiesRepository = StoriesRepository.Companion.getInstance();
graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE;
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -136,7 +136,7 @@ public final class StoryListViewerFragment extends Fragment implements SwipeRefr
context = getContext(); context = getContext();
if (context == null) return; if (context == null) return;
setHasOptionsMenu(true); setHasOptionsMenu(true);
storiesRepository = StoriesRepository.INSTANCE; storiesRepository = StoriesRepository.Companion.getInstance();
} }
@NonNull @NonNull

View File

@ -162,7 +162,7 @@ public class StoryViewerFragment extends Fragment {
userId = CookieUtils.getUserIdFromCookie(cookie); userId = CookieUtils.getUserIdFromCookie(cookie);
deviceId = settingsHelper.getString(Constants.DEVICE_UUID); deviceId = settingsHelper.getString(Constants.DEVICE_UUID);
fragmentActivity = (AppCompatActivity) requireActivity(); fragmentActivity = (AppCompatActivity) requireActivity();
storiesRepository = StoriesRepository.INSTANCE; storiesRepository = StoriesRepository.Companion.getInstance();
mediaService = MediaService.INSTANCE; mediaService = MediaService.INSTANCE;
directMessagesService = DirectMessagesService.INSTANCE; directMessagesService = DirectMessagesService.INSTANCE;
setHasOptionsMenu(true); setHasOptionsMenu(true);

View File

@ -276,7 +276,7 @@ public class FeedFragment extends Fragment implements SwipeRefreshLayout.OnRefre
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
fragmentActivity = (MainActivity) requireActivity(); fragmentActivity = (MainActivity) requireActivity();
storiesRepository = StoriesRepository.INSTANCE; storiesRepository = StoriesRepository.Companion.getInstance();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -335,7 +335,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
fragmentActivity = (MainActivity) requireActivity(); fragmentActivity = (MainActivity) requireActivity();
friendshipRepository = isLoggedIn ? FriendshipRepository.Companion.getInstance() : null; friendshipRepository = isLoggedIn ? FriendshipRepository.Companion.getInstance() : null;
directMessagesService = isLoggedIn ? DirectMessagesService.INSTANCE : null; directMessagesService = isLoggedIn ? DirectMessagesService.INSTANCE : null;
storiesRepository = isLoggedIn ? StoriesRepository.INSTANCE : null; storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null;
mediaService = isLoggedIn ? MediaService.INSTANCE : null; mediaService = isLoggedIn ? MediaService.INSTANCE : null;
userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null; userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null;
graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE;

View File

@ -19,8 +19,7 @@ import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import java.util.* import java.util.*
object StoriesRepository { class StoriesRepository(private val service: StoriesService) {
private val service: StoriesService = retrofit.create(StoriesService::class.java)
suspend fun fetch(mediaId: Long): StoryModel { suspend fun fetch(mediaId: Long): StoryModel {
val response = service.fetch(mediaId) val response = service.fetch(mediaId)
@ -41,7 +40,8 @@ object StoriesRepository {
if (node.optBoolean("hide_from_feed_unit") && Utils.settingsHelper.getBoolean(PreferenceKeys.HIDE_MUTED_REELS)) continue if (node.optBoolean("hide_from_feed_unit") && Utils.settingsHelper.getBoolean(PreferenceKeys.HIDE_MUTED_REELS)) continue
val userJson = node.getJSONObject(if (node.has("user")) "user" else "owner") val userJson = node.getJSONObject(if (node.has("user")) "user" else "owner")
try { try {
val user = User(userJson.getLong("pk"), val user = User(
userJson.getLong("pk"),
userJson.getString("username"), userJson.getString("username"),
userJson.optString("full_name"), userJson.optString("full_name"),
userJson.optBoolean("is_private"), userJson.optBoolean("is_private"),
@ -55,7 +55,8 @@ object StoriesRepository {
if (itemJson != null) { if (itemJson != null) {
firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, null) firstStoryModel = ResponseBodyUtils.parseStoryItem(itemJson, false, null)
} }
feedStoryModels.add(FeedStoryModel( feedStoryModels.add(
FeedStoryModel(
node.getString("id"), node.getString("id"),
user, user,
fullyRead, fullyRead,
@ -63,7 +64,9 @@ object StoriesRepository {
firstStoryModel, firstStoryModel,
node.getInt("media_count"), node.getInt("media_count"),
false, false,
node.optBoolean("has_besties_media"))) node.optBoolean("has_besties_media")
)
)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "parseStoriesBody: ", e) Log.e(TAG, "parseStoriesBody: ", e)
} // to cover promotional reels with non-long user pk's } // to cover promotional reels with non-long user pk's
@ -72,14 +75,16 @@ object StoriesRepository {
for (i in 0 until broadcasts.length()) { for (i in 0 until broadcasts.length()) {
val node = broadcasts.getJSONObject(i) val node = broadcasts.getJSONObject(i)
val userJson = node.getJSONObject("broadcast_owner") val userJson = node.getJSONObject("broadcast_owner")
val user = User(userJson.getLong("pk"), val user = User(
userJson.getLong("pk"),
userJson.getString("username"), userJson.getString("username"),
userJson.optString("full_name"), userJson.optString("full_name"),
userJson.optBoolean("is_private"), userJson.optBoolean("is_private"),
userJson.getString("profile_pic_url"), userJson.getString("profile_pic_url"),
userJson.optBoolean("is_verified") userJson.optBoolean("is_verified")
) )
feedStoryModels.add(FeedStoryModel( feedStoryModels.add(
FeedStoryModel(
node.getString("id"), node.getString("id"),
user, user,
false, false,
@ -88,7 +93,8 @@ object StoriesRepository {
1, 1,
isLive = true, isLive = true,
isBestie = false isBestie = false
)) )
)
} }
return sort(feedStoryModels) return sort(feedStoryModels)
} }
@ -100,7 +106,8 @@ object StoriesRepository {
val highlightModels: MutableList<HighlightModel> = ArrayList() val highlightModels: MutableList<HighlightModel> = ArrayList()
for (i in 0 until length) { for (i in 0 until length) {
val highlightNode = highlightsReel.getJSONObject(i) val highlightNode = highlightsReel.getJSONObject(i)
highlightModels.add(HighlightModel( highlightModels.add(
HighlightModel(
highlightNode.getString("title"), highlightNode.getString("title"),
highlightNode.getString(Constants.EXTRAS_ID), highlightNode.getString(Constants.EXTRAS_ID),
highlightNode.getJSONObject("cover_media") highlightNode.getJSONObject("cover_media")
@ -108,7 +115,8 @@ object StoriesRepository {
.getString("url"), .getString("url"),
highlightNode.getLong("latest_reel_media"), highlightNode.getLong("latest_reel_media"),
highlightNode.getInt("media_count") highlightNode.getInt("media_count")
)) )
)
} }
return highlightModels return highlightModels
} }
@ -129,13 +137,15 @@ object StoriesRepository {
val highlightModels: MutableList<HighlightModel> = ArrayList() val highlightModels: MutableList<HighlightModel> = ArrayList()
for (i in 0 until length) { for (i in 0 until length) {
val highlightNode = highlightsReel.getJSONObject(i) val highlightNode = highlightsReel.getJSONObject(i)
highlightModels.add(HighlightModel( highlightModels.add(
HighlightModel(
null, null,
highlightNode.getString(Constants.EXTRAS_ID), highlightNode.getString(Constants.EXTRAS_ID),
highlightNode.getJSONObject("cover_image_version").getString("url"), highlightNode.getJSONObject("cover_image_version").getString("url"),
highlightNode.getLong("latest_reel_media"), highlightNode.getLong("latest_reel_media"),
highlightNode.getInt("media_count") highlightNode.getInt("media_count")
)) )
)
} }
return ArchiveFetchResponse(highlightModels, data.getBoolean("more_available"), data.getString("max_id")) return ArchiveFetchResponse(highlightModels, data.getBoolean("more_available"), data.getString("max_id"))
} }
@ -306,4 +316,16 @@ object StoriesRepository {
return hasNextPage return hasNextPage
} }
} }
companion object {
@Volatile
private var INSTANCE: StoriesRepository? = null
fun getInstance(): StoriesRepository {
return INSTANCE ?: synchronized(this) {
val service: StoriesService = retrofit.create(StoriesService::class.java)
StoriesRepository(service).also { INSTANCE = it }
}
}
}
} }