-
Notifications
You must be signed in to change notification settings - Fork 8
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 #1 from wimvdputten/develop
Develop
- Loading branch information
Showing
54 changed files
with
1,720 additions
and
223 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
146 changes: 146 additions & 0 deletions
146
app/src/main/java/xo/william/pixeldrain/FileViewActivity.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,146 @@ | ||
package xo.william.pixeldrain | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.widget.* | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.MutableLiveData | ||
import com.bumptech.glide.Glide | ||
import com.github.kittinunf.fuel.core.requests.CancellableRequest | ||
import com.github.kittinunf.fuel.core.requests.tryCancel | ||
import com.github.kittinunf.result.Result | ||
import com.google.android.exoplayer2.MediaItem | ||
import com.google.android.exoplayer2.SimpleExoPlayer | ||
import com.google.android.exoplayer2.ui.PlayerView | ||
import kotlinx.android.synthetic.main.activity_file_view.* | ||
import kotlinx.serialization.decodeFromString | ||
import kotlinx.serialization.json.Json | ||
import xo.william.pixeldrain.api.FuelService | ||
import xo.william.pixeldrain.fileList.InfoModel | ||
|
||
|
||
class FileViewActivity : AppCompatActivity() { | ||
|
||
private val format = Json { ignoreUnknownKeys = true } | ||
private lateinit var infoModel: InfoModel; | ||
|
||
private lateinit var exoPlayer: SimpleExoPlayer | ||
private lateinit var request: CancellableRequest | ||
|
||
private var textLiveData = MutableLiveData<String>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_file_view) | ||
setSupportActionBar(sub_toolbar) | ||
|
||
supportActionBar?.apply { | ||
setDisplayHomeAsUpEnabled(true) | ||
} | ||
|
||
val infoModelString: String? = intent.getStringExtra("infoModel") | ||
if (infoModelString !== null) { | ||
infoModel = format.decodeFromString(infoModelString); | ||
} else { | ||
infoModel = InfoModel("") | ||
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show(); | ||
} | ||
|
||
loadFile() | ||
} | ||
|
||
private fun loadFile() { | ||
val type = infoModel.mime_type; | ||
if (type.contains("image")) { | ||
loadImage(); | ||
} | ||
if (type.contains("video") || type.contains("audio")) { | ||
loadVideo() | ||
} | ||
if (type.contains("text")) { | ||
loadText(); | ||
} | ||
} | ||
|
||
private fun loadImage() { | ||
val imageFile = findViewById<ImageView>(R.id.imageFile) | ||
val fileProgress = findViewById<ProgressBar>(R.id.fileProgressBar); | ||
try { | ||
val urlString = infoModel.getFileUrl() | ||
imageFile.visibility = View.VISIBLE | ||
imageFile.contentDescription = infoModel.name; | ||
Glide.with(this).load(urlString).fitCenter().into(imageFile) | ||
fileProgress.visibility = View.GONE | ||
} catch (e: Exception) { | ||
fileProgress.visibility = View.GONE | ||
Toast.makeText(this, "Error: ${e.message}", Toast.LENGTH_LONG).show(); | ||
Log.e("loadImage", "error: " + infoModel.getThumbnailUrl() + " " + e.message) | ||
} | ||
} | ||
|
||
private fun loadVideo() { | ||
val fileProgress = findViewById<ProgressBar>(R.id.fileProgressBar) | ||
fileProgress.visibility = View.GONE | ||
val videoExoFile = findViewById<PlayerView>(R.id.videoExoFile) | ||
videoExoFile.visibility = View.VISIBLE | ||
exoPlayer = SimpleExoPlayer.Builder(this).build() | ||
videoExoFile.player = exoPlayer; | ||
|
||
val mediaItem: MediaItem = MediaItem.fromUri(infoModel.getFileUrl()) | ||
exoPlayer.setMediaItem(mediaItem); | ||
exoPlayer.prepare() | ||
|
||
exoPlayer.play() | ||
} | ||
|
||
private fun loadText() { | ||
request = FuelService().getFileText(infoModel.getFileUrl()) | ||
.responseString() { _, _, result -> | ||
when (result) { | ||
is Result.Success -> { | ||
Log.d("text", "text ${result.get()}") | ||
textLiveData.postValue(result.get()) | ||
} | ||
is Result.Failure -> { | ||
Log.d("text", "error ${result.error.exception.message}") | ||
textLiveData.postValue(result.error.exception.message) | ||
} | ||
} | ||
} | ||
|
||
textLiveData.observe(this, { | ||
val fileProgress = findViewById<ProgressBar>(R.id.fileProgressBar) | ||
val textFile = findViewById<TextView>(R.id.textFile) | ||
val textScrollView = findViewById<ScrollView>(R.id.textScrollView) | ||
fileProgress.visibility = View.GONE | ||
textFile.text = it | ||
textFile.visibility = View.VISIBLE | ||
textScrollView.visibility = View.VISIBLE | ||
}) | ||
} | ||
|
||
|
||
override fun onBackPressed() { | ||
if (this::exoPlayer.isInitialized) { | ||
exoPlayer.release(); | ||
} | ||
if (this::request.isInitialized) { | ||
request.tryCancel() | ||
} | ||
super.onBackPressed() | ||
} | ||
|
||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
if (this::exoPlayer.isInitialized) { | ||
exoPlayer.release(); | ||
} | ||
if (this::request.isInitialized) { | ||
request.tryCancel() | ||
} | ||
finish(); | ||
return super.onOptionsItemSelected(item) | ||
} | ||
} |
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,76 @@ | ||
package xo.william.pixeldrain | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.view.MenuItem | ||
import android.view.View | ||
import android.widget.Toast | ||
import androidx.core.widget.doOnTextChanged | ||
import androidx.lifecycle.Observer | ||
import androidx.lifecycle.ViewModelProvider | ||
import com.github.kittinunf.fuel.core.requests.tryCancel | ||
import kotlinx.android.synthetic.main.activity_file_view.* | ||
import kotlinx.android.synthetic.main.activity_login.* | ||
import kotlinx.android.synthetic.main.activity_login.sub_toolbar | ||
import xo.william.pixeldrain.model.LoginResponse | ||
import xo.william.pixeldrain.model.LoginViewModel | ||
import xo.william.pixeldrain.repository.SharedRepository | ||
|
||
class LoginActivity : AppCompatActivity() { | ||
|
||
private var username = "" | ||
private var password = "" | ||
|
||
private lateinit var loginViewModel: LoginViewModel | ||
private lateinit var sharedRepository: SharedRepository | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
sharedRepository = SharedRepository(this) | ||
|
||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_login) | ||
setSupportActionBar(sub_toolbar) | ||
supportActionBar?.apply { | ||
setDisplayHomeAsUpEnabled(true) | ||
} | ||
|
||
loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java) | ||
|
||
userInput.doOnTextChanged { text, _, _, _ -> | ||
username = text.toString() | ||
loginButton.isEnabled = (username.isNotEmpty() && password.isNotEmpty()) | ||
} | ||
|
||
passwordInput.doOnTextChanged { text, _, _, _ -> | ||
password = text.toString() | ||
loginButton.isEnabled = (username.isNotEmpty() && password.isNotEmpty()) | ||
} | ||
|
||
loginButton.setOnClickListener { | ||
loginButton.isEnabled = false | ||
loginProgress.visibility = View.VISIBLE | ||
|
||
loginViewModel.loginUser(username, password) | ||
} | ||
|
||
loginViewModel.loginResponse.observe(this, | ||
{ response -> handleLoginResponse(response) }) | ||
} | ||
|
||
private fun handleLoginResponse(response: LoginResponse) { | ||
loginProgress.visibility = View.GONE | ||
if (response.auth_key.isNotEmpty()) { | ||
sharedRepository.saveToken(response.auth_key) | ||
setResult(200) | ||
finish() | ||
} else { | ||
loginButton.isEnabled = true | ||
Toast.makeText(this, "Error: ${response.message}", Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
finish(); | ||
return super.onOptionsItemSelected(item) | ||
} | ||
} |
Oops, something went wrong.