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
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
val view = inflater.inflate(R.layout.tasks_frag, container, false) | |
viewDataBinding = TaskdetailFragBinding.bind(view).apply { | |
viewmodel = viewModel | |
} | |
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
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
viewDataBinding = TasksFragBinding.inflate(inflater, container, false).apply { | |
viewmodel = viewModel | |
} | |
... | |
... | |
return viewDataBinding.root |
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
<?xml version="1.0" encoding="utf-8"?> | |
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item | |
android:id="@id/tasksFragment" | |
android:icon="@drawable/ic_list" | |
android:title="@string/list_title"/> | |
<item | |
android:id="@id/statisticsFragment" |
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
class TasksRepository ( | |
private val tasksRemoteDataSource: TasksDataSource, | |
private val tasksLocalDataSource: TasksDataSource, | |
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO | |
) : ITasksRepository { | |
private var cachedTasks: ConcurrentMap<String, Task>? = null | |
override suspend fun getTasks(forceUpdate: Boolean): Result<List<Task>> { | |
wrapEspressoIdlingResource { |
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
class TasksLocalDataSource @Inject constructor( | |
private val tasksDao: TasksDao, | |
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO | |
) : TasksDataSource { | |
override suspend fun getTasks(): Result<List<Task>> = withContext(ioDispatcher) { | |
return@withContext try { | |
Success(tasksDao.getTasks()) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
Error(e) |
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
class ViewModel { | |
fun getToken(): Token { | |
// 做一些 API 耗時操作 | |
return token 請求 access token | |
} | |
fun verify(token, userName): Boolean { | |
// 做一些 API 耗時操作 | |
return result // 登入成功或失敗 |
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
fun getTokenAsync(action: (Token) -> Unit) { | |
...... | |
} | |
fun verifyAsync( | |
token: Token, | |
userName: String, | |
action: (Boolean) -> Unit | |
) { | |
...... |
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
fun getToken(): Observable<Token> { ...... } | |
fun verify(userName): Observable<Boolean> { | |
return getToken() | |
.flatMap { token -> | |
...... | |
} | |
} | |
fun requestAndSaveUserData(userName: String): Observable<Result> { |
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
suspend fun getToken(): Token { ...... } | |
suspend fun verify(token, userName: String): Boolean { ...... } | |
suspend fun requestAndSaveUserData(userName: String): Result { ...... } | |
fun login(userName: String) { | |
GlobalScope.launch { | |
try { | |
val token = getToken() |
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
class MainActivity : AppCompatActivity(), CoroutineScope { | |
override val coroutineContext: CoroutineContext | |
get() = job | |
val job = Job() // 默認使用預設 thread | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) |
OlderNewer