1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

Move MediaService object to MediaRepository constructor parameter.

This commit is contained in:
Ammar Githam 2021-06-12 10:03:21 +09:00
parent b2cbc18dbb
commit 58e0863de5
9 changed files with 37 additions and 22 deletions

View File

@ -103,6 +103,7 @@ class MainActivity : BaseLanguageActivity(), FragmentManager.OnBackStackChangedL
isActivityCheckerServiceBound = false isActivityCheckerServiceBound = false
} }
} }
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -637,7 +638,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 GraphQLService.fetchPost(shortCode) val media = if (isLoggedIn) mediaRepository.fetch(shortcodeToId(shortCode)) else GraphQLService.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

@ -112,7 +112,7 @@ public final class LikesViewerFragment extends BottomSheetDialogFragment impleme
// final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID); // final String deviceUuid = settingsHelper.getString(Constants.DEVICE_UUID);
final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
if (csrfToken == null) return; if (csrfToken == null) return;
mediaRepository = isLoggedIn ? MediaRepository.INSTANCE : null; mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null;
graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE;
// setHasOptionsMenu(true); // setHasOptionsMenu(true);
} }

View File

@ -229,7 +229,7 @@ public final class NotificationsViewerFragment extends Fragment implements Swipe
deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID); deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID);
csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
friendshipRepository = FriendshipRepository.Companion.getInstance(); friendshipRepository = FriendshipRepository.Companion.getInstance();
mediaRepository = MediaRepository.INSTANCE; mediaRepository = MediaRepository.Companion.getInstance();
newsService = NewsService.getInstance(); newsService = NewsService.getInstance();
} }

View File

@ -163,7 +163,7 @@ public class StoryViewerFragment extends Fragment {
deviceId = settingsHelper.getString(Constants.DEVICE_UUID); deviceId = settingsHelper.getString(Constants.DEVICE_UUID);
fragmentActivity = (AppCompatActivity) requireActivity(); fragmentActivity = (AppCompatActivity) requireActivity();
storiesRepository = StoriesRepository.Companion.getInstance(); storiesRepository = StoriesRepository.Companion.getInstance();
mediaRepository = MediaRepository.INSTANCE; mediaRepository = MediaRepository.Companion.getInstance();
directMessagesService = DirectMessagesService.INSTANCE; directMessagesService = DirectMessagesService.INSTANCE;
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }

View File

@ -59,7 +59,7 @@ public class DiscoverFragment extends Fragment implements SwipeRefreshLayout.OnR
// final String cookie = Utils.settingsHelper.getString(Constants.COOKIE); // final String cookie = Utils.settingsHelper.getString(Constants.COOKIE);
// final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie); // final String csrfToken = CookieUtils.getCsrfTokenFromCookie(cookie);
// final long userId = CookieUtils.getUserIdFromCookie(cookie); // final long userId = CookieUtils.getUserIdFromCookie(cookie);
mediaRepository = MediaRepository.INSTANCE; mediaRepository = MediaRepository.Companion.getInstance();
} }
@Override @Override

View File

@ -336,7 +336,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
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.Companion.getInstance() : null; storiesRepository = isLoggedIn ? StoriesRepository.Companion.getInstance() : null;
mediaRepository = isLoggedIn ? MediaRepository.INSTANCE : null; mediaRepository = isLoggedIn ? MediaRepository.Companion.getInstance() : null;
userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null; userRepository = isLoggedIn ? UserRepository.Companion.getInstance() : null;
graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE; graphQLService = isLoggedIn ? null : GraphQLService.INSTANCE;
final Context context = getContext(); final Context context = getContext();

View File

@ -63,6 +63,7 @@ class ThreadManager(
private val inboxManager: InboxManager = if (pending) DirectMessagesManager.pendingInboxManager else DirectMessagesManager.inboxManager private val inboxManager: InboxManager = if (pending) DirectMessagesManager.pendingInboxManager else DirectMessagesManager.inboxManager
private val threadIdOrUserIds: ThreadIdOrUserIds = of(threadId) private val threadIdOrUserIds: ThreadIdOrUserIds = of(threadId)
private val friendshipRepository: FriendshipRepository by lazy { FriendshipRepository.getInstance() } private val friendshipRepository: FriendshipRepository by lazy { FriendshipRepository.getInstance() }
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() }
val thread: LiveData<DirectThread?> by lazy { val thread: LiveData<DirectThread?> by lazy {
distinctUntilChanged(map(inboxManager.getInbox()) { inboxResource: Resource<DirectInbox?>? -> distinctUntilChanged(map(inboxManager.getInbox()) { inboxResource: Resource<DirectInbox?>? ->
@ -453,7 +454,7 @@ class ThreadManager(
"4", "4",
null null
) )
MediaRepository.uploadFinish(csrfToken, userId, deviceUuid, uploadFinishOptions) mediaRepository.uploadFinish(csrfToken, userId, deviceUuid, uploadFinishOptions)
val broadcastResponse = DirectMessagesService.broadcastVoice( val broadcastResponse = DirectMessagesService.broadcastVoice(
csrfToken, csrfToken,
viewerId, viewerId,
@ -791,7 +792,7 @@ class ThreadManager(
"2", "2",
VideoOptions(duration / 1000f, emptyList(), 0, false) VideoOptions(duration / 1000f, emptyList(), 0, false)
) )
MediaRepository.uploadFinish(csrfToken, userId, deviceUuid, uploadFinishOptions) mediaRepository.uploadFinish(csrfToken, userId, deviceUuid, uploadFinishOptions)
val broadcastResponse = DirectMessagesService.broadcastVideo( val broadcastResponse = DirectMessagesService.broadcastVideo(
csrfToken, csrfToken,
viewerId, viewerId,

View File

@ -45,6 +45,7 @@ class PostViewV2ViewModel : ViewModel() {
private val deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID) private val deviceUuid = Utils.settingsHelper.getString(Constants.DEVICE_UUID)
private val csrfToken = getCsrfTokenFromCookie(cookie) private val csrfToken = getCsrfTokenFromCookie(cookie)
private val viewerId = getUserIdFromCookie(cookie) private val viewerId = getUserIdFromCookie(cookie)
private val mediaRepository: MediaRepository by lazy { MediaRepository.getInstance() }
lateinit var media: Media lateinit var media: Media
private set private set
@ -135,7 +136,7 @@ class PostViewV2ViewModel : ViewModel() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val mediaId = media.pk ?: return@launch val mediaId = media.pk ?: return@launch
val liked = MediaRepository.like(csrfToken!!, viewerId, deviceUuid, mediaId) val liked = mediaRepository.like(csrfToken!!, viewerId, deviceUuid, mediaId)
updateMediaLikeUnlike(data, liked) updateMediaLikeUnlike(data, liked)
} catch (e: Exception) { } catch (e: Exception) {
data.postValue(error(e.message, null)) data.postValue(error(e.message, null))
@ -154,7 +155,7 @@ class PostViewV2ViewModel : ViewModel() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val mediaId = media.pk ?: return@launch val mediaId = media.pk ?: return@launch
val unliked = MediaRepository.unlike(csrfToken!!, viewerId, deviceUuid, mediaId) val unliked = mediaRepository.unlike(csrfToken!!, viewerId, deviceUuid, mediaId)
updateMediaLikeUnlike(data, unliked) updateMediaLikeUnlike(data, unliked)
} catch (e: Exception) { } catch (e: Exception) {
data.postValue(error(e.message, null)) data.postValue(error(e.message, null))
@ -203,7 +204,7 @@ class PostViewV2ViewModel : ViewModel() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val mediaId = media.pk ?: return@launch val mediaId = media.pk ?: return@launch
val saved = MediaRepository.save(csrfToken!!, viewerId, deviceUuid, mediaId, collection) val saved = mediaRepository.save(csrfToken!!, viewerId, deviceUuid, mediaId, collection)
getSaveUnsaveCallback(data, saved, ignoreSaveState) getSaveUnsaveCallback(data, saved, ignoreSaveState)
} catch (e: Exception) { } catch (e: Exception) {
data.postValue(error(e.message, null)) data.postValue(error(e.message, null))
@ -221,7 +222,7 @@ class PostViewV2ViewModel : ViewModel() {
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
val mediaId = media.pk ?: return@launch val mediaId = media.pk ?: return@launch
val unsaved = MediaRepository.unsave(csrfToken!!, viewerId, deviceUuid, mediaId) val unsaved = mediaRepository.unsave(csrfToken!!, viewerId, deviceUuid, mediaId)
getSaveUnsaveCallback(data, unsaved, false) getSaveUnsaveCallback(data, unsaved, false)
} }
return data return data
@ -251,7 +252,7 @@ class PostViewV2ViewModel : ViewModel() {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val postId = media.pk ?: return@launch val postId = media.pk ?: return@launch
val result = MediaRepository.editCaption(csrfToken!!, viewerId, deviceUuid, postId, caption) val result = mediaRepository.editCaption(csrfToken!!, viewerId, deviceUuid, postId, caption)
if (result) { if (result) {
data.postValue(success("")) data.postValue(success(""))
media.setPostCaption(caption) media.setPostCaption(caption)
@ -278,7 +279,7 @@ class PostViewV2ViewModel : ViewModel() {
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val result = MediaRepository.translate(pk, "1") val result = mediaRepository.translate(pk, "1")
if (result.isBlank()) { if (result.isBlank()) {
data.postValue(error("", null)) data.postValue(error("", null))
return@launch return@launch
@ -315,7 +316,7 @@ class PostViewV2ViewModel : ViewModel() {
} }
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
val response = MediaRepository.delete(csrfToken!!, viewerId, deviceUuid, mediaId, mediaType) val response = mediaRepository.delete(csrfToken!!, viewerId, deviceUuid, mediaId, mediaType)
if (response == null) { if (response == null) {
data.postValue(success(Any())) data.postValue(success(Any()))
return@launch return@launch

View File

@ -12,13 +12,7 @@ import awais.instagrabber.utils.retryContextString
import awais.instagrabber.webservices.RetrofitFactory.retrofit import awais.instagrabber.webservices.RetrofitFactory.retrofit
import org.json.JSONObject import org.json.JSONObject
object MediaRepository { class MediaRepository(private val service: MediaService) {
private val DELETABLE_ITEMS_TYPES = listOf(
MediaItemType.MEDIA_TYPE_IMAGE,
MediaItemType.MEDIA_TYPE_VIDEO,
MediaItemType.MEDIA_TYPE_SLIDER
)
private val service: MediaService = retrofit.create(MediaService::class.java)
suspend fun fetch( suspend fun fetch(
mediaId: Long, mediaId: Long,
@ -179,4 +173,22 @@ object MediaRepository {
} }
return service.delete(postId, mediaType, signedForm) return service.delete(postId, mediaType, signedForm)
} }
companion object {
@Volatile
private var INSTANCE: MediaRepository? = null
private val DELETABLE_ITEMS_TYPES = listOf(
MediaItemType.MEDIA_TYPE_IMAGE,
MediaItemType.MEDIA_TYPE_VIDEO,
MediaItemType.MEDIA_TYPE_SLIDER
)
fun getInstance(): MediaRepository {
return INSTANCE ?: synchronized(this) {
val service: MediaService = retrofit.create(MediaService::class.java)
MediaRepository(service).also { INSTANCE = it }
}
}
}
} }