-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ユーザの操作に関するエラーをアプリ全体で処理できるようにした
- Loading branch information
1 parent
b0f6265
commit 11d76ef
Showing
5 changed files
with
167 additions
and
2 deletions.
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
110 changes: 110 additions & 0 deletions
110
.../java/net/pantasystem/milktea/common_android_ui/error/UserActionAppGlobalErrorListener.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,110 @@ | ||
package net.pantasystem.milktea.common_android_ui.error | ||
|
||
import android.app.Dialog | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.coroutineScope | ||
import androidx.lifecycle.flowWithLifecycle | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import net.pantasystem.milktea.app_store.handler.AppGlobalError | ||
import net.pantasystem.milktea.app_store.handler.UserActionAppGlobalErrorStore | ||
import net.pantasystem.milktea.common.APIError | ||
import net.pantasystem.milktea.common_android.resource.getString | ||
import net.pantasystem.milktea.common_android_ui.APIErrorStringConverter | ||
import net.pantasystem.milktea.common_android_ui.R | ||
import net.pantasystem.milktea.model.account.UnauthorizedException | ||
import java.io.IOException | ||
import javax.inject.Inject | ||
|
||
class UserActionAppGlobalErrorListener @Inject constructor( | ||
private val userActionAppGlobalErrorStore: UserActionAppGlobalErrorStore, | ||
@ApplicationContext private val context: Context, | ||
) { | ||
|
||
operator fun invoke( | ||
lifecycle: Lifecycle, | ||
fragmentManager: FragmentManager, | ||
) { | ||
userActionAppGlobalErrorStore.errorFlow.onEach { appGlobalError -> | ||
when (appGlobalError.level) { | ||
AppGlobalError.ErrorLevel.Info -> {} | ||
AppGlobalError.ErrorLevel.Warning -> { | ||
// toastで表示する | ||
Toast.makeText( | ||
context, | ||
appGlobalError.message.getString(context), | ||
Toast.LENGTH_SHORT | ||
) | ||
.show() | ||
} | ||
|
||
AppGlobalError.ErrorLevel.Error -> { | ||
val title = when (val error = appGlobalError.throwable) { | ||
is IOException -> { | ||
context.getString(R.string.network_error) | ||
} | ||
|
||
is APIError -> { | ||
APIErrorStringConverter()(error).getString(context) | ||
} | ||
|
||
is UnauthorizedException -> { | ||
context.getString(R.string.unauthorized_error) | ||
} | ||
|
||
else -> "Unknown error" | ||
|
||
} | ||
val dialogInstance = UserActionAppGlobalErrorDialog.newInstance( | ||
title = title, | ||
message = appGlobalError.message.getString(context) | ||
) | ||
fragmentManager.findFragmentByTag("error_dialog")?.let { | ||
fragmentManager.beginTransaction().remove(it).commit() | ||
} | ||
dialogInstance.show(fragmentManager, "error_dialog") | ||
} | ||
} | ||
}.flowWithLifecycle( | ||
lifecycle, | ||
Lifecycle.State.RESUMED, | ||
).launchIn(lifecycle.coroutineScope) | ||
} | ||
} | ||
|
||
class UserActionAppGlobalErrorDialog : DialogFragment() { | ||
companion object { | ||
const val EXTRA_TITLE = "title" | ||
const val EXTRA_MESSAGE = "message" | ||
|
||
fun newInstance(title: String, message: String): UserActionAppGlobalErrorDialog { | ||
return UserActionAppGlobalErrorDialog().apply { | ||
arguments = Bundle().apply { | ||
putString(EXTRA_TITLE, title) | ||
putString(EXTRA_MESSAGE, message) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
return MaterialAlertDialogBuilder(requireContext()) | ||
.setTitle( | ||
arguments?.getString(EXTRA_TITLE) ?: "" | ||
) | ||
.setMessage( | ||
arguments?.getString(EXTRA_MESSAGE) ?: "" | ||
) | ||
.setPositiveButton(android.R.string.ok) { _, _ -> | ||
dismiss() | ||
} | ||
.create() | ||
} | ||
} |
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