This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Follow hashtag button #346
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7050237
ui setup
ff2b36d
adding get hashtag usecase
a254b33
setup stuff
5d59689
fixing issues
c9a96a6
fixing capitalization bug
2b235f9
cleanup
ef76353
Merge branch 'main' of github.com:MozillaSocial/MozillaSocialAndroid …
25c1c6d
adding analytics event for clicking the follow button
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...ase/mastodon/src/main/java/org/mozilla/social/core/usecase/mastodon/hashtag/GetHashTag.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.mozilla.social.core.usecase.mastodon.hashtag | ||
|
||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emitAll | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import org.mozilla.social.common.Resource | ||
import org.mozilla.social.common.annotations.PreferUseCase | ||
import org.mozilla.social.core.model.HashTag | ||
import org.mozilla.social.core.repository.mastodon.HashtagRepository | ||
import timber.log.Timber | ||
|
||
class GetHashTag( | ||
private val hashtagRepository: HashtagRepository, | ||
private val dispatcherIo: CoroutineDispatcher = Dispatchers.IO, | ||
) { | ||
|
||
@OptIn(PreferUseCase::class) | ||
suspend operator fun invoke( | ||
name: String, | ||
coroutineScope: CoroutineScope, | ||
): Flow<Resource<HashTag>> = | ||
flow { | ||
emit(Resource.Loading()) | ||
val deferred = CompletableDeferred<Resource<Unit>>() | ||
// the hashtag from the server might be lower case, so we need to assign this | ||
// to whatever we get back from the server | ||
var realName: String = name | ||
coroutineScope.launch(dispatcherIo) { | ||
try { | ||
val hashtag = hashtagRepository.getHashTag(realName) | ||
realName = hashtag.name | ||
hashtagRepository.insert(hashtag) | ||
deferred.complete( | ||
Resource.Loaded( | ||
Unit | ||
) | ||
) | ||
} catch (e: Exception) { | ||
Timber.e(e) | ||
deferred.complete(Resource.Error(e)) | ||
} | ||
} | ||
when (val deferredResult = deferred.await()) { | ||
is Resource.Error -> emit(Resource.Error(deferredResult.exception)) | ||
else -> { | ||
try { | ||
emitAll(hashtagRepository.getHashTagFlow(realName).map { Resource.Loaded(it) }) | ||
} catch (e: Exception) { | ||
Timber.e(e) | ||
emit(Resource.Error(e)) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetHashTag
has another parameter for the coroutine dispatcher with a default value, so we have to use the normal constructor pattern