Remove comment draft if user cleared the text input or the comment has been sent.

This commit is contained in:
Docile-Alligator 2025-07-04 17:15:41 -04:00
parent 703e1acb4a
commit a2d991e6d0
9 changed files with 42 additions and 22 deletions

View File

@ -69,6 +69,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.activity:activity:1.10.1'
implementation 'androidx.fragment:fragment:1.8.8'
implementation 'androidx.core:core-ktx:1.16.0'
def lifecycleVersion = "2.7.0"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycleVersion"

View File

@ -34,6 +34,7 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:enableOnBackInvokedCallback="true"
tools:replace="android:label">
<activity
android:name=".activities.LoginChromeCustomTabActivity"

View File

@ -17,6 +17,7 @@ import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -25,7 +26,6 @@ import androidx.core.graphics.Insets;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.bumptech.glide.Glide;
@ -67,7 +67,6 @@ import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomS
import ml.docilealligator.infinityforreddit.bottomsheetfragments.GiphyGifInfoBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.UploadedImagesBottomSheetFragment;
import ml.docilealligator.infinityforreddit.comment.Comment;
import ml.docilealligator.infinityforreddit.comment.CommentDraft;
import ml.docilealligator.infinityforreddit.comment.SendComment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.customviews.LinearLayoutManagerBugFixed;
@ -380,15 +379,19 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
).get(CommentActivityViewModel.class);
if (savedInstanceState == null) {
commentActivityViewModel.getCommentDraft(parentFullname).observe(this, new Observer<CommentDraft>() {
@Override
public void onChanged(CommentDraft commentDraft) {
if (commentDraft != null) {
binding.commentCommentEditText.setText(commentDraft.getContent());
}
commentActivityViewModel.getCommentDraft(parentFullname).observe(this, commentDraft -> {
if (commentDraft != null) {
binding.commentCommentEditText.setText(commentDraft.getContent());
}
});
}
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
handleBackPress();
}
});
}
private void loadCurrentAccount() {
@ -476,7 +479,7 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
onBackPressed();
getOnBackPressedDispatcher().onBackPressed();
return true;
} else if (itemId == R.id.action_preview_comment_activity) {
Intent intent = new Intent(this, FullMarkdownActivity.class);
@ -532,7 +535,10 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
returnIntent.putExtra(EXTRA_PARENT_POSITION_KEY, parentPosition);
}
setResult(RESULT_OK, returnIntent);
finish();
commentActivityViewModel.deleteCommentDraft(parentFullname, () -> {
finish();
return Unit.INSTANCE;
});
}
@Override
@ -594,13 +600,15 @@ public class CommentActivity extends BaseActivity implements UploadImageEnabledA
}
}
@Override
public void onBackPressed() {
private void handleBackPress() {
if (isSubmitting) {
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail, false);
} else {
if (binding.commentCommentEditText.getText().toString().equals("")) {
finish();
if (binding.commentCommentEditText.getText().toString().isEmpty()) {
commentActivityViewModel.deleteCommentDraft(parentFullname, () -> {
finish();
return Unit.INSTANCE;
});
} else {
promptAlertDialog(R.string.save_comment_draft, R.string.save_comment_draft_detail, true);
}

View File

@ -13,13 +13,11 @@ import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
@ -38,7 +36,6 @@ import androidx.core.graphics.Insets;
import androidx.core.splashscreen.SplashScreen;
import androidx.core.view.OnApplyWindowInsetsListener;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
@ -217,6 +214,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private int inboxCount;
private ActivityMainBinding binding;
@ExperimentalBadgeUtils
@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this);
@ -428,6 +426,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
applyFABTheme(navigationWrapper.floatingActionButton);
}
@ExperimentalBadgeUtils
private void initializeNotificationAndBindView() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityResultLauncher<String> requestNotificationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), result -> mInternalSharedPreferences.edit().putBoolean(SharedPreferencesUtils.HAS_REQUESTED_NOTIFICATION_PERMISSION, true).apply());

View File

@ -179,7 +179,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
mActivity = activity;
mFragment = fragment;
mExecutor = executor;
mRetrofit =
mRetrofit = retrofit;
mOauthRetrofit = oauthRetrofit;
mAccessToken = accessToken;
mAccountName = accountName;

View File

@ -13,7 +13,7 @@ interface CommentDraftDao {
suspend fun insert(commentDraft: CommentDraft)
@Delete
fun delete(commentDraft: CommentDraft)
suspend fun delete(commentDraft: CommentDraft)
@Query("SELECT * FROM comment_draft WHERE parent_full_name = :parentFullName")
fun getCommentDraftLiveData(parentFullName: String): LiveData<CommentDraft>

View File

@ -55,7 +55,7 @@ public class AccessTokenAuthenticator implements Authenticator {
String accessTokenFromDatabase = account.getAccessToken();
if (accessToken.equals(accessTokenFromDatabase)) {
String newAccessToken = refreshAccessToken(account);
if (!newAccessToken.equals("")) {
if (!newAccessToken.isEmpty()) {
return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build();
} else {
return null;

View File

@ -7,11 +7,15 @@ import ml.docilealligator.infinityforreddit.comment.CommentDraftDao
class CommentActivityRepository(
private val commentDraftDao: CommentDraftDao
) {
public fun getCommentDraft(parentFullname: String): LiveData<CommentDraft> {
fun getCommentDraft(parentFullname: String): LiveData<CommentDraft> {
return commentDraftDao.getCommentDraftLiveData(parentFullname)
}
public suspend fun saveCommentDraft(parentFullname: String, content: String) {
suspend fun saveCommentDraft(parentFullname: String, content: String) {
commentDraftDao.insert(CommentDraft(parentFullname, content, System.currentTimeMillis()))
}
suspend fun deleteCommentDraft(parentFullname: String) {
commentDraftDao.delete(CommentDraft(parentFullname, "", 0))
}
}

View File

@ -23,6 +23,13 @@ class CommentActivityViewModel(
}
}
fun deleteCommentDraft(parentFullname: String, onDeleted: () -> Unit) {
viewModelScope.launch {
commentActivityRepository.deleteCommentDraft(parentFullname)
onDeleted()
}
}
companion object {
fun provideFactory(commentActivityRepository: CommentActivityRepository) : ViewModelProvider.Factory {
return object: ViewModelProvider.Factory {