-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Konyaco/dev
Dev
- Loading branch information
Showing
26 changed files
with
862 additions
and
638 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
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
33 changes: 19 additions & 14 deletions
33
android/src/main/java/me/konyaco/collinsdictionary/MyApplication.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 |
---|---|---|
@@ -1,24 +1,29 @@ | ||
package me.konyaco.collinsdictionary | ||
|
||
import android.app.Application | ||
import me.konyaco.collinsdictionary.repository.Repository | ||
import me.konyaco.collinsdictionary.service.CollinsOnlineDictionary | ||
import me.konyaco.collinsdictionary.service.LocalCacheDictionary | ||
import me.konyaco.collinsdictionary.store.FileBasedLocalStorage | ||
import me.konyaco.collinsdictionary.viewmodel.AppViewModel | ||
import me.konyaco.collinsdictionary.store.LocalStorage | ||
import org.koin.core.context.startKoin | ||
import org.koin.dsl.module | ||
|
||
class MyApplication : Application() { | ||
lateinit var repository: Repository | ||
lateinit var viewModel: AppViewModel | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
repository = Repository( | ||
CollinsOnlineDictionary(), | ||
LocalCacheDictionary(FileBasedLocalStorage(filesDir.resolve("cache").also { | ||
if (!it.exists()) it.mkdir() | ||
})) | ||
) | ||
viewModel = AppViewModel(repository) | ||
initKoin() | ||
} | ||
|
||
private fun initKoin() { | ||
startKoin { | ||
modules( | ||
commonModule, | ||
module { | ||
single<LocalStorage> { | ||
FileBasedLocalStorage(filesDir.resolve("cache").also { | ||
if (!it.exists()) it.mkdir() | ||
}) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
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
95 changes: 95 additions & 0 deletions
95
common/src/androidMain/kotlin/me/konyaco/collinsdictionary/service/AndroidSoundPlayer.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,95 @@ | ||
package me.konyaco.collinsdictionary.service | ||
|
||
import android.content.Context | ||
import android.media.AudioAttributes | ||
import android.media.MediaPlayer | ||
import android.util.Log | ||
import androidx.core.net.toUri | ||
import io.ktor.client.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import kotlinx.coroutines.* | ||
import java.io.File | ||
import kotlin.coroutines.resume | ||
|
||
private const val TAG = "AndroidSoundPlayer.android" | ||
|
||
class AndroidSoundPlayer : SoundPlayer { | ||
var context: Context? = null | ||
|
||
override fun play( | ||
url: String, | ||
onStart: () -> Unit, | ||
onStop: () -> Unit, | ||
onError: (e: Throwable) -> Unit | ||
) { | ||
job.let { | ||
job = scope.async(Dispatchers.IO) { | ||
val file = try { | ||
getFile(url) | ||
} catch (e: Throwable) { | ||
Log.w(TAG, "Failed to download sound", e) | ||
onError(e) | ||
return@async | ||
} | ||
it?.cancelAndJoin() | ||
try { | ||
onStart() | ||
playMedia(file) | ||
onStop() | ||
} catch (e: Throwable) { | ||
Log.w("Failed to play sound", e) | ||
onError(e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val scope = CoroutineScope(Dispatchers.Default) | ||
private var job: Job? = null | ||
|
||
private suspend fun playMedia(file: File) { | ||
suspendCancellableCoroutine<Unit> { continuation -> | ||
val mediaPlayer = MediaPlayer() | ||
continuation.invokeOnCancellation { | ||
mediaPlayer.pause() | ||
mediaPlayer.release() | ||
} | ||
with(mediaPlayer) { | ||
setOnCompletionListener { | ||
release() | ||
continuation.resume(Unit) | ||
} | ||
setAudioAttributes( | ||
AudioAttributes.Builder() | ||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) | ||
.setUsage(AudioAttributes.USAGE_MEDIA) | ||
.build() | ||
) | ||
setDataSource(context!!, file.toUri()) | ||
prepare() | ||
start() | ||
} | ||
} | ||
} | ||
|
||
private fun directory(): File = | ||
context!!.externalCacheDir!!.resolve("sound").also { it.mkdir() } | ||
|
||
// Get filename part of url (xx://xx/xxx.mp3) | ||
private fun fileNameFromUrl(url: String) = url.substringAfterLast("/") | ||
|
||
private suspend fun getFile(url: String): File { | ||
val fileName = fileNameFromUrl(url) | ||
val file = directory().resolve(fileName) | ||
if (!file.exists()) { | ||
val bytes = HttpClient().get(url).readBytes() | ||
withContext(Dispatchers.IO) { | ||
file.outputStream().use { | ||
it.write(bytes) | ||
} | ||
} | ||
} | ||
return file | ||
} | ||
} |
95 changes: 1 addition & 94 deletions
95
common/src/androidMain/kotlin/me/konyaco/collinsdictionary/service/SoundPlayer.android.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
16 changes: 16 additions & 0 deletions
16
common/src/androidMain/kotlin/me/konyaco/collinsdictionary/ui/util/onEnterPress.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,16 @@ | ||
package me.konyaco.collinsdictionary.ui.util | ||
|
||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.input.key.Key | ||
import androidx.compose.ui.input.key.key | ||
import androidx.compose.ui.input.key.onKeyEvent | ||
|
||
@OptIn(ExperimentalComposeUiApi::class) | ||
actual fun Modifier.onEnterPress(onPress: () -> Unit) = | ||
this.onKeyEvent { | ||
return@onKeyEvent if (it.key == Key.Enter) { | ||
onPress() | ||
true | ||
} else false | ||
} |
Oops, something went wrong.