-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JuTaK97
committed
Jul 2, 2023
1 parent
eea75da
commit f263b13
Showing
7 changed files
with
66 additions
and
45 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
50 changes: 15 additions & 35 deletions
50
app/src/main/java/com/wafflestudio/snutt2/views/RNModuleActivity.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,64 +1,44 @@ | ||
package com.wafflestudio.snutt2.views | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.facebook.hermes.reactexecutor.HermesExecutorFactory | ||
import com.facebook.react.ReactInstanceManager | ||
import com.facebook.react.ReactRootView | ||
import com.facebook.react.common.LifecycleState | ||
import com.facebook.react.shell.MainReactPackage | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
|
||
class RNModuleActivity: Activity() { | ||
@AndroidEntryPoint | ||
class RNModuleActivity : AppCompatActivity() { | ||
|
||
private val rnViewModel: RNViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
GlobalScope.launch(Dispatchers.IO) { | ||
val urlConnection = URL("https://snutt-rn-assets.s3.ap-northeast-2.amazonaws.com/android.jsbundle").openConnection() as HttpURLConnection | ||
urlConnection.connect() | ||
val inputStream = urlConnection.inputStream | ||
val outputFile = File(applicationContext.cacheDir, "android.jsbundle") | ||
|
||
val outputStream = FileOutputStream(outputFile) | ||
val buffer = ByteArray(1024000) | ||
var bytesRead: Int | ||
|
||
while (inputStream.read(buffer).also { bytesRead = it } != -1) { | ||
outputStream.write(buffer, 0, bytesRead) | ||
} | ||
outputStream.close() | ||
inputStream.close() | ||
urlConnection.disconnect() | ||
|
||
val jsBundleFile = File(applicationContext.cacheDir, "android.jsbundle") | ||
|
||
withContext(Dispatchers.Main) { | ||
rnViewModel.done.observe(this) { done -> | ||
if (done) { | ||
val jsBundleFile = File(applicationContext.cacheDir, "android.jsbundle") | ||
val reactInstanceManager = ReactInstanceManager.builder() | ||
.setApplication(application) | ||
.setCurrentActivity(this@RNModuleActivity) | ||
.setJSBundleFile(jsBundleFile.absolutePath) | ||
.addPackage(MainReactPackage()) | ||
.setInitialLifecycleState(LifecycleState.RESUMED) | ||
.setJavaScriptExecutorFactory(HermesExecutorFactory()) | ||
.build() | ||
|
||
val rootView = ReactRootView(this@RNModuleActivity) | ||
rootView.startReactApplication(reactInstanceManager, "friends", null) | ||
setContentView(rootView) | ||
} | ||
} | ||
} | ||
|
||
// val request = DownloadManager.Request(Uri.parse("https://snutt-rn-assets.s3.ap-northeast-2.amazonaws.com/android.jsbundle")) | ||
// .setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "android.jsbundle") | ||
// .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) | ||
// | ||
// val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
// downloadManager.enqueue(request) | ||
override fun onDestroy() { | ||
super.onDestroy() | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/wafflestudio/snutt2/views/RNViewModel.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,45 @@ | ||
package com.wafflestudio.snutt2.views | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class RNViewModel @Inject constructor( | ||
application: Application | ||
) : AndroidViewModel(application) { | ||
|
||
private val _done = MutableLiveData(false) | ||
val done: LiveData<Boolean> get() = _done | ||
|
||
init { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
val urlConnection = URL("https://snutt-rn-assets.s3.ap-northeast-2.amazonaws.com/android.jsbundle").openConnection() as HttpURLConnection | ||
urlConnection.connect() | ||
val inputStream = urlConnection.inputStream | ||
val outputFile = File(application.applicationContext.cacheDir, "android.jsbundle") | ||
|
||
val outputStream = FileOutputStream(outputFile) | ||
val buffer = ByteArray(1024000) | ||
var bytesRead: Int | ||
|
||
while (inputStream.read(buffer).also { bytesRead = it } != -1) { | ||
outputStream.write(buffer, 0, bytesRead) | ||
} | ||
outputStream.close() | ||
inputStream.close() | ||
urlConnection.disconnect() | ||
_done.postValue(true) | ||
} | ||
} | ||
} |
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